"Other tutorials in this series are being translated, click on Category: TestNG for viewing. 】
"Translation by clearly like the month QQ 605283073"
Original address: http://websystique.com/java/testing/testng-groups-example/
Previous post: TestNG Annotations Example
This article describes the use of @beforegroups, @AfterGroups annotations in testng group tests.
TestNG allows us to run multiple test methods in the form of a group.
We can group the test methods according to the behavior and so on.
Multiple tests can belong to a group, and a test can be part of multiple groups.
--------------------------------------
@BeforeGroups, @AfterGroups
The method with @beforegroups annotations will be executed once before any test methods in this group are executed.
Similar to this is the method with @aftergroups annotations that will be executed after any test method in this group
Package com.websystique.testng;
Import org.testng.annotations.AfterGroups;
Import org.testng.annotations.BeforeGroups;
Import Org.testng.annotations.Test; public class Testnggroupexample {@BeforeGroups ("security") public void setupsecurity () {System.out.pri
Ntln ("setupsecurity ()");
} @AfterGroups ("Security") public void teardownsecurity () {System.out.println ("teardownsecurity () \ n");
} @BeforeGroups ("database") public void Setupdatabase () {System.out.println ("setupdatabase ()"); } @AfterGroups ("database") public void Teardowndatabase () {System.out.println ("teardowndatabase () \ n
");
} @BeforeGroups (value= "UI") public void Setupui () {System.out.println ("setupui ()");
} @AfterGroups (value= "UI") public void Teardownui () {System.out.println ("Teardownui () \ n"); } @Test (groups= "database") public void Testinsert () {System.out.println ("TeStinsert () ");
} @Test (groups= "database") public void Testupdate () {System.out.println ("testupdate ()");
} @Test (groups= "database") public void Testdelete () {System.out.println ("testdelete ()");
} @Test (groups= "security") public void Accesshomepage () {System.out.println ("accesshomepage ()");
} @Test (groups= "security") public void Accessadminpage () {System.out.println ("accessadminpage ()"); } @Test (groups= "UI") public void Openconfirmationdialog () {System.out.println ("openconfirmationdial
OG () ");
} @Test (groups= "UI") public void OpenFileDialog () {System.out.println ("OpenFileDialog ()"); }
}
MVN clean test or the TestNG Eclipse plugin mentioned in the first article runs this test:
Setupsecurity ()
accessadminpage ()
accesshomepage ()
teardownsecurity ()
setupui ()
Openconfirmationdialog ()
OpenFileDialog ()
Teardownui ()
setupdatabase ()
testdelete ()
Testinsert ()
testupdate ()
teardowndatabase ()
passed:accessadminpage
passed:accesshomepage
Passed:openconfirmationdialog
Passed:openfiledialog
passed:testdelete
passed:testinsert
passed:testupdate
============== =================================
Default Test
Tests Run:7, failures:0, skips:0
=================== ============================
===============================================
Default Suite
Total Tests Run:7, failures:0, skips:0
===============================================
@BeforeGroups is called once before any test methods in this group are executed.
@AfterGroups is called once after any test method in this group has been executed.
Group Expansion:
Let's say we only want to perform security and database-related tests, not UI-related.
We can use the TestNG configuration XML file to exclude the UI.
Create Testng.xml under the Src/test/resources folder
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "tests" >
<test name= " Backend ">
<groups>
<run>
<exclude name=" UI "/>
</run>
</groups >
<classes>
<class name= "com.websystique.testng.TestNGGroupExample"/>
</classes >
</test>
</suite>
If you are practical with MAVEN-based projects, you can configure Maven-surefire-plugin in Pom.xml
Here is the configuration of the pomx.ml:
ersion>
<configuration>
<suiteXmlFiles>
<suitexmlfile>src/test/resources/ testng.xml</suitexmlfile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Right-click Testng.xml file-> runas->testng Suite]
Setupsecurity ()
accessadminpage ()
accesshomepage ()
teardownsecurity ()
setupdatabase ()
Testdelete ()
Testinsert ()
testupdate ()
teardowndatabase ()
================================ ===============
tests Total
tests Run:5, failures:0, skips:0
====================================== =========
You can see that the UI-related test methods and corresponding beforegroups/aftergroups are not running.