Spock中TestNG Reporter.getOutput()的类比。

问题描述 投票:0回答:1

我使用Groovy和Spock.Requestresponse数据运行API测试,由第三方库产生的数据,出现在系统出(我在Jenkins日志中看到它)。

问题是 什么是正确的方式来开始-停止系统出记录每个测试迭代到一些字符串列表?

TestNG有Reporter.getOutput(result),它返回所有日志条目,出现在测试迭代运行时。Spock中也有类似的功能吗?

我假设它应该是一些实现的 运行监听器 我在哪里开始录制 beforeIteration() 并将其附在报告中 afterIteration()?

testing logging automated-tests spock
1个回答
0
投票

解决的方法是使用 输出捕捉春季开机测试

RunListener的样本。

class RunListener extends AbstractRunListener {

    OutputCapture outputCapture

    void beforeSpec(SpecInfo spec) {
        Helper.log "[BEFORE SPEC]: ${spec.name}"
        outputCapture = new OutputCapture()
        outputCapture.captureOutput() //register a copy off system.out, system.err streams
    }

    void beforeFeature(FeatureInfo feature) {
        Helper.log "[BEFORE FEATURE]: ${feature.name}", 2
    }

    void beforeIteration(IterationInfo iteration) {
        outputCapture.reset() //clear the stream copy before each test iteration 
    }

    void afterIteration(IterationInfo iteration) {
    }

    void error(ErrorInfo error) {
        //attach the content of copy stream object to the report if  test iteration failed
        Allure.addAttachment("${error.method.iteration.name}_console_out", "text/html", outputCapture.toString(), "txt") 

    }

    void afterFeature(FeatureInfo feature) {
    }

    void afterSpec(SpecInfo spec) {
        outputCapture.releaseOutput()
    }
}

Shortly:

CaptureOutput是一个输出流的实现,它同时记录了初始输出和复制流对象。复制流在 beforeIteration() 并附在本报告后。afterIteration()RunListener,所以每个测试都会收到自己的部分输出。

© www.soinside.com 2019 - 2024. All rights reserved.