In the TestNG
Introduction chapter we have come across different annotations used in TestNG
Framework but so far we have used just three (Before, After & Test). All
though these are the most frequently used annotations but who know how far you
will go with your framework and may like to use other useful TestNG
annotations.
Before that I
would like you to give a small idea on Annotations hierarchy or Annotations
levels in TestNG.
<suite>
<test>
<classes>
<method>
<test>
</method>
</classes>
</test>
</suite>
It
says that @Test is the smallest annotation here. @Method will be executed
first, before and after the execution of @Test. The same way @Class will be
executed first, before and after the execution of @Method and so on.
Now
with the below example it will be clear to you easily.
package
automationFramework;
import
org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import
org.testng.annotations.AfterSuite;
import
org.testng.annotations.AfterTest;
import
org.testng.annotations.BeforeClass;
import
org.testng.annotations.BeforeMethod;
import
org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import
org.testng.annotations.Test;
public class Sequencing {
@Test
public void testCase1() {
System.out.println("This is
the Test Case 1");
}
@Test
public void testCase2() {
System.out.println("This is the
Test Case 2");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("This
will execute before every Method");
}
@AfterMethod
public void afterMethod() {
System.out.println("This
will execute after every Method");
}
@BeforeClass
public void beforeClass() {
System.out.println("This
will execute before the Class");
}
@AfterClass
public void afterClass() {
System.out.println("This
will execute after the Class");
}
@BeforeTest
public void beforeTest() {
System.out.println("This
will execute before the Test");
}
@AfterTest
public void afterTest() {
System.out.println("This
will execute after the Test");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("This
will execute before the Test Suite");
}
@AfterSuite
public void afterSuite() {
System.out.println("This
will execute after the Test Suite");
}
}
Output of the above code
will be like this:
This will execute before
the Test Suite
This will execute before
the Test
This will execute before
the Class
This will execute before
every Method
This is the Test Case 1
This will execute after
every Method
This will execute before
every Method
This is the Test Case 2
This will execute after
every Method
This will execute after the
Class
This will execute after the
Test
This will execute after the
Test Suite
No comments:
Post a Comment