TestNG DataProvider usage in a

Why use DataProvider

  1. DataProvider can achieve similar Factory data-driven testing, and the use DataProvider when the class is where the test method is created only once, but the method is called multiple times, so the effort when comparing Province
  2. Dangerous DataProvider can also implement lazy loading, when needed to test a large amount of data can be a load data without the need for a one-time load data into memory, avoiding the waste of memory, and memory is not enough of a possible

DataProvider conventional usage

  1. The definition of a function, the function name free to take, use @DataProvider a comment to this provider by name to a name, the function returns a two-dimensional array
  2. Each row of the two-dimensional array represents the parameter function returns one test, the elements in each row with the desired parameters correspond Test (array type is usually the case Object, but can be replaced with other custom type)
  3. Test notes the need to use the provider's method, using the provider attribute points above DataProvider

example

public class TestDataProvider {

        @DataProvider(name="provider")
    public Object[][] provider(){
        Object [][] provider = new Object [5][2];
        for (int i = 0; i < provider.length; i++) {
            provider[i][0] = "name"+i;
            provider[i][1] = i+10;
        }
        
        return provider;
    }
    
    @Test(dataProvider="provider")
    public void getName(String name,int age){
        System.out.println(name+"_"+age);
        
    }
    
}

Run this Test, Output

[TestNG] Running:
  C:\Users\Administrator\AppData\Local\Temp\testng-eclipse-1631420346\testng-customsuite.xml

name0_10
name1_11
name2_12
name3_13
name4_14
PASSED: getName("name0", 10)
PASSED: getName("name1", 11)
PASSED: getName("name2", 12)
PASSED: getName("name3", 13)
PASSED: getName("name4", 14)

===============================================
    Default test
    Tests run: 5, Failures: 0, Skips: 0
===============================================

This test method can be seen running five times, each time an array regarded as a DataProvider provide their input parameters and then perform the test.

Method parameter with DataProvider

If you want to use different DataProvider different dataprovider Test methods, you can use the Method parameter in a DataProvider

@Test(dataProvider = "provider")
public void getFirst(String name, int age) {
    System.out.println("第一组"+name);
}

@Test(dataProvider = "provider")
public void getSecond(String name, int age) {
    System.out.println("第二组 " + name);
}

@DataProvider(name = "provider")
public Object[][] provider(Method method) {
    Object[][] objects;
    if (method.getName().equals("getFirst")) {              //如果调用该DataProvider的函数是getFirst,那么就返回这个数组
        objects = new Object[][] { { "cq1", 20 }, { "cq2", 22 } };
    } else if (method.getName().equals("getSecond")) {//如果调用该DataProvider的函数是getSecond,那么就返回这个数组
        objects = new Object[][] { { "cq3", 20 }, { "cq4", 22 } };
    } else {                                                                    //如果调用该DataProvider的函数不是getFirst也不是getSecond,那么就返回这个数组
        objects = new Object[][] { {"cq5",33}, {"cq6",34} };
    }

    return objects;
}        

According to the function name calling here DataProvider returns different parameters.
Since there is no designated running order, so the Test will perform in alphabetical order, the first execution getFirst, then getSecond.
It found that two parameters, a first set of parameters into the first pass performed again, and then passed into a second set of parameters when executed again performed getFirst.
The same sequence is performed getSecond, so that the final order of execution
①getFirst first set of input parameters
②getFirst input of a second set of parameters
③getSecond input a first set of parameters
④getSecond enter the second set of parameters

With the DataProvider ITestContext

In Dataprovider can ITestContext with a parameter, the context can get some information from the execution of the method parameters, such that the packet can be obtained context.getIncludeGroups tests performed, so that the test can be performed in a different packet classes pass different parameters of.

Other locations of DataProvider

Use the dataProvider property in @Test tab can have a dataProviderClass specify additional attribute that can define the class dataProvider function, if you do not specify this property will be located in @Test class looking dataProvider, if you specify will be in specified class looking dataProvider. DataProvider specified class must be a static method.

Tips: dataProvider method after the call, followed by the method name in TestNG plugin or report will show parameter values, so that when a problem occurs after running may cause the program to know which set of parameters appeared abnormal.

Guess you like

Origin www.cnblogs.com/beifucangqiong/p/11246988.html