testNg annotation
- @BeforeSuite(@AfterSuite)
- Function and scope
@BeforeSuite runs before all tests in the test suite run, only once;
@AfterSuite runs after all tests in the test suite run, only once;
The Test suite can be composed of one or more tests.
- @BeforeTest(@AfterTest)
- Function and scope
The @BeforeTest annotated method will only run once and all test methods that have been run in the current class;
After the @AfterTest annotation method will be run, all test methods belong to the <test> tag of the internal class;
Test can be composed of one or more test classes.
- @BeforeClass(@AfterClass)
- Function and scope
@BeforeClass The annotated method will only run once the method call in the current class;
@AfterClass annotated methods will be run only once and all test methods that have been run in the current class;
Class consists of one or more methods
- @BeforeGroups(@AfterGroups)
- Function and scope
@BeforeGroups group list, this configuration method will run before. This method is to ensure that the first test method belongs to any of these groups is called.
After the @AfterGroups annotation method will be run, all test methods belong to the <test> tag of the internal class;
A group is a collection of tests customized by a test group
- @BeforeMethod(@AfterMethod)
- Function and scope
@BeforeMethod The annotated method will run before each test method.
@AfterMethod annotated methods will run before each test method.
Every time the method in class is executed, @BeforeMethod and @AfterMethod will be called separately
The test level is: suite -> test -> class -> method. test refers to the test tag in the test class, not a @Test in the test class. @Test in the test class actually corresponds to method. Therefore, when using tags such as @BeforeSuite, @BeforeTest, @BeforeClass, and @BeforeMethod, their actual execution order is also based on the above level.
Annotations on superclasses and subclasses will also be inherited. In this case, TestNG guarantees that the "@Before" method is executed in inherited order (first the highest superclass, then the chain is inherited down), and the "@After" method is executed in reverse order (the chain is inherited up).
- Interpretation of testNg test example
<!DOCTYPE suite SYSTEM "/testng-1." > <!-- @BeforeSuite --> <suite name="TestAll"> <packages> <package name=""></package> </packages> <!-- @BeforeTest --> <test name="case1"> <groups> <run> <exclude name="shopping"></exclude> </run> </groups> <classes> <class name="" /> <class name="" /> </classes> </test> <!-- @AfterTest --> <!-- @BeforeTest --> <test name="case2"> <classes> <class name="" /> </classes> </test> <!-- @AfterTest --> <test name="case3"> <groups> <run> <include name="shopping"></include> </run> </groups> <classes> <class name="" /> </classes> </test> </suite> <!-- @AfterSuite -->
The TestAll test group consists of 3 tests (case1, case2, case3);
In case1, a group and a class are defined; a run tag is defined in groups, and an exclude tag is included below the tag; two class files (TestConfiguration and TestDBConnection) are defined in classes;
public class TestConfiguration { @BeforeSuite() public void beforeSuite() { System.out.println("@BeforeSuite"); } @AfterSuite() public void afterSuite() { System.out.println("@AfterSuite"); } @BeforeTest() public void beforeTest() { System.out.println("@BeforeTest"); } @AfterTest() public void afterTest() { System.out.println("@AfterTest"); } @BeforeGroups(groups = "shopping") public void beforeGroups() { System.out.println("@BeforeGroups"); } @AfterGroups(groups = "shopping") public void afterGroups() { System.out.println("@AfterGroups"); } @BeforeClass public void beforeClass() { System.out.println("@BeforeClass"); } @AfterClass public void afterClass() { System.out.println("@AfterClass"); } @BeforeMethod public void beforeMethod() { System.out.println("@BeforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("@AfterMethod"); } @Test(groups = "shopping") public void runTest1() { System.out.println("@Test - runTest1"); } @Test public void runTest2() { System.out.println("@Test - runTest2"); } }
TestConfiguration defines all the annotation files above:
(@BeforeSuite(),@AfterSuite(),@BeforeTest(),@AfterTest(),@BeforeGroups(groups="shopping"),@AfterGroups(groups = "shopping"),@BeforeClass, @AfterClass,@BeforeMethod, @AfterMethod)
There are two test methods (runTest1() and runTest2()); where runTest1() has the grouping identifier of groups = "shopping".
public class TestDBConnection { @Test public void runOtherTest1() { System.out.println("@Test - runOtherTest1"); } @Test public void runOtherTest2() { System.out.println("@Test - runOtherTest2"); } }
There are only two test methods in TestDBConnection, runOtherTest1() and runOtherTest2();
In case2, there is only one class TestDBConnection;
In case3, first there is a group, which contains an include under the run tag, and references the TestConfiguration file.
The output result obtained after running the file:
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test - runTest2
@AfterMethod
@AfterClass
@Test - runOtherTest1
@Test - runOtherTest2
@AfterTest
@Test - runOtherTest1
@Test - runOtherTest2
@BeforeGroups
@Test - runTest1
@AfterGroups
@AfterSuite
During execution, @BeforeSuite and @AfterSuite are executed once after the start and the last end of the execution respectively; in the output file above, the red font is the execution result in case1; the blue content is the execution result in case2, and the purple content is the execution result in case.
The @BeforeSuite() and @AfterSuite() scopes in TestConfiguration in Case1 are at the outermost layer of the entire test group; the @BeforeTest() and @AfterTest() scopes run at the beginning and end of case1; the @BeforeClass and @AfterClass scopes run at the beginning and end of the testConfiguration file; there are two test methods in TestConfiguration, but only runTest2 is executed during execution, because of the exclusion of groups in case1;
There are only two output methods in Case2, and there are no other annotation configurations;
In Case3, @BeforeGroups, @AfterGroups and runTest1 are executed. RunTest2 in TestConfiguration is not executed because the "shopping" group is specified in groups for execution.