TestNG methods can have arguments also. There are two ways we can inject method arguments:
TestNG方法也可以有参数。 我们可以通过两种方式注入方法参数:
- Using
@DataProvider
annotation – I have explained about it in TestNG DataProvider tutorial. 使用@DataProvider
批注–我已经在TestNG DataProvider教程中对此进行了解释。 - Using
@Parameters
annotation – this annotation allows us to inject parameters from TestNG XML suite file. We will focus on this annotation in this tutorial and learn how to use it. 使用@Parameters
批注–通过此批注,我们可以从TestNG XML套件文件中注入参数。 在本教程中,我们将重点关注此注释并学习如何使用它。
TestNG参数注释 (TestNG Parameters Annotation)
Some important points about @Parameters annotation are:
有关@Parameters批注的一些重要点是:
- TestNG @Parameters annotation can be applied on
@Before
,@After
and@Test
methods. TestNG的@参数注解可以被应用@Before
,@After
和@Test
方法。 - If we want to keep changing our test method inputs, this is the preferred way because we don’t need to compile our test classes again. 如果我们想继续更改测试方法的输入,这是首选方法,因为我们不需要再次编译测试类。
- @Parameters annotation requires us to provide string array of parameter names to be looked in the test suite xml file. The number of parameters should match the number of arguments in the method. @Parameters批注要求我们提供要在测试套件xml文件中查找的参数名称的字符串数组。 参数的数量应与方法中的参数的数量匹配。
- We can use
@Optional
with method argument to provide a default value, when the parameter is missing from test suite xml file. 当测试套件xml文件中缺少参数时,可以将@Optional
与method参数一起使用以提供默认值。
Parameters in TestNG suite XML file can be defined at suite level or test level. If there are parameters with the same name, then test parameters take precedence and override the value.
可以在套件级别或测试级别定义TestNG套件XML文件中的参数。 如果存在具有相同名称的参数,则测试参数具有优先级并覆盖该值。
TestNG参数示例 (TestNG Parameters Example)
Now that we have gone through the theory part, let’s look into a complete example of TestNG test class where we will use @Parameters annotation.
现在,我们已经完成了理论部分,我们来看一个完整的TestNG测试类示例,在该示例中将使用@Parameters批注。
package com.journaldev.parameters;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParameters {
@Test
@Parameters("arguments")
public void singleParameterTest(String s) {
System.out.println("Testing for input parameter = " + s);
}
@Test
@Parameters({ "id", "name" })
public void multipleParameterTest(int id, String s) {
System.out.println("Testing for multiple input parameter = " + id + " and " + s);
}
@BeforeSuite
@Parameters("before_suite")
public void beforeSuite(String s) {
System.out.println("Before Suite Parameter = " + s);
}
@AfterSuite
@Parameters("after_suite")
public void afterSuite(@Optional("Default Parameter") String s) {
System.out.println("After Suite Parameter = " + s);
}
}
Here is the test suite xml file:
这是测试套件的xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Default suite" guice-stage="DEVELOPMENT">
<parameter name="before_suite" value="My Before Suite Parameter"></parameter>
<parameter name="id" value="100"></parameter> <!-- Suite Level Parameters -->
<test thread-count="5" name="Default test" verbose="2">
<parameter name="arguments" value="Pankaj"></parameter>
<parameter name="name" value="JournalDev"></parameter> <!-- Test Level Parameters -->
<classes>
<class name="com.journaldev.parameters.TestParameters" />
</classes>
</test>
</suite>
TestNG参数XML Suite测试 (TestNG Parameters XML Suite Test)
If you are using Eclipse TestNG plugin, then you can run the test suite XML file directly as shown in below image.
如果您使用的是Eclipse TestNG插件,则可以直接运行测试套件XML文件,如下图所示。
We will get following output in eclipse console.
我们将在eclipse控制台中获得以下输出。
[RemoteTestNG] detected TestNG version 6.14.3
Before Suite Parameter = My Before Suite Parameter
Testing for multiple input parameter = 100 and JournalDev
Testing for input parameter = Pankaj
PASSED: multipleParameterTest(100, "JournalDev")
PASSED: singleParameterTest("Pankaj")
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
After Suite Parameter = Default Parameter
===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
We can also run the test suite XML file from command line through org.testng.TestNG
class. All your test classes and TestNG jars should be in the classpath.
我们还可以通过org.testng.TestNG
类从命令行运行测试套件XML文件。 您所有的测试类和TestNG jar都应该在类路径中。
$java -cp .:../../lib/testng-6.14.3.jar:../../lib/jcommander-1.72.jar:../../lib/bsh-2.0b6.jar org.testng.TestNG test_parameters.xml
That’s all for TestNG Parameters example.
这就是TestNG Parameters示例的全部内容。
翻译自: https://www.journaldev.com/21289/testng-parameters-dataprovider-xml