TestNG使用總結(jié)

        TestNG是誕生于JUnit3和JUnit4之間的一個測試框架,雖然在JUnit4之前誕生,但被認(rèn)為更先進(jìn),NG是Next Generation的意思。TestNG擁有諸多新特性,以及消除了JUnit中的許多限制,更適合于大型項目的測試。

成都創(chuàng)新互聯(lián)主營華亭網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),華亭h5小程序開發(fā)搭建,華亭網(wǎng)站營銷推廣歡迎華亭等地區(qū)企業(yè)咨詢

常用注解:

@BeforeSuiteThe annotated method will be run before all tests in this suite have run. 
@AfterSuiteThe annotated method will be run after all tests in this suite have run. 
@BeforeClassThe annotated method will be run before the first test method in the current class is invoked. 
@AfterClassThe annotated method will be run after all the test methods in the current class have been run. 
@BeforeTestThe annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterTestThe annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run. 
@BeforeMethodThe annotated method will be run before each test method. 
@AfterMethodThe annotated method will be run after each test method.
@BeforeGroupsThe list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked. 
@AfterGroupsThe list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked. 
@TestA Test Case

注解使用示例:

public class OrderTest {
    @BeforeSuite
    public void testBeforeSuite(){System.out.println("BeforeSuite");}
    @AfterSuite
    public void testAfterSuite(){System.out.println("AfterSuite");}
    @BeforeTest
    public void testBeforeTest(){System.out.println("BeforeTest");}
    @AfterTest
    public void testAfterTest(){System.out.println("AfterTest");}
    @BeforeClass
    public void testBeforeClass(){System.out.println("BeforeClass");}
    @AfterClass
    public void testAfterClass(){System.out.println("AfterClass");}
    @BeforeGroups(groups = "data")
    public void testBeforeGroups(){System.out.println("BeforeGroups");}
    @AfterGroups(groups = "data")
    public void testAfterGroups(){System.out.println("AfterGroups");}
    @BeforeMethod
    public void testBeforeMethod(){System.out.println("BeforeMethod");}
    @AfterMethod
    public void testAfterMethod(){System.out.println("AfterMethod");}
    @Test(groups = "data")
    public void testGroup1(){System.out.println("1、Belong to group data");}
    @Test(groups = "data")
    public void testGroup2(){System.out.println("2、Belong to group data");}
}

執(zhí)行順序:
    BeforeSuite
    BeforeTest
    BeforeClass
    BeforeGroups
    BeforeMethod
    1、Belong to group data
    AfterMethod
    BeforeMethod
    2、Belong to group data
    AfterMethod
    AfterGroups
    AfterClass
    AfterTest
    AfterSuite
    
備注:
    其中BeforeGroups和AfterGroups會在指定組的所有方法執(zhí)行之前和之后執(zhí)行。


超時測試:

@Test(timeOut = 1000)

忽略測試:

@Test(enabled = false)

異常測試:

@Test(expectedExceptions = ArrayIndexOutOfBoundsException.class)

用例分組:

@Test(groups = {"group_one", "group_two"})

依賴測試:

@Test

public void testOne(){}

@Test(dependsOnMethods = "testOne")

public void testTwo(){}

多次執(zhí)行以及使用線程池:

@Test(invocationCount = 5, threadPoolSize = 5)

用例執(zhí)行5次,使用5個線程并行執(zhí)行


套件測試

需要借助XML文件,運(yùn)行時需要指定XML才能正確執(zhí)行

//測試整個NewTest以及OldTest中的testThird方法
<suite name="Method Suit" verbose="1">
    <test name="includedGroupsInAPackage">
        <classes>
            <class name="com.me.hello.NewTest"/>
            <class name="com.me.hello.OldTest">
                <methods>
                    <include name="testThird"/>
                </methods>
            </class>
        </classes>
    </test>
</suite>

//測試指定兩個package里的所有測試類
<test name="tttt">
<packages>
    <package name="com.me.hello"/>
    <package name="com.me.chat"/>
</packages>
</test>
        
//指定package中的分組測試,以及指定class測試。
<suite name="Group Suit" verbose="10">
<test name="tttt">
    <groups>
        <run>
            <include name="slow"/>//分組測試
            <include name="fast"/>
        </run>
    </groups>
    <packages>
        <!--<package name="com.me.good"/>-->
        <!--<package name="com.me.hello"/>-->
        <package name="com.me.*"/>
    </packages>
</test>
<test name="next test">
    <classes>
        <class name="com.me.hello.NewTest"/>//指定類
    </classes>
</test>
</suite>

備注:
   IDEA的套件測試需要在Run-Edit Configurations中配置運(yùn)行條件以及指定xml文件,或者直接命令行執(zhí)行。


參數(shù)化測試:

1、通過xml文件(基本數(shù)據(jù)類型)

2、代碼中使用@DataProvider(復(fù)雜數(shù)據(jù)類型)

@DataProvider(name = "getData")
public Object[][] getDataFromFile(){
    return new Object[][]{{1,2},{3,4},{5,6}};
}

@Test(dataProvider = "getData")
public void testCalc(int a, int e){
    Assert.assertEquals(a+1, e);
}

1、聲明一個方法給定名字,返回數(shù)組數(shù)據(jù)。
2、Test方法根據(jù)DataProvider方法返回的數(shù)據(jù),設(shè)定參數(shù)列表。
3、按照參數(shù)列表順序操作參數(shù)。

斷言

       TestNG的斷言總體和JUnit相似,大體上區(qū)別只是把提示消息位置放在后面而已。

總結(jié):

      相比JUnit,TestNG的參數(shù)化測試,依賴測試,分組測試都更方便使用。另外套件測試使用XML和代碼實現(xiàn)數(shù)據(jù)和測試邏輯的分離,并且方便定制分類邏輯更復(fù)雜的測試。

分享名稱:TestNG使用總結(jié)
鏈接分享:http://muchs.cn/article24/jehjje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、用戶體驗、網(wǎng)站建設(shè)建站公司、外貿(mào)建站、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)