TestNG是一個設計用來簡化普遍的測試需求的測試框架,從單元測試到集成測試,這個是TestNG設計的出發點,不單單是單元測試,並且能夠用於集成測試。設計目標的不一樣,對比junit的只適合用於單元測試,TestNG無疑走的更遠。能夠用於集成測試,這個特性是我選擇TestNG的最重要的緣由。html
先了解這兩個註解的意思java
@DataProvider 標記一個方法用於爲測試方法提供數據。被註釋的方法必須返回Object[][], 其中每一個Object[]能夠指派爲這個測試方法的參數列表。框架
從這個DataProvider接收數據@Test方法須要使用一個dataProvider名稱等於這個註解的名字。ide
@Factory 標記方法做爲一個返回對象的工廠,這些對象將被TestNG用於做爲測試類。這個方法必須返回Object[]。單元測試
文件結構學習
├── src/main/java/test/demo/TestFactory.java
├── src/main/java/test/demo/LoginPageTest.java
│── src/main/java/test/demo/HomePageTest.java
└── src/test/resources/smokeTest.xml
1 package test.demo; 2 3 import org.testng.annotations.DataProvider; 4 import org.testng.annotations.Factory; 5 6 /** 7 * @Description: Demo 8 * @author wadexu 9 * 10 * @updateUser 11 * @updateDate 12 */ 13 public class TestFactory { 14 15 @DataProvider(name = "datasource") 16 public Object[][] datasource() { 17 18 // Creating and filling result array 19 Object[][] credentials = new Object[2][2]; 20 21 credentials[0][0] = "User1"; 22 credentials[0][1] = "WadeXu"; 23 24 credentials[1][0] = "User2"; 25 credentials[1][1] = "JamesBond"; 26 27 return credentials; 28 } 29 30 @Factory(dataProvider = "datasource") 31 public Object[] createTest(String userID, String userName) { 32 33 Object[] tests = new Object[2]; 34 35 tests[0] = new HomePageTest(userID, userName); 36 tests[1] = new LoginPageTest(userID, userName); 37 38 return tests; 39 } 40 41 42 }
TestFactory工廠類,在帶有@Factory註解的方法中調用被執行的測試類,TestNg會自動調用被執行類中帶有@Test註解的方法,被執行的測試類爲:HomePageTest , LoginPageTest測試
使用@DataProvider做爲數據注入的方式,向Factory中注入數據,並執行測試。ui
##轉載註明出處:http://www.cnblogs.com/wade-xu/p/4761275.html this
1 package test.demo; 2 3 import org.testng.annotations.Test; 4 5 /** 6 * @Description: Demo Login Page 7 * @author wadexu 8 * 9 * @updateUser 10 * @updateDate 11 */ 12 public class LoginPageTest { 13 14 private String userID; 15 private String userName; 16 17 public LoginPageTest(String userID, String userName) { 18 this.userID = userID; 19 this.userName = userName; 20 } 21 22 @Test 23 public void testLogin() { 24 // for demo purpose only 25 System.out.println(userID + " - " + userName + " is testing Login page"); 26 } 27 28 }
1 package test.demo; 2 3 import org.testng.annotations.Test; 4 5 /** 6 * @Description: Demo Home Page 7 * @author wadexu 8 * 9 * @updateUser 10 * @updateDate 11 */ 12 public class HomePageTest { 13 14 private String userID; 15 private String userName; 16 17 public HomePageTest(String userID, String userName) { 18 this.userID = userID; 19 this.userName = userName; 20 } 21 22 23 @Test 24 public void testHomePage() { 25 System.out.println(userID + " - " + userName + " is testing Home page"); 26 } 27 28 }
##轉載註明出處:http://www.cnblogs.com/wade-xu/p/4761275.html spa
TestNG xml配置文件以下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!--<suite name="DMP_Test_Suite" -->
<suite name="Demo_Test_Suite" parallel="false">
<test name="Demo_Test">
<classes>
<class name="test.demo.TestFactory" />
</classes>
</test>
</suite>
右擊xml文件Run as TestNG
Test Result:
Console output:
User1 - WadeXu is testing Home page User2 - JamesBond is testing Home page User1 - WadeXu is testing Login page User2 - JamesBond is testing Login page
注意測試方法將被一共執行的次數,@DataProvider屬於循環測試的類型。
TestNG更多功能請參考官方站點
感謝閱讀,若是您以爲本文的內容對您的學習有所幫助,您能夠點擊右下方的推薦按鈕,您的鼓勵是我創做的動力。