David
David

Reputation: 197

Obtain testNG reporter output from the current test only

I would like to obtain testNG reporter output from the current test only.

Reporter output in total (for as far as execution of test(s) have progressed) is available with:

        List<String> reporterOutputSoFar = Reporter.getOutput();
        System.out.println("reporterOutputSoFar:" + reporterOutputSoFar);

Is there a way to achieve this?

Upvotes: 0

Views: 947

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

Yes you can do it easily via Reporter.getOutput(tr) where tr represents the ITestResult object of a particular @Test method.

Here's a complete example that shows how this would look like:

import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

public class ReporterSample {
    private String text;
    private Map<String, ITestResult> data = new ConcurrentHashMap<>();

    @Test
    public void one() {
        Reporter.log("one");
        data.put("one", Reporter.getCurrentTestResult());
    }

    @Test(dependsOnMethods = "one")
    public void two() {
        Reporter.log("two");
        data.put("two", Reporter.getCurrentTestResult());
    }

    @Test(dependsOnMethods = "two")
    public void three() {
        Reporter.log("three");
        text = Reporter.getOutput().stream().collect(Collectors.joining(","));
        data.put("three", Reporter.getCurrentTestResult());
    }

    @AfterClass
    public void afterClass() {
        System.err.println("Total output as string [" + text + "]");
        String methodThreeoutput = Reporter.getOutput(data.get("three")).stream().collect(Collectors.joining());
        System.err.println("Output of method three() = [" + methodThreeoutput + "]");
    }
}

Here's the output

Total output as string [one,two,three]

Output of method three() = [three]

===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

Upvotes: 1

Related Questions