How to set the output directory of TestNG @BeforeTest?

TestNGRest AssuredDynamic Programming

TestNG supports default report generation when a user runs testng.xml, either from an IDE or the command line. By default, all reports are generated at the Project -> test-output folder. If the test-output folder is not present, then TestNG creates it at runtime and saves all the files related to the result.

However, the user can provide a desired location or folder name where TestNG should save the reports. It can be done using native dependency injection. It allows to declare additional parameters in methods. At runtime, TestNG automatically fills these parameters with the correct values.

To set up the output directory at @BeforeTest, ITestContext dependency can be used. It creates the folder at a given path or override it, if a folder already exists, to save the reports of the latest run.

In this article, we will use ITestContext dependency to illustrate how to set up an output directory to TestNG @BeforeTest.

Approach/Algorithm to solve this problem

  • Setp 1 − Create a TestNG class, NewTestngClass.

  • Setp 2 − Write the following code inside @BeforeTest in the class;

public void setOutputDirectory(ITestContext context) {
   TestRunner runner = (TestRunner) context;
   String path=System.getProperty("user.dir");
   runner.setOutputDirectory(path+"/output-testng");
}
  • Setp 3 − Write a @Test method in the class, NewTestngClass.

  • Setp 4 − Create the testNG.xml as given below to run the TestNG class.

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

  • Setp 6 − Once the execution is completed, the user can check to ensure whether the output-testng folder has been created at the project path and all the reports are present there for the latest run. In this example, String path=System.getProperty("user.dir"); fetches the absolute path where the TestNG project is present and then adds the folder. The user can provide any path to generate reports in that folder.

Example

Use the following code for the common TestNG class, NewTestngClass

src/ NewTestngClass.java

import org.testng.ITestContext;
import org.testng.TestRunner;
import org.testng.annotations.*;
public class NewTestngClass {
   @Test()
   public void testcase1(ITestContext testContext){
      System.out.println("Thread ID: "+Thread.currentThread().getId());
      int currentCount = testContext.getAllTestMethods()[0].getCurrentInvocationCount();
      System.out.println("Executing count: " + currentCount);
   }
   @BeforeTest
   public void setOutputDirectory(ITestContext context) {
      TestRunner runner = (TestRunner) context;
      String path=System.getProperty("user.dir");
      runner.setOutputDirectory(path+"/output-testng");
   }
}

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"/>ng="testng">
      </classes>
   </test>
</suite>

Output

Thread ID: 12
Executing count: 0
===============================================
Suite1
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
raja
Updated on 12-Jan-2022 13:14:37

Advertisements