- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
Annotation | ITestContext | XmlTest | Method | ITestResult |
---|---|---|---|---|
BeforeSuite | Yes | No | No | No |
BeforeTest | Yes | Yes | No | No |
BeforeGroups | Yes | Yes | No | No |
BeforeClass | Yes | Yes | No | No |
BeforeMethod | Yes | Yes | Yes | Yes |
Test | Yes | No | No | No |
AfterMethod | Yes | Yes | Yes | Yes |
AfterClass | Yes | Yes | No | No |
AfterGroups | Yes | Yes | No | No |
AfterTest | Yes | Yes | No | No |
AfterSuite | Yes | No | No | No |
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 ===============================================
- Related Articles
- How can a particular group of test cases get executed in TestNG?
- How to set the script to be executed asynchronously in HTML?
- How to skip a particular test method in TestNG?
- How to specify method name sequence in TestNG?
- How to measure the time taken by a function in Java?
- How to measure execution time for a Java method?
- How to measure time taken by a function in C?
- How to overlook a particular test method from execution in TestNG?
- How to get the name of a test method that was run in a TestNG teardown method?
- Find time taken for signal to reach all positions in a string - C++
- Find time taken for signal to reach all positions in a string in C++
- How will you run a prerequisite method and post condition method for\nevery test in TestNG?
- What should be taken away from (6m2 $-$ 4n2 $+$ 3mn $+$ 15) to obtain ($-$m2 $-$ n2 $+$ 3mn $+$ 15)?
- How will you test a leaf for starch, also mention precautions to be taken.
- How to attach a function to be executed before an Ajax request is sent using jQuery?
