Adam Grunt
Adam Grunt

Reputation: 207

TestNG: priority of @BeforeClass and @BeforeTest

I have been using TestNG and having problems with two annotations, @BeforeTest and @BeforeClass. I would like to know if both are applied which will run first ?

Upvotes: 6

Views: 11183

Answers (3)

Yacdaniel Hurtado
Yacdaniel Hurtado

Reputation: 56

Before test first and then before class.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
http://testng.org/doc/documentation-main.html#annotations

Upvotes: 1

Annotations execution order:

  1. BeforeSuite
  2. BeforeTest
  3. BeforeClass
  4. BeforeMethod
  5. Test
  6. AfterGroups
  7. AfterClass
  8. AfterTest

You can check with the pseudo code:

public class TestAnnotationsPriorityOrder {

public int i=0;


@BeforeSuite
public void beforeSuite(){
    i++;
    System.out.println(i+"::BeforeSuite");
}

@AfterSuite
public void afterSuite(){
    i++;
    System.out.println(i+"::AfterSuite");
}

@BeforeTest
public void beforeTest(){
    i++;
    System.out.println(i+"::BeforeTest");
}

@AfterTest
public void afterTest(){
    i++;
    System.out.println(i+"::AfterTest");
}


@BeforeGroups
public void beforeGroups(){
    i++;
    System.out.println(i+"::BeforeGroups");
}

@AfterGroups
public void afterGroups(){
    i++;
    System.out.println(i+"::AfterGroups");
}


@BeforeClass
public void beforeClass(){
    i++;
    System.out.println(i+"::BeforeClass");
}

@AfterClass
public void afterClass(){
    i++;
    System.out.println(i+"::AfterClass");
}


@BeforeMethod
public void beforeMethod(){
    i++;
    System.out.println(i+"::BeforeMethod");
}

@AfterMethod
public void afterMethod(){
    i++;
    System.out.println(i+"::AfterGroups");
}


@Test
public void TestMethod(){
    i++;
    System.out.println(i+"::Test");
}


}

Upvotes: 2

Atul Dwivedi
Atul Dwivedi

Reputation: 1462

Answer: Method annotated with @BeforeTest will be invoked before than the method annotated with @BeforeClass.

TestNG annotations execution order in reference to @Test and description:

  1. @BeforeSuite: The annotated method will be run before all tests in this suite have run.
  2. @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
  3. @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.
  4. @BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
  5. @BeforeMethod: The annotated method will be run before each test method.
  6. @Test: The test method or class
  7. @AfterMethod: The annotated method will be run after each test method.
  8. @AfterClass: The annotated method will be run after all the test methods in the current class have been run.
  9. @AfterGroups: The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.
  10. @AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
  11. @AfterSuite: The annotated method will be run after all tests in this suite have run.

There are various other annotations provided by TestNG and different types of attributes/parameters can be passed to these annotations. For more information on TestNG annotations follow this link

Upvotes: 12

Related Questions