by

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

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

Write a Comment

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.