Earlier, we have discussed the @BeforeTest annotation, which executes before the <test> annotation, and in this post, we will talk about the @AfterTest annotation of TestNG.
@AfterTest annotation executes after all the test cases inside the <test> tag classes are executed. Below XML will illustrate the point of execution of the @AfterTest annotated method.
<suite name="codekru">
<test name="codekru"> <!-- @BeforeTest will execute here -->
<classes>
<class name="Test.CodekruTestFirst" />
<class name="Test.CodekruTestSecond" />
</classes>
</test> <!-- @AfterTest will execute here -->
</suite>
So, let’s see things now with an example. We will be taking two classes mentioned above in the XML file, and we can keep @AfterTest annotated method in any of the classes within the <test> tag.
CodekruTestFirst.java
package Test;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class CodekruTestFirst {
@AfterTest
public void afterTest() {
System.out.println("after test method in CodekruTestFirst class");
}
@Test
public void test() {
System.out.println("Executing the test in CodekruTestFirst class");
Assert.assertTrue(true);
}
}
CodekruTestSecond.java
package Test;
import org.testng.Assert;
import org.testng.annotations.Test;
public class CodekruTestSecond {
@Test
public void test() {
System.out.println("Executing the test in CodekruTestSecond class");
Assert.assertTrue(true);
}
}
Now, run below XML file and let’s see what happens
<suite name="codekru">
<test name="codekru"> <!-- @BeforeTest will execute here -->
<classes>
<class name="Test.CodekruTestFirst" />
<class name="Test.CodekruTestSecond" />
</classes>
</test> <!-- @AfterTest will execute here -->
</suite>
Output –
Executing the test in CodekruTestFirst class
Executing the test in CodekruTestSecond class
after test method in CodekruTestFirst class
===============================================
codekru
Total tests run: 2, Failures: 0, Skips: 0
Here, we can see that the @AfterTest annotated ran after all of the test cases in the classes.
But what if we place another @AfterTest annotated method in the other class? What will happen then?
Well, to know this, we have to try it. Let’s have a @AfterTest annotated method in the CodekruTestSecond class too.
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class CodekruTestSecond {
@AfterTest
public void afterTest() {
System.out.println("after test method in CodekruTestSecond class");
}
@Test
public void test() {
System.out.println("Executing the test in CodekruTestSecond class");
Assert.assertTrue(true);
}
}
Now. let’s run the same XML file again
Executing the test in CodekruTestFirst class
Executing the test in CodekruTestSecond class
after test method in CodekruTestFirst class
after test method in CodekruTestSecond class
===============================================
codekru
Total tests run: 2, Failures: 0, Skips: 0
You can see that both of the annotated methods ran after the execution of test cases. Please visit this link if you want to know more about the other annotations in TestNG.
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.
Other TestNG annotations
- TestNG annotations – @BeforeTest annotation ( counterpart of @AfterTest annotation )
- TestNG annotations – @BeforeSuite annotation
- TestNG annotations – @AfterSuite annotation
- TestNG annotations – @BeforeClass annotation
- TestNG annotations – @AfterClass annotation
- TestNG annotations – @BeforeMethod annotation
- TestNG annotations – @AfterMethod annotation
- TestNG annotations – @BeforeGroups annotation
- TestNG annotations – @AfterGroups annotation