目錄html
1、概述java
2、@Test註解經常使用參數數組
3、測試中經常使用的斷言(assert)瀏覽器
4、TestNG經常使用註解及使用多線程
5、配置文件xml經常使用標籤框架
6、參數傳遞ide
7、測試報告測試
一、TestNG是一個開源自動化測試框架,其靈感來自JUnit和NUnit,TestNG還涵蓋了整個核心的JUnit4功能,但引入了一些新的功能,使其功能更強大,使用更方便。ui
優點:支持依賴測試方法,並行測試,負載測試,局部故障;靈活的插件API;支持多線程測試;spa
二、Maven依賴
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.7</version> <scope>test</scope> </dependency>
注:Eclicpe上想要直接運行還須要安裝testng插件
三、helloword
(1)被測試方法類HelloWrold:
package study.testng; public class HelloWorld { public String hello(){ return "hello world !"; } }
(2)測試類TestHelloWorld:
package study.testng; import org.testng.Assert; import org.testng.annotations.Test; public class TestHelloWorld { //測試返回結果不爲空 @Test public void tester1(){ HelloWorld hello = new HelloWorld(); String helloworld = hello.hello(); Assert.assertNotNull(helloworld); } //測試返回結果爲」hello world !「字符串 @Test public void tester2(){ HelloWorld hello = new HelloWorld(); String helloworld = hello.hello(); System.out.println(helloworld); Assert.assertEquals(helloworld, "hello world !"); } }
(3)運行測試
(4)測試結果
一、測試方法是否執行enable
默認是true,若是設置爲false,則在運行時不會執行這個測試方法;
二、預期異常expectedExeption
@Test(expectedExceptions
= ClassName
.class)
若是ClassName類拋出了異常,測算測試經過,沒有異常算測試不經過;
expectedExceptions的值也能夠是一個數組:
@Test(expectedExceptions
= {ClassName
.class, ClassName2.class,...
})
三、超時timeOut
單位爲毫秒,若是測試方法運行時間超這個值算測試不經過;
四、分組groups
(1)把在一個<test>標籤內的中全部類方法再進行組,在運行時,一個組的方法會一塊兒運行,而後再運行下一個組的方法;
(2)分組的最小維度爲方法,當把帶分組的@Test(groups = 」groupName」)註解對類使用時,這個測試類中的全部方法都屬於這同一個組;
(3)一個方法也能夠同時屬於多個組,@Test(groups = {「groupName1」, 「groupName2」}),那麼每組運行時這個方法都會被執行一次;
(4)同一個組裏的方法類,若是分別屬於兩個不一樣的測試用例(<test>)內,那麼它們其實能夠算兩個組,它們會在每一個測試用例分別運行,而不會合在一塊兒運行;
五、依賴方法dependsOnMethods
在被依賴的方法運行完成以後運行當前方法,若是依賴方法測試不經過,那麼當前方法也不會繼續運行了;依賴的方法能夠有多個;
例:@Test(dependsOnMethods = { "methodName1" , 「methodName2」 })
六、依賴組,dependsOnGroups
和依賴方法相似,在被依賴組運行完成以後運行當前組,若是依賴組中的方法沒有測試能過,那麼當前的方法也不會繼續運行了;依賴組能夠有多個;
1 assertEqual ([String message], expected value, actual value) 斷言兩個值相等。值多是類型有 int, short, long, byte, char or java.lang.Object. 第一個參數是一個可選的字符串消息; 2 assertTrue([String message], boolean condition) 斷言一個條件爲真; 3 assertFalse([String message],boolean condition) 斷言一個條件爲假; 4 assertNotNull([String message], java.lang.Object object) 斷言一個對象不爲空(null); 5 assertNull([String message], java.lang.Object object) 斷言一個對象爲空(null); 6 assertSame([String message], java.lang.Object expected, java.lang.Object actual) 斷言兩個對象引用相同的對象; 7 assertNotSame([String message], java.lang.Object unexpected, java.lang.Object actual) 斷言兩個對象不是引用同一個對象; 8 assertArrayEquals([String message], expectedArray, resultArray) 斷言預期數組和結果數組相等。數組的類型多是 int, long, short, char, byte or java.lang.Object.;
@BeforeSuite 在該套件的全部測試都運行在註釋的方法以前,僅運行一次(套件測試是一塊兒運行的多個測試類)。 @AfterSuite 在該套件的全部測試都運行在註釋方法以後,僅運行一次。 @BeforeClass 在調用當前類的第一個測試方法以前運行,註釋方法僅運行一次。 @AfterClass 在調用當前類的第一個測試方法以後運行,註釋方法僅運行一次 @BeforeTest 註釋的方法將在屬於<test>標籤內的類的全部測試方法運行以前運行。 @AfterTest 註釋的方法將在屬於<test>標籤內的類的全部測試方法運行以後運行。 @BeforeGroups 配置方法將在以前運行組列表。 此方法保證在調用屬於這些組中的任何一個的第一個測試方法以前不久運行。 @AfterGroups 此配置方法將在以後運行組列表。該方法保證在調用屬於任何這些組的最後一個測試方法以後不久運行。 @BeforeMethod 註釋方法將在每一個測試方法以前運行。 @AfterMethod 註釋方法將在每一個測試方法以後運行。 @Parameters 描述如何將參數傳遞給@Test方法。 @DataProvider 標記一種方法來提供測試方法的數據。 註釋方法必須返回一個Object [] [],其中每一個Object []能夠被分配給測試方法的參數列表。 要從該DataProvider接收數據的@Test方法須要使用與此註釋名稱相等的dataProvider名稱。 @Factory 將一個方法標記爲工廠,返回TestNG將被用做測試類的對象。 該方法必須返回Object []。 @Listeners 定義測試類上的偵聽器。 @Test 將類或方法標記爲測試的一部分。
例:
一、增長一個測試類TestConfig
package study.testng; import org.testng.annotations.AfterGroups; import org.testng.annotations.AfterMethod; import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; public class TestConfig { @BeforeSuite public void beforeSuite() { System.out.println("測試套件(當前xml中<suite>標籤)以前運行@BeforeSuite--------------------"); } @AfterSuite public void afterSuite() { System.out.println("測試套件(當前xml中<suite>標籤)以後運行@AfterSuite--------------------\n"); } @BeforeTest public void beforeTest() { System.out.println("測試用例(當前xml中<test>標籤)以前運行@BeforeTest----------"); } @AfterTest public void afterTest() { System.out.println("測試用例(當前xml中<test>標籤)以後運行@AfterTest----------\n"); } @BeforeMethod public void beforeMethod() { System.out.println("當前類每一個測試方法(@Test)以前運行@BeforeMethod"); } @AfterMethod public void AfterMethod(){ System.out.println("當前類每一個測試方法(@Test)以後運行@AfterMethod"); } @BeforeGroups(value="group1") public void beforeGroups(){ System.out.println("配置組配group1以前運行@BeforeGroups.................."); } @AfterGroups(value="group1") public void afterGroups(){ System.out.println("配置組配group1以前運行@AfterGroups.................."); } @Test public void test1(){ System.out.println("runnig TestConfig.test1()"); } @Test(groups = "group1") public void test2(){ System.out.println("runnig TestConfig.test2()"); } @Test(groups = "group1") public void test3(){ System.out.println("runnig TestConfig.test3()"); } }
二、新建一個自定義xml配置文件tester.xml(位置在哪都行)
<?xml version="1.0" encoding="UTF-8"?> <!-- @BeforeSuite --> <suite name="tester"> <!-- @BeforeTest --> <test name="case1"> <classes> <class name="study.testng.TestConfig" /> <class name="study.testng.TestHelloWorld" /> </classes> </test> <!-- @AfterTest --> <!-- @BeforeTest --> <test name="case2"> <classes> <class name="study.testng.TestConfig" /> </classes> </test> <!-- @AfterTest --> </suite> <!-- @AfterSuite -->
三、運行此配置測試
在有@Test方法的類頁面右鍵
選擇xml文件
四、運行結果
從這個結果顯示出註釋的做用位置。其中@BeforeGroups和@AfterGroups的做用範圍是能夠跨類的,但類必須是在同一個測試用例(<test>標籤)範圍內;
這些標籤的運行前後順序能夠總結爲:
@BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass->@AfterTest->@AfterSuite(其中{}內的與多少個@Test,就循環執行多少次)。
<suite> 套件,根標籤,一般由幾個<test組成> 屬性: name 套件的名稱,必須屬性; verbose 運行的級別或詳細程度; parallel 是否運行多線程來運行這個套件; thread-count 若是啓用多線程,用於指定開戶的線程數; annotations 在測試中使用的註釋類型; time-out 在本測試中的全部測試方法上使用的默認超時時間; <test> 測試用例,name爲必須屬性; <classes> 用例中包含的類,子標籤爲<class name=」className」>; <class> 測試類,其中屬性name爲必須屬性;; <packages> 用例中包含的包,包中全部的方法都會執行,子標籤爲<package name=」packageName」>; <package> 測試包,name爲必須屬性; <methods> 指定測試類中包含或排除的方法,子類爲<include>,<exclude>; <include> 指定須要測試的方法,name爲必須屬性; <exclude> 指定類中不須要測試的方法,name爲必須屬性; <groups> 指定測試用例中要運行或排除運行的分組,子標籤爲<run>,<run>下包含<include>,<exclude>標籤,<include>,<exclude>的name指定運行、不運行的分組;
例:
<?xml version="1.0" encoding="UTF-8"?> <suite name="tester"> <test name="case1"> <classes> <class name="study.testng.TestHelloWorld"/> <class name="study.testng.TestConfig"> <methods> <include name="test1" /> <!-- 運行test1()方法--> <exclude name="test2" /> <!-- 不運行test2()方法--> </methods> </class> </classes> </test> <test name="case2"> <packages> <package name="study.testng" /> </packages> <groups> <run> <exclude name="group1" /> <!-- 不運行組group1的全部方法,或者也可include標籤來表示須要行的組--> </run> </groups> </test> </suite>
一、使用@Parameters註解從測試配置xml文件獲取參數
(1)新建測試類TestParameter
package study.testng; import org.testng.annotations.Parameters; import org.testng.annotations.Test; public class TestParameter { @Test @Parameters({"param", "param2"}) public void printParameters(String param, String param2){ System.out.println("param參數值爲 : " + param); System.out.println("param2參數值爲 : " + param2); } }
(2)新建配置xml文件,parameterTeser.xml
<?xml version="1.0" encoding="UTF-8"?> <suite name="parameterTester"> <test name="case1"> <parameter name="param" value="param的值"/> <parameter name="param2" value="param2的值"/> <classes> <class name="study.testng.TestParameter" /> </classes> </test> </suite>
(3)以這個配置文件運行測試
(4)結果
二、使用@DataProvider傳送參數,@DataProvider能夠傳遞一些比較複雜的參數
(1)新建一個測試類TestParameter2
package study.testng; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class TestParameter2 { @DataProvider(name="user") public Object[][] Users(){ return new Object[][]{ {"root","passowrd"}, {"cnblogs.com", "tankxiao"}, {"tank","xiao"} }; } @Test(dataProvider="user") public void verifyUser(String userName, String password){ System.out.println("Username: "+ userName + " Password: "+ password); } }
(2)點擊運行獲得以下結果
默認的測試報告位於當前項目文件夾下的test-output文件夾內,index.html即爲總的測試報告(html文件用瀏覽器打開),tester文件夾下是按測試用例生成的報告,old文件夾下爲歷史報告。
附:與Junit4的異同比較,可直接參考使用Junit4;