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

今天分享就到这里, 方式还有很多, 大家根据个人使用场景,合适的才是最好的.