我们可以在testng.xml中的方法级别传递参数或任何属性吗?

时间:2017-08-23 06:11:59

标签: selenium testing appium

// Testng xml

<test name="Test1" preserve-order ="true">
        <parameter name="deviceName_" value="aaaa"/>
<classes>
        <class name="Test">
                <methods>
<include name="methodName"/>

            </methods>  
            </class>  
        </classes>
</test> 

//我可以在方法级别传递参数,例如我在测试级别传递了deviceName

1 个答案:

答案 0 :(得分:2)

实际上,存在方法级别的参数。这是一个例子:

<suite name="my-suite" verbose="1">
    <test name="my-test">
        <classes>
            <class name="testng.ex1.TestParams">
                <methods>
                    <include name="m1">
                        <parameter name="key1"  value="val1"/>
                        <parameter name="key2"  value="val2"/>
                    </include>
                    <include name="m2">
                        <parameter name="key1"  value="valA"/>
                        <parameter name="key2"  value="valB"/>
                    </include>
                </methods>
            </class>
        </classes>
    </test>
</suite>

,测试类是,

package testng.ex1;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestParams {

    @Test
    @Parameters({ "key1", "key2" })
    public void m1(String key1, String key2) throws Exception {
        System.out.println(key1 + ", " + key2);
    }

    @Test
    @Parameters({ "key1", "key2" })
    public void m2(String key1, String key2) throws Exception {
        System.out.println(key1 + ", " + key2);
    }
}

另一种方法是使用从testng.xml获取密钥的数据提供程序。见例:

<suite name="my-suite" verbose="1">
    <test name="my-test">
        <classes>
            <parameter name="keys" value="key1,key2,key3,key4" />
            <class name="testng.ex2.TestParams" />
        </classes>
    </test>
</suite>

测试calss,

package testng.ex2;

import java.util.Arrays;
import java.util.List;

import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class TestParams {

    @Test(dataProvider = "dp")
    public void m1(Employee e) throws Exception {
        System.out.println("name: " + e.getName() + ", age: " + e.getAge());
    }

    @DataProvider(name = "dp")
    @Parameters("keys")
    public Object[][] createData(ITestContext ctx) {
        String keysString = ctx.getCurrentXmlTest().getLocalParameters().get("keys");
        List<String> keys = Arrays.asList(keysString.split(","));

        Object[][] result = new Object[keys.size()][1];
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            result[i] = new Object[] { new Employee(key) };
        }
        return result;
    }
}

员工类

package testng.ex2;

public class Employee {

    private final String name;
    private final int age;

    public Employee(String key) {
        // use 'key' to lookup employee in database
        name = key + "_name"; // dummy value
        age = 41; // dummy value
    }

    String getName() {
        return name;
    }

    int getAge() {
        return age;
    }
}

您可以在before方法中获取参数,如下所示,

@BeforeMethod
@Parameters({ "key1", "key2" })
public void beforem1(String key1, String key2){
    System.out.println(key1 + ", " + key2);
}