Parameterized TestNG

TestNG supports parametric testing, one is parameterized via xml file, and the second is the parameter of DataProvider

1️⃣ by xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="parmsuitename">
    <test name="parm">
        <!--参数-->
        <parameter name="name" value="Jack"></parameter>
        <parameter name="age" value="25"></parameter>
        <!- class<>classes<->Specifies the class of operation
        
            name="com.course.testng.parm.ParamterTest"></class>
        </classes>
    </test>
</suite>

Class Code: requires two notes, one @Test, marked a test class; the other is @Parameters, says the incoming parameters

package com.course.testng.parm;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParamterTest {
    @Test
    @Parameters({"age","name"})
    public void paramTest1(int age, String name) {
        System.out.println(age + name);
    }
}

2️⃣ by DataProvider: Object [] []

First, the value is directly passed to the method

package com.course.testng.parm;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderTest {
    @Test(dataProvider = "dataDemo")
    public void paramTestByDataProvider(String name, int age) {
        System.out.println("年龄是:" + age + ",姓名是:" + name);
    }

    @DataProvider(name = "dataDemo")
    public Object[][] providerData() {
        Object[][] objects = new Object[][]{
                {"Jack", 25},
                {"Rose", 26}
        };
        return objects;
    }
}

operation result:

Age: 25, names are: Jack 
age: 26, names are: Rose 

================================ =============== 
the Default Suite 
Total Tests RUN: 2, Failures: 0, Skips: 0 
==================== ===========================  

Second, names by different methods, different parameter values ​​passed

    @Test(dataProvider = "methodDemo")
    public void methodTest1(String name, int age) {
        System.out.println("Iterator1的年龄是:" + age + ",姓名是:" + name);
    }

    @Test(dataProvider = "methodDemo")
    public void methodTest2(String name, int age) {
        System.out.println("Iterator222年龄是:" + age + ",姓名是:" + name);
    }

    @DataProvider(name = "methodDemo")
    public Object[][] methodDataTest(Method method) { //The method must write parameters (Method method), the package is introduced: the java.lang.reflect.Method 
        Object [] [] Objects = null ;
         IF (method.getName () the equals ( "methodTest1." )) {// depending on the method name, a different transmission parameter values 
            Objects = new new Object [] [] { 
                    { "John Doe it", 23 }, 
                    { "John Doe Yes", 12 is } 
            }; 
        } the else  IF (method.getName () .equals ( "methodTest2" )) { 
            Objects = new new Object [] [] { 
                    { "Gaga", 36 }, 
                    {"justin", 14}
            };
        }
        return objects;
    }

According to the method name, pass different parameters

 There is also a DataProvider by the method returns an iterator, next time supplement

Guess you like

Origin www.cnblogs.com/starstarstar/p/11317864.html
Recommended