1

I need to retrieve test method status, actual result, expected result & other attributes in IReporter implementation. Is it possible to use ITestResult to pass required data to generateReport() method in IReporter implementation? Any other suggestion would be appreciated.

Sample test class as follows,

 public void methodOne() {

        //Values I want to pass to generateReport
        String testString01;
        String testString02;
        String testString03;

        //Status of the method one - pass/fail
        Assert.assertTrue(true);
    }

testNG xml as follows,

<?xml version="1.0" encoding="UTF-8"?>
<suite name="TestRunner" verbose="1" configfailurepolicy="continue">

    <listeners>
        <listener class-name="com.wsdscd.util.TestNGEmailableReport2"/>
    </listeners>
    <test name="TestRunner">
        <classes>
            <class name="com.dsfscce.tests.poc.POC"></class>
        </classes>
    </test>
</suite>

IReporter implementation as follows,

public class TestNGEmailableReport2 implements IReporter {

    @Override
    public void generateReport(List<XmlSuite> xml, List<ISuite> suites, String outdir) {

//required to retrieve data method data here
//required to retrive suite information here 
}}
1

For example using this way:

class MyReporter implements IReporter {
    @Override
    public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
        List<ITestContext> testContexts = suites
                .stream()
                .flatMap(iSuite -> iSuite.getResults().values().stream())
                .map(iSuiteResult -> iSuiteResult.getTestContext())
                .collect(Collectors.toList());
        for(ITestContext testContext: testContexts){
            System.out.printf("Suite: " + testContext.getSuite().getName());
            Set<ITestResult> failedTests = testContext.getFailedTests().getAllResults();
            for(ITestResult iTestResult: failedTests){
                System.out.println("This test failed:" + iTestResult.getName();
            }
        }
        
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.