Ah ha…a topic which confused me also to understand the difference between BeforeTest and BeforeClass annotation. When we read first time it looks same and then we think if it is same then why TestNg included it. So clue is, it is not same and included for two different purposes-

We will go through again with these annotations definition which is available under TestNg official site and that are-

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

@BeforeClass-The annotated method will be run before the first test method in the current class is invoked.

Have a look on below example-

public class FirstClass {
@BeforeTest
public void BT(){
System.out.println(“This is before test”);
}
@AfterTest
public void AT(){
System.out.println(“This is after test”);
}
@Test (priority =-20)
public void fctestmethod(){
System.out.println(“This is First Class test method.”);
}}

————————————————————————————————-

public class SecondClass {
@BeforeClass
public void BC(){
System.out.println(“This is before class”);
}
@AfterClass
public void AC(){
System.out.println(“This is after class”);
}
@Test (priority =-19)
public void sctestFirstmethod(){
System.out.println(“This is Second Class first test method.”);
}
@Test (priority =-18)
public void sctestSecondmethod(){
System.out.println(“This is Second Class second test method.”);
}}

————————————————————————————————-

————————————————————————————————-

[TestNG] Running:
D:\Training\BeforeTestndClassExample\testng.xml

This is before test
This is First Class test method.
This is before class
This is Second Class first test method.
This is Second Class second test method.
This is after class
This is after test

===============================================
My test suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

————————————————————————————————-

Hints:

  • <test> tag is wrapped by @BeforeTest and @AfterTest
  • <class> tag is wrapped by @BeforeClass and @AfterClass

So if we see in test result @BeforeTest method executed very first and then lowest priority @Test method (fctestmethod). In third position @BeforeClass method executed which was defined in Second Class. Now question is why it got executed at third position, so answer is-

  • <class> tag is wrapped by @BeforeClass and @AfterClass

and

@BeforeClass-The annotated method will be run before the first test method in the current class is invoked.

As @Before and After was defined in Second Class therefore it got executed when testng.xml invoked Second Class class file not before any test.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s