testng.xml 中参数的多个值(不使用 dataProvider) [英] Multiple values for parameters in testng.xml (without using dataProvider)
问题描述
我想用不同的参数值多次运行我的测试用例.是否可以使用 testng.xml 和 @Parameters 注释?
I want to run my test case multiple times with different values of parameters. Is it possible using testng.xml and @Parameters annotation?
例如.
<test name="Login Tests">
<parameter name="one" />
<parameter name="two" />
<classes>
<class name="test.java.Login"/>
</classes>
</test>
因此,这应该运行两次测试,一次使用值 1,然后使用值 2.
So, this should run the test two times, once with value one and then with value two.
是否可以使用 testng.xml 和 @Parameter?
Is it possible using testng.xml and @Parameter?
第 2 季度.另外,是否可以只为套件中的特定@Test 添加参数
Q2. Also, is it possible to add parameters for only particular @Test in a suite
例如.我的 TestSuite 有 2 个测试用例和一个与之关联的 testng.xml.
Eg. My TestSuite has 2 test cases and one testng.xml, associated to it.
是否可以只为一个@Test 在testng.xml 中添加@Parameters,因为我的两个测试都采用相同的参数.
Is it possible to add @Parameters in testng.xml for only one @Test, since both my tests are taking same parameters.
推荐答案
以下示例应该基本上可以帮助您回答所有问题.
The below sample should basically help answer all your questions.
如何根据通过
标签提供的值多次运行 @Test
How to run a @Test
multiple times based on the values provided via the <parameters>
tag
如何仅将参数传递给特定的测试类
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class FirstTestClass {
@Test(dataProvider = "getData")
public void testMethod(String param) {
System.out.println("Name = " + param);
}
@DataProvider
public Object[][] getData(ITestContext context) {
String parameter = context.getCurrentXmlTest().getLocalParameters().get("names");
String[] names = parameter.split(",");
Object[][] returnValues = new Object[names.length][1];
int index = 0;
for (Object[] each : returnValues) {
each[0] = names[index++].trim();
}
return returnValues;
}
}
这里我们将通过 testng.xml
文件传递的单个参数解析为多个值,方法是使用 ,
Here we are parsing a single parameter that was passed via the testng.xml
file into multiple values by splitting them using ,
这是第二个测试类的样子,它将接收一个特定于测试类的参数.
Here's how the second test class would look like, which is going to receive a test class specific parameter.
public class SecondTestClass {
@Test
@Parameters({"age"})
public void testMethod(int age) {
System.out.println("Age = " + age );
}
}
最后,这里是 testng.xml
的样子:
Finally, here's how the testng.xml
would look like :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="45160355_Suite" parallel="false" verbose="2" >
<test name="45160355_test" verbose="2">
<parameter name="names" value="Cedric, Julien"/>
<classes>
<class name="com.rationaleemotions.stackoverflow.qn45160355.FirstTestClass">
</class>
<class name="com.rationaleemotions.stackoverflow.qn45160355.SecondTestClass">
<parameter name="age" value="15"/>
</class>
</classes>
</test>
</suite>
这是输出
... TestNG 6.11 by Cédric Beust (cedric@beust.com)
...
{names=Cedric, Julien}
Name = Cedric
Name = Julien
Age = 15
===============================================
45160355_Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
这篇关于testng.xml 中参数的多个值(不使用 dataProvider)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!