Specify Method Name Sequence in TestNG



A TestNG class can have various TestNG methods such as @BeforeTest, @AfterTest, @BeforeSuite, @BeforeClass, @BeforeMethod, @test, etc. In this article, we will explain the order of execution of different TestNG methods.

TestNG consists of the following methods to support the main @Test method. The order of execution should be as follows −

<beforeSuite> <beforeTest> <beforeClass> <beforeMethod> <test1> <afterMethod> <afterClass> <afterTest> <afterSuite>

Key points in this order are:

  • First of all, beforeSuite() method is executed only once.

  • The afterSuite() method executes only once.

  • Even the methods beforeTest(), beforeClass(), afterClass(), and afterTest() methods are executed only once.

  • beforeMethod() method executes for each test case (every time for a new @Test), but before executing the test case.

  • afterMethod() method executes for each test case (every time for a new @Test), but after executing the test case.

  • In between beforeMethod() and afterMethod(), each test case (@Test annotation’s method) executes.

Approach/Algorithm to solve this problem:

  • Step 1 − import org.testng.annotations.* for TestNG.

  • Step 2 − Write an annotation as @test

  • Step 3 − Create a method for the @test annotation as test1.

  • Step 4 − Repeat the steps for test2 and test3.

  • Step 5 − Write different annotations and their respective methods. For example, @beforeSuite, @afterSuite, @beforeTest, @afterTest, @beforeClass, @afterClass, @beforeMethod, @afterMethod.

  • Step 6 − Now create the testNG.xml as given below.

  • Step 7 − Finally, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

Use the following code to show the order of different TestNG methods −

import org.testng.annotations.*; import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { // test case 1 @Test public void testCase1() { System.out.println("in test case 1"); } // test case 2 @Test public void testCase2() { System.out.println("in test case 2"); } @BeforeMethod public void beforeMethod() { System.out.println("in beforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("in afterMethod"); } @BeforeClass public void beforeClass() { System.out.println("in beforeClass"); } @AfterClass public void afterClass() { System.out.println("in afterClass"); } @BeforeTest public void beforeTest() { System.out.println("in beforeTest"); } @AfterTest public void afterTest() { System.out.println("in afterTest"); } @BeforeSuite public void beforeSuite() { System.out.println("in beforeSuite"); } @AfterSuite public void afterSuite() { System.out.println("in afterSuite"); } }

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "OrderofTestExecutionInTestNG"/> </classes> </test> </suite>

Output

in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite
Updated on: 2022-01-12T13:21:20+05:30

624 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements