PAGE 3: BEFORE AND AFTER ANNOTATION IN TestNG

Before and After annotations are mainly used to execute a certain set of code before and after the execution of test methods. 

These are used to basically set up some variables or configuration before the start of a test execution and then to cleanup any of these things after the test execution ends.

TestNG provides five different kinds of Before and After annotation options, each of which can be used depending upon the test requirements. 

The following are the different before and after options provided by TestNG.

@BeforeSuite and @AfterSuite
@BeforeTest and @AfterTest
@BeforeGroups and @AfterGroups
@BeforeClass and @AfterClass
@BeforeMethod and @AfterMethod


EXAMPLE :

package com.package1;

import org.testng.annotations.Test;

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


public class beforeAndAfterAnnotation {
@Test
public void testCase() {
System.out.println("Test Method");
}

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

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

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

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

@BeforeClass
public void beforeClass() {
System.out.println("Before Class method");
}

@AfterClass
public void afterClass() {
System.out.println("After Class method");
}

@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}

@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
}


EXECUTING TEST VIA TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="My First TestSuite with TestNG">
<test name="Regression Test">
<classes>
<class name="com.package1.beforeAndAfterAnnotation" />
</classes>
</test>
</suite>


OUTPUT:

[TestNG] Running:
  D:\1282016 WorkSpace\TestNgChapter1\src\com\package1\FirstTestNg.xml

Before Suite method
Before Test method
Before Class method
Before Method
Test Method
After Method
After Class method
After Test method
After Suite method

===============================================
My First TestSuite with TestNG
Total tests run: 1, Failures: 0, Skips: 0
===============================================

EXAMPLE-2:

Before and After annotations used in Parent and Child Classes


Let’s create two new classes BaseClass and ChildClass. Then add similar before/after annotations on both of them. Here main thing to notice is that ChildClass extends BaseClass. And Test is defined in ChildClass class.

BASE CLASS:

package com.package1;

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

public class BaseClass {
@BeforeMethod
   public void beforeMethod() {
       System.out.println("BaseClass's Before Test method");
   }
@AfterMethod
   public void afterMethod() {
       System.out.println("BaseClass's After Test method");
   }
@BeforeClass
   public void beforeClass() {
       System.out.println("BaseClass's Before Class method");
   }
@AfterClass
   public void afterClass() {
       System.out.println("BaseClass's After Class method");
   }
}


CHILD CLASS :

package com.package1;

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 ChildClass extends BaseClass {

@BeforeMethod
public void beforeChildMethod() {
System.out.println("ChildClass's Before Test method");
}

@AfterMethod
public void afterChildMethod() {
System.out.println("ChildClass's After Test method");
}

@BeforeClass
public void beforeChildClass() {
System.out.println("ChildClass's Before Class method");
}

@AfterClass
public void afterChildClass() {
System.out.println("ChildClass's After Class method");
}

@Test
public void testCase() {
System.out.println("===== Executing actual test ======");
}

}



EXECUTING TEST VIA TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<suite name="My First TestSuite with TestNG">
<test name="Regression Test">
<classes>
<class name="com.package1.BaseClass" />
<class name="com.package1.ChildClass" />
</classes>
</test>

</suite>



OUTPUT:

[TestNG] Running:
  D:\1282016 WorkSpace\TestNgChapter1\src\com\package1\FirstTestNg.xml

BaseClass's Before Class method
ChildClass's Before Class method
BaseClass's Before Test method
ChildClass's Before Test method
===== Executing actual test ======
ChildClass's After Test method
BaseClass's After Test method
ChildClass's After Class method
BaseClass's After Class method

===============================================
My First TestSuite with TestNG
Total tests run: 1, Failures: 0, Skips: 0
===============================================





















No comments:

Post a Comment

About Me

My photo
You can reach me out at : jimmiamrit@gmail.com

Total Pageviews