How to obtain the time taken for a method to be executed in TestNG?


TestNG supports native dependency injection. It allows to declare additional parameters in methods. At runtime, TestNG automatically fills these parameters with the correct values. Here is a set of native dependencies in TestNG −

  • ITestContext
  • XmlTest
  • Method
  • ITestResult

These dependencies help to retrieve the time taken by a Test method to execute. The time taken to execute a Test method can be retrieved only after the execution of the test.

If the user wants to get the time taken by the method after its execution, then @AfterMethod can be useful to retrieve it. @AfterMethod supports all these native dependencies. The full access of these dependencies is given below −

AnnotationITestContextXmlTestMethodITestResult
BeforeSuiteYesNoNoNo
BeforeTestYesYesNoNo
BeforeGroupsYesYesNoNo
BeforeClassYesYesNoNo
BeforeMethodYesYesYesYes
TestYesNoNoNo
AfterMethodYesYesYesYes
AfterClassYesYesNoNo
AfterGroupsYesYesNoNo
AfterTestYesYesNoNo
AfterSuiteYesNoNoNo

In this article, we will use ITestResult dependency to show how to retrieve the execution time taken by each test method.

Approach/Algorithm to solve this problem

  • Step 1 − Create a TestNG class NewTestngClass and write @AfterMethod method.

  • Step 2 − Write the following code inside @AfterMethod

public void name(ITestResult result) {
   System.out.println("in aftermethod of NewTestngClass");
   long a = result.getEndMillis()-result.getStartMillis();
   System.out.println("Time taken to run test is :"+a+" miliiseconds");
}
  • Step 3 − Write two different @Test methods in the class, NewTestngClass.

  • Step 4 − Now create the testNG.xml as given below to run the TestNG classes.

  • Step 5 − Finally, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

Use the following code for the common TestNG class, NewTestngClass

src/ NewTestngClass.java

import org.testng.ITestResult;
import org.testng.annotations.*;
public class NewTestngClass {
   // test case 1
   @Test()
   public void testCase1() throws InterruptedException {
      Thread.sleep(5000);
      System.out.println("in test case 1 of NewTestngClass");
   }
   // test case 2
   @Test()
   public void testCase2() throws InterruptedException {
      Thread.sleep(1000);
      System.out.println("in test case 2 of NewTestngClass");
   }
   @AfterMethod
   public void name(ITestResult result) {
      System.out.println("in aftermethod of NewTestngClass");
      long a = result.getEndMillis()-result.getStartMillis();
      System.out.println("Time taken to run test is :"+a+" miliiseconds");
   }
}

testng.xml

This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name = "Suite1">
   <test name = "test1">
      <classes>
         <class name = "NewTestngClass"/>
      </classes>
   </test>
</suite>

Output

in test case 1 of NewTestngClass
in aftermethod of NewTestngClass
Time taken to run test is :5011 miliiseconds
in test case 2 of NewTestngClass
in aftermethod of NewTestngClass
Time taken to run test is :1008 miliiseconds
===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Updated on: 12-Jan-2022

970 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements