Execution Order Configuration with TestNG

Have you ever get confused when trying TestNG for the first time? especially when want to make a unit test with some execution order? I have :). So, I made a little example to encounter those problem. As we know, TestNG has some annotation to point our method/class and indicate when the test will running. Some of which are :

  • @BeforeSuite: The annotated method will be run before all tests in this suite have run.
  • @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
  • @BeforeGroups: The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.
  • @BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
  • @BeforeMethod: The annotated method will be run before each test method.

Thus, learning from practice is better, I made a simple unit test class with TestNG

package com.kusandriadi.test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

@Test
public class OrderExecutionTest {

	private Logger log = LoggerFactory.getLogger(OrderExecutionTest.class);

	@Test(groups = "groupTest")
	public void firstGroupTest(){
		printResult("groupTest");
	}

	@Test
	public void firstCommonTest() {
		printResult("commonTest");
	}

	@BeforeGroups(groups = "groupTest")
	public void firstBeforeGroup() {
		printResult("@BeforeGroup");
	}

	@BeforeClass
	public void firstBeforeClass(){
		printResult("@BeforeClass");
	}

	@BeforeMethod
	public void firstBeforeMethod() {
		printResult("@BeforeMethod");
	}

	@BeforeSuite
	public void firstBeforeSuite() {
		printResult("@BeforeSuite");
	}

	@BeforeTest
	public void firstBeforeTest() {
		printResult("@BeforeTest");
	}

	private void printResult(String param) {
		log.info("running {} in class {}", param, OrderExecutionTest.class.getSimpleName());
	}

}

and the result were :

running @BeforeSuite in class OrderExecutionTest
running @BeforeTest in class OrderExecutionTest
running @BeforeClass in class OrderExecutionTest
running @BeforeMethod in class OrderExecutionTest
running commonTest in class OrderExecutionTest
running @BeforeGroup in class OrderExecutionTest
running @BeforeMethod in class OrderExecutionTest
running groupTest in class OrderExecutionTest

Based on above result, the execution order is

  1. @BeforeSuite
  2. @BeforeTest
  3. @BeforeClass
  4. @BeforeGroups
  5. @BeforeMethod

Again, based on above result, we had known the different between @BeforeMethod and @BeforeTest were if we used BeforeTest annotation, the block will be executed once before ALL test methods running whereas the BeforeMethod annotation will executed before every EACH test methods.

Reference :

  1. http://testng.org/doc/documentation-main.html

Regards,

tandatangankecil

Leave a Reply

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