Search code examples
javatestngreportng

Java - Turn off TestNG's default reporters programmatically


I have an application where I need to execute my TestNG @Test annotated methods from my main method, and use ReportNG to generate reports.

My main method looks like this:

public static void main(String args[])
{
    TestNG myTestNG = new TestNG();
    XmlSuite mySuite = new XmlSuite();
    mySuite.setName("Sample Suite");
    mySuite.addListener("org.uncommons.reportng.HTMLReporter");
    mySuite.addListener("org.uncommons.reportng.JUnitXMLReporter");
    XmlTest myTest = new XmlTest(mySuite);
    myTest.setName("Sample Test");
    List<XmlClass> myClasses = new ArrayList<XmlClass>();
    myClasses.add(new XmlClass("com.varunmulloli.testng.SampleTest"));
    myTest.setXmlClasses(myClasses);
    List<XmlTest> myTests = new ArrayList<XmlTest>();
    myTests.add(myTest);
    mySuite.setTests(myTests);
    List<XmlSuite> mySuites = new ArrayList<XmlSuite>();
    mySuites.add(mySuite);
    myTestNG.setXmlSuites(mySuites);
    myTestNG.run();
}

And my SampleTest class looks like this:

public class SampleTest 
{
    @Test
    public void testSample() 
    {
        String str = "TestNG is working fine";
        assertEquals("TestNG is working fine", str);
    }
}

The code runs fine, but in the test-output folder, I get the reports generated by the default TestNG reporter as well along with the ReportNG reports. How do I disable the default reporters programmatically?

Turning off test-output in TestNG - This question is similar to mine, but it does not discuss about disabling it programmatically.


Solution

  • Found the answer.

    myTestNG.setUseDefaultListeners(false);