testng xml
TestNG XML file allows us to configure a test suite and execute it from the command line or ant script.
TestNG XML文件使我们可以配置测试套件,并从命令行或ant脚本执行它。
When we execute a TestNG test through Eclipse or through maven build, HTML reports are generated. These HTML reports also create a test suite XML file that we can use to execute the same test through the command line. If you are new to TestNG, please go through TestNG Tutorial.
当我们通过Eclipse或通过Maven构建执行TestNG测试时,将生成HTML报告。 这些HTML报告还创建了一个测试套件XML文件,我们可以使用该文件通过命令行执行相同的测试。 如果您不熟悉TestNG,请阅读TestNG教程 。
TestNG XML (TestNG XML)
TestNG XML suite are based on following DTD: https://testng.org/testng-1.0.dtd
TestNG XML套件基于以下DTD: https://testng.org/testng-1.0.dtd
: https://testng.org/testng-1.0.dtd
Before we look into some TestNG XML examples, let’s create a simple class and corresponding TestNG test class.
在研究一些TestNG XML示例之前,让我们创建一个简单的类和相应的TestNG测试类。
package com.journaldev.xml;
public class Utils {
public static String NAME = "";
public int add(int x, int y) {
return x + y;
}
public static void setName(String s) {
System.out.println("Setting NAME to " + s);
NAME = s;
}
}
package com.journaldev.xml;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestNGXMLTest {
@Test(dataProvider = "dp", priority=1)
public void test_add(Integer x, Integer y) {
Utils u = new Utils();
Assert.assertEquals(u.add(x, y), x + y);
}
@Test(dataProvider = "dpName", priority=3, groups="setName")
public void test_setName(String s) {
Utils.setName(s);
Assert.assertEquals(Utils.NAME, s);
}
@Test
@Parameters("name")
public void test_name(@Optional("NA") String s) {
System.out.println("Input parameter = "+s);
}
@DataProvider
public Object[][] dp() {
return new Object[][] { new Object[] { 1, 1 }, new Object[] { 2, 2 }, };
}
@DataProvider
public Object[][] dpName() {
return new Object[][] { new Object[] { "Utils" }, new Object[] { "MyUtils" }, };
}
}
TestNG XML示例 (TestNG XML Example)
Let’s create a very simple TestNG XML test suite file.
让我们创建一个非常简单的TestNG XML测试套件文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNGXMLTest Test Suite" guice-stage="DEVELOPMENT" parallel="classes">
<test thread-count="5" name="TestNGXMLTest Test" verbose="2">
<classes>
<class name="com.journaldev.xml.TestNGXMLTest" />
</classes>
</test>
</suite>
Above TestNG XML file is very easy to understand. We have created a test suite and defined the class to execute.
上面的TestNG XML文件非常容易理解。 我们创建了一个测试套件,并定义了要执行的类。
parallel
attribute tells TestNG to run the test suite classes in parallel, thread-count
is used to define the number of maximum threads to use when running tests in parallel.
parallel
属性告诉TestNG parallel
运行测试套件类, thread-count
用于定义并行运行测试时使用的最大线程数。
verbose
is used to define the amount of logging to be performed on the console.
verbose
用于定义要在控制台上执行的日志记录数量。
When we execute above test suite XML file, we get following output.
当我们执行上述测试套件XML文件时,将得到以下输出。
[RemoteTestNG] detected TestNG version 6.14.3
Input parameter = NA
Setting NAME to Utils
Setting NAME to MyUtils
PASSED: test_name("NA")
PASSED: test_add(1, 1)
PASSED: test_add(2, 2)
PASSED: test_setName("Utils")
PASSED: test_setName("MyUtils")
===============================================
TestNGXMLTest Test
Tests run: 5, Failures: 0, Skips: 0
===============================================
===============================================
TestNGXMLTest Test Suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================
Below image shows how to run above TestNG XML suite file through command line.
下图显示了如何通过命令行在TestNG XML套件文件上运行。
We can also run it directly using Eclipse TestNG plugin.
我们还可以使用Eclipse TestNG插件直接运行它。
TestNG XML Suite软件包 (TestNG XML Suite Packages)
We can also define the packages to look for TestNG test classes. It’s handy when we have many test classes to execute.
我们还可以定义包以查找TestNG测试类。 当我们有许多要执行的测试类时,这很方便。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG Packages Test Suite" guice-stage="DEVELOPMENT">
<test thread-count="5" name="TestNG Packages Test" verbose="2">
<packages>
<package name="com.journaldev.xml"></package>
</packages>
</test>
</suite>
Since I have only single TestNG test class in the package, the output will be same as earlier test.
由于程序包中只有一个TestNG测试类,因此输出将与早期测试相同。
TestNG XML组 (TestNG XML Groups)
We can specify which groups to include in test and which ones to exclude in XML test suite file.
我们可以指定要包含在测试中的组,以及要排除在XML测试套件文件中的组。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG Groups Test Suite" guice-stage="DEVELOPMENT">
<test thread-count="5" name="TestNG Groups Test" verbose="2">
<groups>
<run>
<include name="setName"></include>
</run>
</groups>
<classes>
<class name="com.journaldev.xml.TestNGXMLTest">
</class>
</classes>
</test>
</suite>
Above test suite will only execute methods in setName
group, so only test_setName
method will be executed.
上面的测试套件将仅执行setName
组中的方法,因此将仅执行test_setName
方法。
TestNG XML包含排除方法 (TestNG XML Include Exclude Methods)
By default, all the test methods are included from the test class. We can exclude some methods from the test suite using exclude
element.
默认情况下,所有测试方法都包含在测试类中。 我们可以使用exclude
元素从测试套件中排除某些方法。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG Excludes Test Suite" guice-stage="DEVELOPMENT">
<test thread-count="5" name="TestNG Excludes Test" verbose="2">
<classes>
<class name="com.journaldev.xml.TestNGXMLTest">
<methods>
<exclude name="test_add"></exclude>
</methods>
</class>
</classes>
</test>
</suite>
TestNG XML参数 (TestNG XML Parameters)
We can pass parameters to Test classes methods using parameter
element.
我们可以使用parameter
元素将参数传递给Test类方法。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG Parameters suite" guice-stage="DEVELOPMENT">
<test thread-count="5" name="TestNG Packages Test" verbose="2">
<parameter name="name" value="JournalDev"></parameter> <!-- Test Level Parameters -->
<classes>
<class name="com.journaldev.xml.TestNGXMLTest" />
</classes>
</test>
</suite>
When we run above test, the “name” parameter will be mapped to the test_name
method argument. You will notice following statements in the output console logs.
当我们在测试之上运行时,“ name”参数将映射到test_name
方法参数。 您将在输出控制台日志中注意到以下语句。
Input parameter = JournalDev
PASSED: test_name("JournalDev")
Read more at TestNG Parameters.
在TestNG Parameters中了解更多信息。
TestNG XML侦听器 (TestNG XML Listeners)
We can also specify listeners for our TestNG XML test suite.
我们还可以为我们的TestNG XML测试套件指定侦听器。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="TestNG Listeners Test Suite" guice-stage="DEVELOPMENT">
<listeners>
<listener class-name="com.journaldev.listeners.Test3SuiteListener" />
<listener class-name="com.journaldev.listeners.Test3TestListener" />
</listeners>
<test thread-count="5" name="TestNG Listeners Test" verbose="2">
<classes>
<class name="com.journaldev.xml.TestNGXMLTest" />
</classes>
</test>
</suite>
TestNG listeners is a great feature to monitor and modify test cases behavior, read more at TestNG Listeners.
TestNG侦听器是监视和修改测试用例行为的重要功能,有关更多信息,请参见TestNG侦听器 。
testng xml