TestNG參數化測試-@DataProvider
- 2020 年 2 月 14 日
- 筆記
參數化@DataProvider
欲將沉醉換悲涼,清歌莫斷腸。這混亂的塵世,究竟充斥了多少絕望和悲傷。你想去做一個勇敢的男子,為愛,為信仰,轟轟烈烈的奮鬥一場。
上期TestNG使用xml方式傳遞, 廣大粉友私信我, 再出一期簡介兩者的應用場景.應粉友的要求,本期分享@DataProvider 方式在TestNG中傳遞參數.
那就還是使用上期的項目示例:
一. 使用@DataProvider 傳遞 int 參數.
1.創建java類:TestParameterDataProvider1
public class TestParameterDataProvider1 {
@Test(dataProvider = "provideNumbers") public void test1(int actual, int expected){ Assert.assertEquals(actual+10,expected); } @DataProvider(name="provideNumbers") public Object[][] provideData(){ return new Object[][]{{10,20},{100,110},{200,210}}; } }
2.配置testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd"> <suite name="test-parameter" verbose="1"> <test name="example1" preserve-order="true"> <classes> <class name="TestParameterDataProvider1"/> </classes> </test> <!--<test name="example2">--> <!--<groups>--> <!--<run>--> <!--<include name="groupA"/>--> <!--<exclude name="groupB"/>--> <!--</run>--> <!--</groups>--> <!--<classes>--> <!--<class name="TestParameterDataProvider2"/>--> <!--</classes>--> <!--</test>-->
<test name="example3" preserve-order="true"> <classes> <class name="TestParameterDataProvider3"/> </classes> </test>
</suite>
3. 運行看下結果
/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java
actual==>10
expected==>20
actual==>100
expected==>110
actual==>200
expected==>210
===============================================
test-parameter
Total tests run: 3, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
二. 使用@DataProvider 傳遞 ITestContext 對象
1.創建java類:TestParameterDataProvider2
public class TestParameterDataProvider2 { @Test(dataProvider = "dataProvider", groups = {"groupA"}) public void test1(int number) { Assert.assertEquals(number, 1); } @Test(dataProvider = "dataProvider", groups = {"groupB"}) public void test2(int number) { Assert.assertEquals(number, 2); } @DataProvider(name = "dataProvider") public Object[][] provideData(ITestContext context) { Object[][] result = null; for (String group : context.getIncludedGroups()) { System.out.println("group==>>" + group); if ("groupA".equals(group)) { result = new Object[][] {{1}}; System.out.println("===groupA==="); }else if ("groupB".equals(group)){ result=new Object[][]{{2}}; System.out.println("===groupB==="); } } if (result == null) { result = new Object[][] {{3}}; System.out.println("===什麼都不是==="); } return result; } }
2.配置testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd"> <suite name="test-parameter" verbose="1"> <test name="example2"> <groups> <run> <include name="groupA"/> <exclude name="groupB"/> </run> </groups> <classes> <class name="TestParameterDataProvider2"/> </classes> </test> </suite>
3. 運行看下結果
/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java
group==>>groupA
===groupA===
===============================================
test-parameter
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
三.使用@DataProvider 傳遞 Method
1.創建java類:TestParameterDataProvider3
public class TestParameterDataProvider3 { @Test(dataProvider = "dataProvider") public void test1(int actual, int expected){ Assert.assertEquals(actual,expected); System.out.println("actual="+actual+" expected="+ expected); } @Test(dataProvider = "dataProvider") public void test2(String actual, String expected){ Assert.assertEquals(actual,expected); System.out.println("actual="+actual+" expected="+ expected); } @DataProvider(name = "dataProvider") public Object[][] provideData(Method method){ Object[][]result=null; if (method.getName().equals("test1")) { result = new Object[][] { { 1, 1 }, { 200, 200 } }; } else if (method.getName().equals("test2")) { result = new Object[][] { { "實際結果1", "實際結果1" }, { "實際結果2", "實際結果2" } }; } System.out.println("result="+result); return result; }
2.配置testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPEsuiteSYSTEM"http://testng.org/testng-1.0.dtd"> <suite name="test-parameter" verbose="1"> <test name="example3" preserve-order="true"> <classes> <class name="TestParameterDataProvider3"/> </classes> </test> </suite>
3. 運行看下結果
/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java
actual=1 expected=1
actual=200 expected=200
result=[[Ljava.lang.Object;@623f5254
actual=實際結果1 expected=實際結果1
actual=實際結果2 expected=實際結果2
===============================================
test-parameter
Total tests run: 4, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
今天分享就到這裡, 方式還有很多, 大家根據個人使用場景,合適的才是最好的.