Earlier, we talked about the @BeforeMethod annotation, and in this post, we will discuss the @AfterMethod annotation of TestNG. @AfterMethod annotated method executes after each test case or test method in a class.
So, when does this @AfterMethod annotated method will be executed?
Below XML will help you understand the annotated method’s execution point.
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="codekru">
<test name="codekru">
<classes>
<class name="Test.CodekruTestFirst">
<methods>
<include name="testMethod1" />
<!-- @AfterMethod annotated method of CodekruTestFirst class will execute
here -->
<include name="testMethod2" />
<!-- @AfterMethod annotated method of CodekruTestFirst class will again
execute here -->
</methods>
</class>
<class name="Test.CodekruTestSecond">
<methods>
<include name="testMethod1" />
<!-- @AfterMethod annotated method of CodekruTestSecond class will execute
here -->
<include name="testMethod2" />
<!-- @AfterMethod annotated method of CodekruTestSecond class will again
execute here -->
</methods>
</class>
</classes>
</test>
</suite>
So, as shown in the above XML, that @AfterMethod annotated method will be executed after every test case in the class. Let’s see that in action.
CodekruTestFirst.java
package Test;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class CodekruTestFirst {
@AfterMethod
public void afterMethod() {
System.out.println("afterMethod in CodekruTestFirst class");
}
@Test
public void testMethod1() {
System.out.println("Executing the test1 in CodekruTestFirst class");
Assert.assertTrue(true);
}
@Test
public void testMethod2() {
System.out.println("Executing the test2 in CodekruTestFirst class");
Assert.assertTrue(true);
}
}
CodekruTestSecond.java
package Test;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class CodekruTestSecond {
@AfterMethod
public void afterMethod() {
System.out.println("afterMethod in CodekruTestSecond class");
}
@Test
public void testMethod1() {
System.out.println("Executing the test1 in CodekruTestSecond class");
Assert.assertTrue(true);
}
@Test
public void testMethod2() {
System.out.println("Executing the test2 in CodekruTestSecond class");
Assert.assertTrue(true);
}
}
Now, we will execute both classes using the below XML file
<suite name="codekru">
<test name="codekruTest">
<classes>
<class name="Test.CodekruTestFirst" />
<class name="Test.CodekruTestSecond" />
</classes>
</test>
</suite>
Output of the XML file –
Executing the test1 in CodekruTestFirst class
afterMethod in CodekruTestFirst class
Executing the test2 in CodekruTestFirst class
afterMethod in CodekruTestFirst class
Executing the test1 in CodekruTestSecond class
afterMethod in CodekruTestSecond class
Executing the test2 in CodekruTestSecond class
afterMethod in CodekruTestSecond class
===============================================
codekru
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================
So, what happened here?
As, By default, TestNG will run our tests in the order they are found in the XML file. So, it will pick CodekruTestFirst class and run its first test case, then the @AfterMethod annotated method, and then again, it will run the second test case and then the @AfterMethod annotated method, and so on, until all of the cases are executed.
And then, it will pick the CodekruTestSecond class and execute it in the same manner.
Now, let us play with things a little 😛
What if there are two @AfterMethod annotated methods in a class? Will both of them execute?
The easiest way of knowing this is just by doing it. So, let us put one more annotated method in the CodekruTestFirst class.
package Test;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class CodekruTestFirst {
@AfterMethod
public void afterMethod1() {
System.out.println("afterMethod1 in CodekruTestFirst class");
}
@AfterMethod
public void afterMethod2() {
System.out.println("afterMethod2 in CodekruTestFirst class");
}
@Test
public void testMethod1() {
System.out.println("Executing the test1 in CodekruTestFirst class");
Assert.assertTrue(true);
}
@Test
public void testMethod2() {
System.out.println("Executing the test2 in CodekruTestFirst class");
Assert.assertTrue(true);
}
}
Below is our XML file to run the CodekruTestFirst class.
<suite name="codekru">
<test name="codekruTest">
<classes>
<class name="Test.CodekruTestFirst" />
</classes>
</test>
</suite>
Output of the XML file –
Executing the test1 in CodekruTestFirst class
afterMethod1 in CodekruTestFirst class
afterMethod2 in CodekruTestFirst class
Executing the test2 in CodekruTestFirst class
afterMethod1 in CodekruTestFirst class
afterMethod2 in CodekruTestFirst class
===============================================
codekru
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
Here we can see that both of the @AfterMethod annotated methods have run. So, it doesn’t matter if you put one or more annotated methods in a single class, they all will run after every test case in that class.
Please visit this article to learn more about TestNG annotations.
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 – @BeforeMethod annotation ( counterpart of @AfterMethod annotation )
- TestNG annotations – @BeforeSuite annotation
- TestNG annotations – @AfterSuite annotation
- TestNG annotations – @BeforeTest annotation
- TestNG annotations – @AfterTest annotation
- TestNG annotations – @BeforeClass annotation
- TestNG annotations – @AfterClass annotation
- TestNG annotations – @BeforeGroups annotation
- TestNG annotations – @AfterGroups annotation