Before and After annotations explained

Before and After annotations explained

On a general note the Before and After annotations are used to set variables or configurations before the execution of test methods and clean up after the execution.

TestNg has five different options for Before and After annotations. Each of them will be explained below.

	‹ @BeforeSuite/@AfterSuite
	‹ @BeforeTest/@AfterTest
	‹ @BeforeGroups/@AfterGroups ‹
	   @BeforeClass/@AfterClass
	‹ @BeforeMethod/@AfterMethod
‹ @BeforeSuite/@AfterSuite

Before / After suite method will be executed before / after any test from the suite

‹ @BeforeTest/@AfterTest

Before / After test is a method that is executed before / after the first test method from the ‘test’ tag from the testNG.xml

  @BeforeGroups/@AfterGroups 

BeforeGroups/AfterGroups is a method that is executed before / after any test method from the Group that is defined by the “Group” attribute.

See below example :

  @BeforeGroups(groups= {"test"})
  public void beforeGroupsTest(){ 
       System.out.println("This method will execute before any method 
       from the group 'test' ");
	   }
   @BeforeClass/@AfterClass

Before / After class method will execute before / after any of the test methods from the given class

   @BeforeMethod/@AfterMethod

Before / After method will execute before / after each test method

Let us see an example of how the @Before and @After annotations are called

We will create a method for each of these Before/After and call them all so we can actually see the order in which they are executed, related to each other and related to @Test methods.

@BeforeSuite
	public void beforeSuite(){ 
		System.out.println("Before Suite method print ");
	   }

	@AfterSuite
	public void afterSuite(){ 
		System.out.println("After Suite method print");
	   }
	@BeforeTest
	public void beforeTest(){ 
		System.out.println("Before Test method print ");
	   }

	@AfterTest
	public void afterTest(){ 
		System.out.println("After Test method print");
	   }

	@BeforeGroups
	public void beforeGroups(){ 
		System.out.println("Before Groups method print ");
	   }

	@AfterGroups
	public void afterGroups(){ 
		System.out.println("After Groups method print");
	   }
	@BeforeClass
	public void beforeClass(){ 
		System.out.println("Before Class method print ");
	   }

	@AfterClass
	public void afterClass(){ 
		System.out.println("After Class method print");
	   }
	@BeforeMethod
	public void beforeMethods(){ 
		System.out.println("Before Method method print ");
	   }

	@AfterMethod
	public void afterMethods(){ 
		System.out.println("After Method method print");
	   }
	
	@Test
	public void testMethodOne() {
		System.out.println("Test Method One print");
	}
	@Test
	public void testMethodTwo() {
		System.out.println("Test Method Two print");
	}
}

Running the above code will give us the following order :

[RemoteTestNG] detected TestNG version 7.2.0
Before Suite method print 
Before Test method print 
Before Class method print 
Before Method method print 
Test Method One print
After Method method print
Before Method method print 
Test Method Two print
After Method method print
After Class method print
After Test method print
PASSED: testMethodOne
PASSED: testMethodTwo

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

After Suite method print

===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Thus we can see the priority in each of these methods gets to be executed!

The After Suite method has been executed after the entire suite has finished running.

The BeforeMethod/AfterMethod has executed twice for each test method that we had.