Thursday 12 March 2020

TestNG: AfterSuite: Run after all the tests in this suite ran

@AfterSuite annotated method that run after all the tests in the suite ran.

Step 1: Let’s define a class that holds all the suite methods.

BaseTest.java
package com.sample.app.arithmetic;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseTest {

 @BeforeSuite
 public void beforeSuite_1() {
  System.out.println("Inside beforeSuite_1");
 }

 @BeforeSuite
 public void beforeSuite_2() {
  System.out.println("Inside beforeSuite_2\n");
 }

 @AfterSuite
 public void afterSuite_1() {
  System.out.println("\nInside afterSuite_1");
 }

 @AfterSuite
 public void afterSuite_2() {
  System.out.println("Inside afterSuite_2");
 }
}

Step 2: If any test class want to use suite methods, then it should extend BaseTest class

AfterSuiteTest.java
package com.sample.app.arithmetic;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class AfterSuiteTest extends BaseTest {
 @BeforeClass
 public void beforeClass_1() {
  System.out.println("Inside before class 1");
 }

 @BeforeClass
 public void beforeClass_2() {
  System.out.println("Inside before class 2");
 }

 @BeforeMethod
 public void beforeMethod_1() {
  System.out.println("\nInside beforeMethod_1");
 }

 @BeforeMethod
 public void beforeMethod_2() {
  System.out.println("Inside beforeMethod_2");
 }

 @Test
 public void testCase1() {
  System.out.println("testCase1 executing");
 }

 @Test
 public void testCase2() {
  System.out.println("testCase2 executing");
 }

 @AfterMethod
 public void afterMethod_1() {
  System.out.println("Inside afterMethod_1");
 }

 @AfterMethod
 public void afterMethod_2() {
  System.out.println("Inside afterMethod_2");
 }

 @AfterClass
 public void afterClass_1() {
  System.out.println("\nInside after class 1");
 }

 @AfterClass
 public void afterClass_2() {
  System.out.println("Inside after class 2");
 }

}

Run AfterSuiteTest.java, you will see below messages in console.

Inside beforeSuite_1
Inside beforeSuite_2

Inside before class 1
Inside before class 2

Inside beforeMethod_1
Inside beforeMethod_2
testCase1 executing
Inside afterMethod_1
Inside afterMethod_2

Inside beforeMethod_1
Inside beforeMethod_2
testCase2 executing
Inside afterMethod_1
Inside afterMethod_2

Inside after class 1
Inside after class 2
PASSED: testCase1
PASSED: testCase2

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


Inside afterSuite_1
Inside afterSuite_2



Previous                                                    Next                                                    Home

No comments:

Post a Comment