Thursday 12 March 2020

TestNG: BeforeTest and AfterTest annotations

@BeforeTest annotated methods run before the classes in <test> element start execution.

@AfterTest annotated methods run after the classes in <test> element executed.

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

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

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

 @BeforeTest
 public void beforeTest_1() {
  System.out.println("---------------------------------\n");
  System.out.println("Inside beforeTest_1");
 }

 @BeforeTest
 public void beforeTest_2() {
  System.out.println("Inside beforeTest_2");
 }

 @AfterTest
 public void afterTest_1() {
  System.out.println("Inside afterTest_1");
 }

 @AfterTest
 public void afterTest_2() {
  System.out.println("Inside afterTest_2");
  System.out.println("---------------------------------\n");
 }
}

TestClass1.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 TestClass1 extends BaseTest {
 @BeforeClass
 public void beforeClass_1() {
  System.out.println("********************************");
  System.out.println("TestClass1: Inside before class 1");
 }

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

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

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

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

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

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

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

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

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

}

TestClass2.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 TestClass2 extends BaseTest {
 @BeforeClass
 public void beforeClass_1() {
  System.out.println("\n********************************");
  System.out.println("TestClass2: Inside before class 1");
 }

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

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

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

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

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

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

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

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

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

}

TestClass3.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 TestClass3 extends BaseTest {
 @BeforeClass
 public void beforeClass_1() {
  System.out.println("\n********************************");
  System.out.println("TestClass3: Inside before class 1");
 }

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

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

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

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

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

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

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

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

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

}

TestClass4.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 TestClass4 extends BaseTest {
 @BeforeClass
 public void beforeClass_1() {
  System.out.println("\n********************************");
  System.out.println("TestClass4: Inside before class 1");
 }

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

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

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

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

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

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

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

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

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

}

suiteDemo.xml
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >

<suite name="sanity test suite">

 <test name="Sanity test 1">
  <classes>
   <class name="com.sample.app.arithmetic.TestClass1"></class>
   <class name="com.sample.app.arithmetic.TestClass2"></class>
  </classes>
 </test>

 <test name="Sanity test 2">
  <classes>
   <class name="com.sample.app.arithmetic.TestClass3"></class>
   <class name="com.sample.app.arithmetic.TestClass4"></class>
  </classes>
 </test>

</suite>

Place ‘suiteDemo.xml’ file in the same package where test classes are present.

How to run the test suite file?
Right click on the xml file.

Run As -> TestNG Suite.

You will get below messages in console.

Inside beforeSuite_1
Inside beforeSuite_2

---------------------------------

Inside beforeTest_1
Inside beforeTest_2
********************************
TestClass1: Inside before class 1
TestClass1: Inside before class 2

TestClass1: Inside beforeMethod_1
TestClass1: Inside beforeMethod_2
TestClass1: testCase1 executing
TestClass1: Inside afterMethod_1
TestClass1: Inside afterMethod_2

TestClass1: Inside beforeMethod_1
TestClass1: Inside beforeMethod_2
TestClass1: testCase2 executing
TestClass1: Inside afterMethod_1
TestClass1: Inside afterMethod_2

TestClass1: Inside after class 1
TestClass1: Inside after class 2

********************************
TestClass2: Inside before class 1
TestClass2: Inside before class 2

TestClass2: Inside beforeMethod_1
TestClass2: Inside beforeMethod_2
TestClass2: testCase1 executing
TestClass2: Inside afterMethod_1
TestClass2: Inside afterMethod_2

TestClass2: Inside beforeMethod_1
TestClass2: Inside beforeMethod_2
TestClass2: testCase2 executing
TestClass2: Inside afterMethod_1
TestClass2: Inside afterMethod_2

TestClass2: Inside after class 1
TestClass2: Inside after class 2
Inside afterTest_1
Inside afterTest_2
---------------------------------

---------------------------------

Inside beforeTest_1
Inside beforeTest_2

********************************
TestClass3: Inside before class 1
TestClass3: Inside before class 2

TestClass3: Inside beforeMethod_1
TestClass3: Inside beforeMethod_2
TestClass3: testCase1 executing
TestClass3: Inside afterMethod_1
TestClass3: Inside afterMethod_2

TestClass3: Inside beforeMethod_1
TestClass3: Inside beforeMethod_2
TestClass3: testCase2 executing
TestClass3: Inside afterMethod_1
TestClass3: Inside afterMethod_2

TestClass3: Inside after class 1
TestClass3: Inside after class 2

********************************
TestClass4: Inside before class 1
TestClass4: Inside before class 2

TestClass4: Inside beforeMethod_1
TestClass4: Inside beforeMethod_2
TestClass4: testCase1 executing
TestClass4: Inside afterMethod_1
TestClass4: Inside afterMethod_2

TestClass4: Inside beforeMethod_1
TestClass4: Inside beforeMethod_2
TestClass4: testCase2 executing
TestClass4: Inside afterMethod_1
TestClass4: Inside afterMethod_2

TestClass4: Inside after class 1
TestClass4: Inside after class 2
Inside afterTest_1
Inside afterTest_2
---------------------------------


Inside afterSuite_1
Inside afterSuite_2




Previous                                                    Next                                                    Home

No comments:

Post a Comment