TestNG - Basic Annotations - AfterMethod



@AfterMethod annotated method will be run after each test method i.e say there are three test methods (i.e test cases), then @AfterMethod annotated method will be called thrice after each test method.

The following is a list of attributes supported by the @AfterMethod annotation:

Attribute Description

alwaysRun

For before methods (AfterClass, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.

For after methods (afterSuite, afterClass, ...): If set to true, this configuration method will be run even if one or more methods invoked previously failed or was skipped.

dependsOnGroups

The list of groups this method depends on.

dependsOnMethods

The list of methods this method depends on.

enabled

Whether methods on this class/method are enabled.

groups

The list of groups this class/method belongs to.

inheritGroups

If true, this method will belong to groups specified in the @Test annotation at the class level.

onlyForGroups

Only for @BeforeMethod and @AfterMethod. If specified, then this setup/teardown method will only be invoked if the corresponding test method belongs to one of the listed groups.

Create a Class

Create a java class to be tested, say, MessageUtil.java in /work/testng/src.

/*
* This class prints the given message on console.
*/

public class MessageUtil {

   private String message;

   //Constructor
   //@param message to be printed
   public MessageUtil(String message) {
      this.message = message;
   }

   // prints the message
   public String printMessage() {
      System.out.println(message);
      return message;
   }
}

Create Test Case Class

  • Create a java test class, say, TestAnnotationAfterMethod.java in /work/testng/src.

  • Add a test method testMethod() to your test class.

  • Add an Annotation @Test to method testMethod().

  • Add a method afterMethod to the test class with annotation @AfterMethod.

  • Implement the test condition and check the behaviour of @AfterMethod annotation.

Following are the TestAnnotationAfterMethod.java contents:

  import org.testng.Assert;
  import org.testng.annotations.Test;
  import org.testng.annotations.AfterMethod;

  public class TestAnnotationAfterMethod {
    @AfterMethod
    public void afterMethod(){
      System.out.println("executing afterMethod after each method");
    }
    @Test
    public void testMethodOne(){
      Assert.assertEquals("Test method one", (new MessageUtil("executing testMethodOne method")).printMessage());
    }
    @Test
    public void testMethodTwo(){
      Assert.assertEquals("Test method two", (new MessageUtil("executing testMethodTwo method")).printMessage());
    }
  }

Create testng.xml

Next, let's create testng.xml file in /work/testng/src, to execute test case(s). This file captures your entire testing in XML. This file makes it easy to describe all your test suites and their parameters in one file, which you can check in your code repository or e-mail to coworkers. It also makes it easy to extract subsets of your tests or split several runtime configurations (e.g., testngdatabase.xml would run only tests that exercise your database).

  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
  <suite name="Suite">
    <test thread-count="5" name="Test">
      <classes>
        <class name="TestAnnotationAfterMethod"/>
      </classes>
    </test> <!-- Test -->
  </suite> <!-- Suite -->

Compile the test case using javac.

/work/testng/src$ javac TestAnnotationAfterMethod.java MessageUtil.java

Now, run the testng.xml, which will run the test case defined in <test> tag. As you can see the @AfterMethod is called after all other test cases.

/work/testng/src$ java org.testng.TestNG testng.xml

Verify the output.

executing testMethodOne method
executing afterMethod after each method
executing testMethodTwo method
executing afterMethod after each method

===============================================
Suite
Total tests run: 2, Passes: 0, Failures: 2, Skips: 0
===============================================
Advertisements