From: https://www.cnblogs.com/du-hong/p/11696589.html
brief introduction
In fact, the group brother of this article mentioned it in the previous article, but it was brought with an example, so today there is a special article to explain the relevant knowledge of group. I hope you can have a better understanding of the test team.
1, Test Group
TestNG allows you to group complex test methods into different groups. You can not only declare that a method belongs to a group, but also make the group contain other groups. In this way, TestNG can call or request to include a specific group (or regular expression) and exclude other collections that do not need a group. This way, you don't have to recompile if you're going to split your tests in two. This feature will give you great flexibility in grouping.
The Group is specified in the testng.xml file and can be found under the < test > or < suite > tags. The Group specified in the < suite > tag applies to all < test > tags below. Note that groups are cumulative in these tags: if you specify Group "a" in < suite > and "b" in < test >, then "a" and "b" will be included.
For example, it is common to divide tests into two categories:
Check in tests: these tests run before you commit new code. They are generally very fast, and ensure that no basic function is difficult to use.
Functional test s: these tests cover all the functions in your software and run at least once a day, but you may want them to run continuously.
Typically, detective testing is a subset of functional testing. TestNG allows you to group based on your personal feelings. For example, you might want to group all your test classes into "functest" groups, with several additional methods entering the "checkintent" group.
TestNG allows you to use test group assignments in a very intuitive way. For example, you can build tests by stating that your entire test class belongs to the "functest" group. In addition, some methods belong to the "checkintent" group:
package hongge; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; import hongge.TestSum; /** * @author Beijing - Hongge * * java Automatic test exchange group: 694280102 * * Java Automated test framework-03 - Test Group of TestNG * * 2019 October 22, 2010 */ public class Test1 { @Test(groups = { "functest", "checkintest" }) public void testMethod1() { } @Test(groups = {"functest", "checkintest"} ) public void testMethod2() { } @Test(groups = { "functest" }) public void testMethod3() { } }
Call TestNG with the following
<test name="Test1"> <groups> <run> <include name="functest"/> </run> </groups> <classes> <class name="example1.Test1"/> </classes> </test>
The above will run all the tests in the above class. When you want to use checkintent to make a call, just run testMethod1() and testMethod2().
Here is another example. Use regular expressions this time. Assume that some test methods should not run in Linux In an environment, your tests will look like:
package hongge; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; import hongge.TestSum; /** * @author Beijing - Hongge * * java Automatic test exchange group: 694280102 * * Java Automated test framework-03 - Test Group of TestNG * * 2019 October 22, 2010 */ @Test public class Test1 { @Test(groups = {"windows.checkintest"}) public void testWindowsOnly() { } @Test(groups = {"linux.checkintest"}) public void testLinuxOnly() { } @Test(groups = {"windows.functest"}) public void testWindowsToo() { } }
Then you can use the following testng.xml to run only under Windows:
<test name="Test1"> <groups> <run> <include name="windows.*"/> </run> </groups> <classes> <class name="example1.Test1"/> </classes> </test>
Note: TestNG uses regular expressions, not wildcards. Pay attention to the difference between the two
For example, "anything" matches "*" -- points and asterisks -- not asterisks "*"
2, MetaGroups (in group)
Test groups can also contain other groups. Such groups are called "MetaGroups.". For example, you might define a group all to contain other groups, chekcintest and functest. "Functest" itself only contains group windows and linux, while "checkintent" only contains windows. You can define it in the properties file as follows:
<test name="Regression1"> <groups> <define name="functest"> <include name="windows"/> <include name="linux"/> </define> <define name="all"> <include name="functest"/> <include name="checkintest"/> </define> <run> <include name="all"/> </run> </groups> <classes> <class name="test.sample.Test1"/> </classes> </test>
3, Exclusion group
TestNG allows you to include groups and, of course, exclude them.
For example, it's common for current tests to be interrupted because of recent changes and you don't have time to fix these problems. However, you also need your own functional tests to run correctly, so pharmaceutical simply let these unnecessary tests fail. But don't forget to make it work again when you need it later.
A simple way to solve this problem is to create a group called "broken" and then make these test methods subordinate to that group. For example, suppose I know that testMethod2() is broken, so I want to invalidate it:
package hongge; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; import hongge.TestSum; /** * @author Beijing - Hongge * * java Automatic test exchange group: 694280102 * * Java Automation Test framework -03 - Test Group of TestNG * * 2019 October 22, 2010 */ @Test(groups = {"checkintest", "broken"} ) public void testMethod2() {}
All I need to do is exclude this group from running:
<test name="Simple example"> <groups> <run> <include name="checkintest"/> <exclude name="broken"/> </run> </groups> <classes> <class name="example1.Test1"/> </classes> </test>
In this way, we can get a clean test run and track the tests that need to be fixed later.
Note: you can do this by using the "enabled" attribute, which applies to @ Test and @ Before/After annotation.
4, Local group
You can define groups at the class level, and then at the method level:
package hongge; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; import hongge.TestSum; /** * @author Beijing - Hongge * * java Automatic test exchange group: 694280102 * * Java Automated test framework-03 - Test Group of TestNG * * 2019 October 22, 2010 */ @Test(groups = { "checkin-test" }) public class All { @Test(groups = { "func-test" ) public void method1() { ... } public void method2() { ... } }
In this class, method2() is part of the class level group "checkin test", while method1() belongs to both "checkin test" and "func test" groups.
5, Method group
You can exclude or include a single method
<test name="Test1"> <classes> <class name="example1.Test1"> <methods> <include name=".*enabledTestMethod.*"/> <exclude name=".*brokenTestMethod.*"/> </methods> </class> </classes> </test>
This can be used to disable individual methods without having to recompile anything, but I don't recommend using this technique too much, because if you start refactoring Java code (regular expressions used in regular expressions), it will crash your test framework. Tags may no longer match your method).
Summary
Hey hey! Let's share today. The next plan is Test Method. I hope you guys and kids like and continue to pay attention to brother Hong!