![]() |
VOOZH | about |
@AfterMethod is one of the TestNG Annotations. As the name defines, @AfterMethod is executed after each test method within a test class. Suppose there are n test methods within a test class, then n times @AfterMethod annotated method will be invoked. This annotation allows developers to specify various actions to be taken after test methods are run.
Let's understand the @AfterMethod annotation through an example.
Step 1: In a Maven Project, create a TestNG Class that contains @AfterMethod.
After_Method.java
Now, let's explain what this code does:
afterMethod (@AfterMethod)
Test Methods (@Test):
Step 2: Now, we create the AnnotationsTest.xml file to configure the After_Method class.
Step 3: Run the After_Method file. Right click on the After_Method and move the cursor down to Run As and then click on the TestNG Suite.
As we can observe in above code, the execution of different tests or methods is in proper order. First fifthMethod() Test is executed, then firsthMethod() test, after that secondMethod() and at the end thirdMethod() will be executed.