Skip to toolbar

TestNG – 10 || Use @BeforeTest and @AfterTest Annotations.

@BeforeTest and @AfterTest annotations invoke specific methods around a Test in a TestSuite.

To understand these annotations let’s revisit @BeforeClass and @AfterClass annotations. As we know that in a Class file, methods marked with these annotations would be executed before and after all the test methods get executed.

The same way methods marked as @BeforeTest and @AfterTest would get executed before and after all the methods from each class(s). These classes have been called under one Test which is defined in a TestSuite.

I know the above statement looks a little complex. Let’s understand it step by step.

@BeforeTest and @AfterTest

@BeforeTest and @AfterTest Example.

To achieve the above process we have to follow a few steps:

  • Create 2 Class Files with Methods: As described in the above image, first of all, we have to create 2 Class files with few methods marked with @Test annotations.
  • Create one more Class to Define @BeforeTest and @AfterTest: In any one class define methods marked with @BeforeTest and @AfterTest annotations.
  • TestSuite.XML File: Generate a TestSuite.xml file to define Tests in it.
    • Define multiple Tests: Create and define multiple tests under TestSuite.xml file using <test name=”string”> tag.
    • Declare classes: Now declare all relevant classes under test tag using <classes> and <class name=”packageName.methodName”/> tags.

Note: To understand @BeforeTest and @Aftertest annotation, one should have better understanding of TestSuite.xml files. To learn more on TestSuites please visit previous chapters.

Step 1: Create 2 Class Files with Methods

MyOrders.java

package beforeAfterTest;
import org.testng.annotations.Test;
/**
 * @author ashok.kumar
 *
 */
public class MyOrders {

	@Test
	public void viewOrder()
	{
		System.out.println("View your orders.");
	}
	
	@Test
	public void placeOrder()
	{
		System.out.println("Place your order.");
	}
}

MyProfile.java

package beforeAfterTest;
import org.testng.annotations.Test;
/**
 * @author ashok.kumar
 *
 */
public class MyProfile {

	@Test
	public void viewProfile()
	{
		System.out.println("View your profile.");
	}
	@Test
	public void updateProfile()
	{
		System.out.println("Update your profile");
	}
}

Step 1: Create one more Class to Define @BeforeTest and @AfterTest

BrowserHandling.java

package beforeAfterTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/**
 * @author ashok.kumar
 */
public class BrowserHandling {
	
	@BeforeTest
	public void launchURL()
	{
		System.out.println("Before Test Executed.");
	}
	
	@Test
	public void login()
	{
		System.out.println("Before Test Executed.");
	}
	
	@AfterTest
	public void logout()
	{
		System.out.println("After Test Executed.");
	}
}

Step 3: Create TestSuite.XML File

We can create n number of tests in a test suite. But here we will create one test named “Test1” and call all the required classes under “Test1”.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="beforeAfterTestSuite">
<test name="Test1">
	<classes>
		<class name="beforeAfterTest.BrowserHandling"/>
		<class name="beforeAfterTest.MyOrders"/>
		<class name="beforeAfterTest.MyProfile"/>
	</classes>
</test>
</suite>

Execution and Result Analysis

Once all the above files have been created. Now run the TestSuite.xml file using TestNG.

Console Output for to view functionality of @BeforeTest and @AfterTest.

Index.html

Result Analysis TestSuite in index.html

In the above result, we can see that we have achieved the same result which we have seen in the example at the starting point.

Now if revisit the first statement of this chapter “@BeforeTest and @AfterTest annotations invoke specific methods around a Test in a TestSuite.

For any query or question just drop me a mail or leave a message in the comment box.


Related Links:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


2975total visits,5visits today

Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 2
  • Total visitors : 48,855
  • Total page view: 67,273

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.