While writing our first testcase with TestNG, we had seen the usage of
Annotations also accepts parameters just like normal Java methods, making them more the useful in our scripts. Below is a list of annotations available in TestNG Framework.
@Test
: It is used to mark a class or method as a Test method. If there are more than one@Test
annotation, they are executed in alphabetical order.@BeforeTest
: It is used when any method needs to be executed before the@Test
method.@AfterTest
: It is used when any method has to be executed after all the@Test
methods in a class.@BeforeMethod
: It is used when a particular method has to be executed before each@Test
method.@AfterMethod
: It is used when a particular method has to be executed after each@Test
method.@BeforeClass
: It is used when a method has to be invoked once before the first test method in the current class.@AfterClass
: It is used when a method has to be invoked once after the last test method in the current class has run.@BeforeGroups
: It is used when a method has to be run before invoking a particular group of@Test
methods.@AfterGroups
: It is used when a method has to be run after a particular group of@Test
methods have been run.@BeforeSuite
: It is used when a method has to be run once before all tests declared inside a TestNG suite.@AfterSuite
: It is used when a method has to be run after all test methods in a TestNG suite have been run.@DataProvider
: It marks a method as a data provider for a@Test
. The annotated method returns an Object double array (Object[ ][ ]) as data which can be assigned as a parameter.@Factory
: It is used to annotate a method that is to be used as a factory that returns an array of class objects (Object[ ]). It’s normally used to run a set of test cases with different values.@Listeners
: This annotation helps in tracking the execution status of a test class and for logging purpose. org.testng.ITestNGListener has to be imported for using this annotation.@Parameters
: It is used to pass parameters to a test method at runtime. Parameter values are provided using testng.xml configuration file.
Now, let’s look at a code example using the above annotations to understand the execution flow. We’ll be covering @DataProvider
, @Factory
, @Listeners
, and @Parameters
in their own post.
Annotations: Example 1
File: AnnotationsDemo.java
package TestNGDemo; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.AfterSuite; public class AnnotationsDemo { @Test public void testCase2() { System.out.println("Executed Test Case 2"); } @Test public void testCase1() { System.out.println("Executed Test Case 1"); } @BeforeMethod public void beforeMethod() { System.out.println("Executed Before Every Method"); } @AfterMethod public void afterMethod() { System.out.println("Executed After Every Method"); } @BeforeClass public void beforeClass() { System.out.println("Executed Before Class Method"); } @AfterClass public void afterClass() { System.out.println("Executed After Class Method"); } @BeforeTest public void beforeTest() { System.out.println("Executed Before Test Method"); } @AfterTest public void afterTest() { System.out.println("Executed After Test Method"); } @BeforeSuite public void beforeSuite() { System.out.println("Executed Before Test Suite Method"); } @AfterSuite public void afterSuite() { System.out.println("Executed After Test Suite Method"); } }
On running the above code you should get the following as output in console:
As you can see, @Suite methods are executed first and the last. It is then followed by @BeforeTest
and @BeforeClass
methods, respectively. @BeforeMethod
is executed twice before @Test
as there are two Tests here. You can also see that testCase1()
was executed before testCase2()
showing us that @Test
methods are executed in alphabetical order.
Extending on the example, say you are running a project that has multiple classes, the execution flow remains the same. Using annotations you can have a common Before and After method for each class or a specific method that has to be executed before every other class. Annotations gives you that control over your project. Let’s see an example that uses an XML file to run multiple test cases together.
Annotations: Example 2
File: A1.java
package TestNGDemo; import org.testng.annotations.BeforeTest; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.AfterSuite; public class A1 { @BeforeSuite() public void BeforeSuite() { System.out.println("Execute Before Suite"); } @AfterSuite() public void AfterSuite() { System.out.println("Execute After Suite"); } @BeforeTest() public void BeforeTest() { System.out.println("Execute Before Test"); } @AfterTest() public void AfterTest() { System.out.println("Execute After Test"); } }
File: A2.java
package TestNGDemo; import org.testng.annotations.Test; public class A2 { @Test public void Test1() { System.out.println("Executing Test1"); } @Test public void Test2() { System.out.println("Executing Test2"); } }
File: testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="AnnotationsDemo"> <test name="test-case1"> <classes> <class name="TestNGDemo.A2" /> <class name="TestNGDemo.A1" /> </classes> </test> <test name="test-case2"> <classes> <class name="TestNGDemo.A2" /> <class name="TestNGDemo.A1" /> </classes> </test> </suite>
Output:
[RemoteTestNG] detected TestNG version 6.14.2 Execute Before Suite Execute Before Test Executing Test1 Executing Test2 Execute After Test Execute Before Test Executing Test1 Executing Test2 Execute After Test Execute After Suite =============================================== AnnotationsDemo Total tests run: 4, Failures: 0, Skips: 0 ===============================================