TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

TestNG

TestNG Annotations Order Example

Overview

In this tutorial, we will learn TestNG annotations order. In the earlier post, we had a look at some of the various TestNG annotations.

https://www.testingdocs.com/testng-annotations/

@BeforeSuite
This Before Suite Method is executed before any of the tests in the TestNG suite XML file.

@AfterSuite
This After Suite Method is executed after all the tests in the TestNG suite XML file are executed.

@BeforeTest
This Before Test method is executed before the first @Test annotated method in each test tag in TestNG suite XML file.

@AfterTest
This After Test method is executed after the last test method in the test tag in the suite XML file.

@BeforeClass
This Before Class method which is executed before the test methods inside a test class.

@AfterClass
This After Class Method is executed after all of the test methods inside a test class.

@BeforeGroups
This Before Groups Method is executed before execution of the tests belonging to the group specified in the ‘groups’ attribute.

@AfterGroups
This After Groups Method is executed after execution of all the tests belonging to the group specified in the ‘groups’ attribute.

@BeforeMethod
This Before Method is executed before each @Test annotated method.

@AfterMethod
This After Method is executed after each @Test annotated method.

TestNG Annotations Order – Example

To know the TestNG Annotations order of execution, we will write a test class with the annotations and run the test class using the testng.xml file.

 

package com.testingdocs.testng.tutorials;

//www.TestingDocs.com - TestNG Tutorials

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestNGAnnotationExecutionOrder {

 // Before Suite method
 @BeforeSuite
 public void beforeSuiteTest(){
 System.out.println("@BeforeSuite execution");
 }


 // After Suite method
 @AfterSuite
 public void afterSuiteTest(){
 System.out.println("@AfterSuite execution");
 }

 // Before Test method 
 @BeforeTest
 public void beforeTest(){
 System.out.println("@BeforeTest execution"); }

 // After test method
 @AfterTest
 public void afterTest(){
 System.out.println("@AfterTest execution");
 }

 //Before class method
 @BeforeClass
 public void beforeClassTest(){
 System.out.println("@BeforeClass execution");
 }


 // After class method 
 @AfterClass
 public void afterClassTest(){
 System.out.println("@AfterClass execution");
 }

 //Before Groups
 //This method is executed before execution of the test method belonging
// to group "P1".
 @BeforeGroups(groups={"P1"})
 public void beforeGroupOneTest(){
 System.out.println("@BeforeGroups P1 execution");
 }

 //After Groups
 //This method is executed after execution of the * test methods belonging to
// group "P1". 
 @AfterGroups(groups={"P1"})
 public void afterGroupOneTest(){
 System.out.println("@AfterGroups P1 excution");
 } 


 //This method is executed before execution of the
 //test methods belonging to group "P2" 
 @BeforeGroups(groups={"P2"})
 public void beforeGroupTwoTest(){
 System.out.println("@BeforeGroups P2 execution");
 }


 //This method is executed after execution of the
 //test methods belonging to group "P2"
 @AfterGroups(groups={"P2"})
 public void afterGroupTwoTest(){
 System.out.println("@AfterGroups P2 execution");
 }

 //BeforeMethod
 @BeforeMethod
 public void beforeMethodTest(){
 System.out.println("@BeforeMethod execution");
 }


 // AfterMethod
 @AfterMethod
 public void afterMethodTest(){
 System.out.println("@AfterMethod execution");
 }

 //This Test method doesn't belong to any group. 
 @Test()
 public void sampleTestOne(){ 
 System.out.println("Sample Test One method execution");
 }

 //This Test method doesn't belong to any group. 
 @Test()
 public void sampleTestTwo(){ 
 System.out.println("Sample Test Two method execution");
 }
 
 //This Test method which belongs to group "P1". 
 @Test(groups={"P1"})
 public void groupP1MethodTest(){ 
 System.out.println("Group P1 Test method execution");
 } 


 //This Test method belongs to group "P2". 
 @Test(groups={"P2"})
 public void groupP2MethodTest(){
 System.out.println("Group P2 Test method execution");
 }
}

 

TestNG Annotations Order

testng.xml File

 

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<!-- www.TestingDocs.com TestNG Tutorials -->
<suite name="TestingDocs Suite">
 <test name="TestOne">
 <classes>
 <class name="com.testingdocs.testng.tutorials
.TestNGAnnotationExecutionOrder">
 </class>
 </classes>
 </test>
</suite>

 

Run the TestNG Suite file. Right click on the testng.xml file and choose Run As >> TestNG Suite option in the context menu. The TestNG annotations order of execution is shown in the output.

Sample Output

@BeforeSuite execution
@BeforeTest execution
@BeforeClass execution
@BeforeMethod execution
Group P1 Test method execution
@AfterMethod execution
@BeforeMethod execution
Group P2 Test method execution
@AfterMethod execution
@BeforeMethod execution
Sample Test One method execution
@AfterMethod execution
@BeforeMethod execution
Sample Test Two method execution
@AfterMethod execution
@AfterClass execution
@AfterTest execution
@AfterSuite execution

===============================================
TestingDocs Suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

 

—

TestNG Tutorials on this website can be found at:

https://www.testingdocs.com/testng-framework-tutorial/

For more details on the TestNG Framework, visit the official website of TestNG at:

https://testng.org

Related Posts

Tests TestNG Suite testng.xml

TestNG /

Run tests from TestNG Suite testng.xml file

Add TestNG library

TestNG /

Add the TestNG library to the project

new TestNG class

TestNG /

Create TestNG test class in Eclipse IDE.

TestNG Plugin IntelliJ

TestNG /

Enable TestNG in IntelliJ IDE

Installing Eclipse from Update Site

TestNG /

Install TestNG latest version from the update site

‹ Hard and Soft dependency in TestNG Framework› TestNG Before and After Annotations

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version