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

时间:2018-01-19 14:32:55

标签: testng

我想从当前的测试中获得testNG报告输出。

总共报告输出(对于测试的执行已经取得进展)可用于:

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

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

1 个答案:

答案 0 :(得分:1)

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

这是一个完整的示例,展示了这种情况:

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
===============================================