仅从当前测试获取 testNG 报告器输出



我只想从当前测试中获取testNG报告器输出。

报告器输出总数(就测试执行进度而言(可用于:

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

有没有办法实现这一目标?

是的,您可以通过Reporter.getOutput(tr)轻松完成,其中tr表示特定@Test方法的ITestResult对象。

下面是一个完整的示例,显示了它的外观:

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 + "]");
    }
}

这是输出

Total output as string [one,two,three]
Output of method three() = [three]
===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================

最新更新