Thursday 12 March 2020

TestNG: @BeforeSuite: Run before all the tests in this suite run

@BeforeSuite annotated method run before all the tests in the suite run.

Step 1: Let’s define a class that holds all the suite methods.
package com.sample.app.arithmetic;

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");
 }
}

Step 2: If any test class want to use suite methods, then it should extend BaseTest class.
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 BeforeSuiteTest 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 BeforeSuitTest class, 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


Previous                                                    Next                                                    Home

No comments:

Post a Comment