TestNG参数化测试

 @Test
 @Parameters({ "dbconfig", "datasize" }) 
 
public void dbTest(String dbconfig, int datasize) {
}
<?xml version="1.0" encoding="UTF-8"?>
<suite name="test-parameter">

    <test name="test1">

        <parameter name="dbconfig" value="x.properties" />
        <parameter name="datasize" value="10" />

        <classes>
            <class name="com.qa.TestParametersInXML" />
        </classes>

    </test>

</suite>

 

Use Data Provider in test case class:

public class TestDataProvider {

    @Test(dataProvider = "myData")
    public void test(int number, int expected) {
        Assert.assertEquals(number + 10, expected);
    }

    @DataProvider(name = "myData")
    public Object[][] provideData() {

        return new Object[][] { { 20, 20 }, { 300, 110 }, { 400, 210 } };
    }

}

 

Use Data Provider in a different class:

  @Test(description = "xxxx", groups = {"regression", "search"}, 
          dataProvider = "FilterSearchData", dataProviderClass = TestDataProvider.class)
  public void TC009_FilterSearch(Map<String, String> filderConditions) throws Exception {
      
 ....... 
       
  }  
public class TestDataProvider {

      //description = "This method is responsible for test data reading, supplies data for TC009_applyFilterSearch."
      @DataProvider(name = "FilterSearchData")
      public static Object[][] TC009_FilterSearchData() {
            
          //Read test data.      
          String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
          String testDataPath = Constant.testDataPath + methodName + ".xlsx";
          
          return readTestData(testDataPath, "FilterSearch");
          
      }
      
}

 

posted on 2018-06-12 11:42  alicegu  阅读(163)  评论(0编辑  收藏  举报