1. Preface
Most of the previous statements are based on test scripts, and a few are based on xml, which is explained here. Before I tell you, there are actually many ways TestNG works
- Run TestNG programmatically
- XML Run TestNG
2. Running TestNG programmatically
1. Run by use case class
package com.tech.design; import org.testng.TestListenerAdapter; import org.testng.TestNG; public class test { public static void main(String[] args) { /* This example creates a TestNG object and runs the test class Run2. It also adds a TestListener. You can use the adapter class org.testng.TestListenerAdapter or implement org.testng.ITestListener yourself. This interface contains various callback methods that allow you to track when tests start, succeed, fail, and so on... */ TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); testng.setTestClasses(new Class[] { Case.class });//Run by Class testng.addListener(tla); testng.run(); } }
2. Run as Group
package com.tech.design; import org.testng.TestListenerAdapter; import org.testng.TestNG; public class test { public static void main(String[] args) { /* This example creates a TestNG object and runs the test class Run2. It also adds a TestListener. You can use the adapter class org.testng.TestListenerAdapter or implement org.testng.ITestListener yourself. This interface contains various callback methods that allow you to track when tests start, succeed, fail, and so on... */ TestListenerAdapter tla = new TestListenerAdapter(); TestNG testng = new TestNG(); testng.setTestClasses(new Class[] { Case.class });//Run by Class /* Set the group to run only a few of the groups. Setting only the group name does not work. You must load the use cases first. It can be either a class or an xml, and group names can be added, separated, depending on the source code. If only some groups do not want to run, you can use the method testNG.setExcludedGroups(groups); Similarly, this group can also contain one or more group names, separated by; */ testng.setGroups("group1");//Run in groups testng.addListener(tla); testng.run(); } }
3. XML Profile Description
1. This is a sample testng.xml file
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Nopackage" > <classes> <class name="NoPackageTest" /> </classes> </test> <test name="Regression1"> <classes> <class name="test.sample.ParameterSample"/> <class name="test.sample.ParameterTest"/> </classes> </test> </suite>
2. You can specify a package name instead of a class name
In this example, TestNG will look at all the classes in the package test.sample. Only classes with TestNG comments will be retained.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" > <suite name="Suite1" verbose="1" > <test name="Regression1" > <packages> <package name="test.sample" /> </packages> </test> </suite>
3. Groups and methods to be included and excluded may be specified:
<test name="Regression1"> <groups> <run> <exclude name="brokenTests" /> <include name="checkinTests" /> </run> </groups> <classes> <class name="test.IndividualMethodsTest"> <methods> <include name="testMethod" /> </methods> </class> </classes> </test>
4. The execution order may be disrupted
TestNG runs tests by default in the order they are found in the XML file. If you want the classes and methods listed in this file to run in an unpredictable order, you can preserve the order Property set to false
<test name="Regression1" preserve-order="false"> <classes> <class name="test.Test1"> <methods> <include name="m1" /> <include name="m2" /> </methods> </class> <class name="test.Test2" /> </classes> </test>
5. Explain XML Tags
1) suite tag
The outermost label of the testNG.xml file, suite, is a test suite, which can have multiple <test>and <groups> beneath it. Several of the properties that can be added are described in the subset test in section 10, which is described in detail below:
(1) Name attribute: This attribute is required, the value can be set by itself, this name will be seen in the testNG report;
(2), verbose property: This property specifies the level of detail of the testNG report, from 0 to 10, of which 10 is the most detailed, and the default generated xml value is 1;
(3) parallel property: This property refers to the designated mode of operation, defaulting to none, that is, the serial mode of operation; parallel execution methods include the following, which are described below:
<suite name="Suite1" parallel="methods" verbose="1" >
- Methods: method level, if this is the value, all test methods under this suite will be multithreaded, that is, at the test case level. If there are dependencies between use cases, the execution order will run according to the set dependencies;
- Tests:TestNG runs all the methods in the same <Test>tag on the same thread, and each <test>tag will be in a separate thread, which allows you to group all classes that are not thread-safe in the same <test>and ensure that they all run in the same thread, while using TestNG to run tests with as many threads as possible.
- classes: class level concurrency, that is, TestNG will run each class under the suite in a separate thread, and all use cases under the same class will run in the same thread;
- Instances: Instance level, that is, TestNG will run all methods in the same instance on the same thread, and two methods on two different instances will run in different threads.
(4), thread-count property: This property is used to specify the number of threads, which can be added only when the parallel parameter is not none, as required;
(5), annotations properties: This is the level of annotations, the method level and the class level, generally do not need to be set;
(6), time-out property: This property is used to specify the time out time for all use cases under the suite;
(7), group-by-instances property: This item is used for methods that have dependencies, and objects that are dependent have multiple overloaded objects, because if the method is dependent and has multiple overloaded methods, the default is to run all the overloaded methods before running the dependent methods, but sometimes we don't want to, set this item to true;
(8), preserve-order property: Value can be entered true or false, if true, use-case execution will be executed in the order in the xml, otherwise it will be executed out of order, default is to execute in order without adding this property;
2) test tag
This tag has no special meaning. It can include several tags, such as groups, classes, etc. It can be accompanied by several attributes, which are described separately below:
(1), name attribute
This attribute is required, the value can be set by itself, and the name will be shown in the testNG report.
(2), verbose properties
This property specifies the level of detail of the testNG report, from 0 to 10, of which 10 is the most detailed, and the default generated xml has a value of 1
(3), threadPoolSize property
This property specifies the thread pool size for this test as a number;
(4), invocationCount property
This property specifies the number of times this test has been run, as shown in the code above.
(5), time-out attribute
This property is used to specify the timeout for all use cases under the suite, as shown in the code above;
(6), group-by-instances attribute
This item is used for methods that depend on, and objects that depend on have multiple overloaded objects, because if the method is dependent and has multiple overloaded methods, the default is to run all overloaded methods before running the dependent methods, but sometimes we don't want to, set this item to true.
(7), preserve-order attribute
Value can be entered true or false, if true, the use case execution will be executed in the order in the xml, otherwise it will be executed out of order, default is to execute in order without adding this attribute;
3) group Tags
This tag must be under the <test>tag to identify groups that will be used for testing or excluded from testing, and its peers must include a <classes>tag or <pakages>tag to specify which packages or classes groups come from;