How to turn off TestNG's default reporters programmatically?

TestNGRest AssuredDynamic Programming

TestNG allows to run the test suites from IntelliJ IDE as well as from the command line. When user runs the testNG.xml either from an IDE or the command line, TestNG generates a default report. It saves all the reports and respective HTML files in the Project->test-output folder. If this folder does not exist, then TestNG creates one.

To disable the default reports programmatically, TestNG should be run through the command line (cmd).

Following are the prerequisites to run the test suites from the command line −

  • testNG.xml file should be created to define the test suites and testing classes to execute.

  • All the dependent jars should be available inside a project folder. It includes testing.jar, jcommander.jar and any other jars that are used in the test cases.

  • The Path of bin or out folder where .class files are stored after compilation.

Approach/Algorithm to solve this problem −

  • Step 1 − Create different testing classes having different @Test methods and one @BeforeSuite method.

  • Step 2 − Compile the class. It will create an out folder in IntelliJ and bin folder in Eclipse.

  • Step 3 − Place all the jar files in the lib folder.

  • Step 4 − Now create the testNG.xml as given below.

  • Step 5 − Open the cmd.

  • Step 6 − Navigate to the project path using cd <project_path>

  • Step 7 − Run the following command:

java -cp <path of lib;> <path of out or bin folder> org.testng.TestNG <path of testng>/testng.xml

Example

The following code shows how to disable the default report generation at runtime −

src/ NewTestngClass.java

import org.testng.TestNG;
import org.testng.annotations.*;
public class NewTestngClass {
   // test case 1
   @Test()
   public void testCase1() throws InterruptedException {
      System.out.println("in test case 1 of NewTestngClass");
   }
   // test case 2
   @Test()
   public void testCase2() throws InterruptedException {
      System.out.println("in test case 2 of NewTestngClass");
   }
   @BeforeSuite
   public void name() {
      TestNG myTestNG = new TestNG();
      myTestNG.setUseDefaultListeners(false);
      System.out.println("Default reports are disabled");
   }
}

src/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>

Command to run −

java -cp .\lib\*;.\out\production\TestNGProject org.testng.TestNG src\testng.xml

Output

Default reports are disabled
in test case 1 of NewTestngClass
in test case 2 of NewTestngClass

===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
raja
Updated on 09-Mar-2022 11:14:00

Advertisements