C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
TestNG @AfterMethod AnnotationThe @AfterMethod annotation is specific to a class not to an XML file. The @AfterMethod annotated method will be invoked after the execution of each test method. Suppose there are four test methods means that @AfterMethod annotated method will be executed four times. Let's understand the @AfterMethod annotation through an example. Step 1: Open the Eclipse. Step 2: We create a simple java project which contains the @AfterMethod annotated method. After_Method.java
package com.TheDeveloperBlog;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class After_Method
{
@AfterMethod
public void after_method()
{
System.out.println("This method will be invoked after the execution of each test method");
}
@Test
public void c_programmers()
{
System.out.println("I am a C programmer");
}
@Test
public void java_programmers()
{
System.out.println("I am a java programmer");
}
@Test
public void dotnet_developer()
{
System.out.println("I am a .Net Developer");
}
}
Step 3: Now, we create a testng.xml file to configure After_Method class. Step 4: Run the testng.xml file. Right click on the testng.xml file and move the cursor down to Run As, and then click on the 1 TestNG Suite. Output
Note: TestNG executes the test methods in alphabetical order.In the above case, first c_programmers() method runs, then @AfterMethod annotated method is executed, thereafter dotnet_developer() method runs, then @AfterMethod annotated method is executed, and at the last java_programmers() method run, and then @AfterMethod annotated method will be executed.
Next TopicTestNG Annotations
|