Reputation: 207
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
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
Reputation: 127
Annotations execution order:
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
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:
There are various other annotations provided by
TestNG
and different types of attributes/parameters can be passed to these annotations. For more information onTestNG
annotations follow this link
Upvotes: 12