简体   繁体   English

无法将 RestAssured 的 RQ/RS of Spring Cloud Contract 的测试附加到 Allure 报告

[英]Can't attach RestAssured's RQ/RS of Spring Cloud Contract's tests to Allure report

I've successfully added Allure2 to my project with Spring Cloud Contract tests (it uses JUnit5), but tab "Overview" is blank in all successed test of report.我已经通过 Spring Cloud Contract 测试(它使用 JUnit5)成功地将 Allure2 添加到我的项目中,但是在所有成功的报告测试中选项卡“Overview”都是空白的。

I created a listener class, that gets RQ and RS from RestAssured:我创建了一个侦听器 class,它从 RestAssured 获取 RQ 和 RS:

public class JunitListener extends RunListener {
    public ByteArrayOutputStream request = new ByteArrayOutputStream();
    public ByteArrayOutputStream response = new ByteArrayOutputStream();

    public PrintStream requestVar = new PrintStream(request, true);
    public PrintStream responseVar = new PrintStream(response, true);

    @Override
    public void testStarted(Description description) throws Exception {
        RestAssured.filters(new ResponseLoggingFilter(LogDetail.ALL, responseVar),
                new RequestLoggingFilter(LogDetail.ALL, requestVar));
    }

    @Override
    public void testFinished(Description description) throws Exception {
        logRequest(request);
        logResponse(response);
    }

    @Attachment(value = "Client RQ", type = "plain/text", fileExtension = ".log")
    public byte[] logRequest(ByteArrayOutputStream stream) {
        return attach(stream);
    }

    @Attachment(value = "Client RS", type = "plain/text", fileExtension = ".log")
    public byte[] logResponse(ByteArrayOutputStream stream) {
        return attach(stream);
    }

    public byte[] attach(ByteArrayOutputStream log) {
        byte[] array = log.toByteArray();
        log.reset();
        return array;
    }
}

And runner class, that uses listener class:跑步者 class,它使用监听器 class:

public class JunitRunner extends BlockJUnit4ClassRunner {
    public JunitListener junitListener;

    public JunitRunner(Class<?> klass) throws InitializationError {
        super(klass);
        junitListener = new JunitListener();
    }

    @Override
    public void run(RunNotifier notifier) {
        notifier.addListener(junitListener);
        super.run(notifier);
    }
}

And then I've added runner class to my base test class:然后我将跑步者 class 添加到我的基础测试 class 中:

@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        properties = "server.port=0"
)
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@RunWith(JunitRunner.class)
@AutoConfigureWireMock(port = 8081)
@ActiveProfiles("test")
public abstract class BaseTest {
    
    @LocalServerPort
    int localPort;

    @BeforeEach
    public void setUp(TestInfo testInfo, RestDocumentationContextProvider restDocumentation) {
        RestAssured.baseURI = "http://localhost";
        RestAssured.port = localPort;

        final RequestSpecification idx = new RequestSpecBuilder()
                .setBaseUri("http://localhost")
                .setPort(localPort)
                .addFilter(documentationConfiguration(restDocumentation))
                .build();

        RestAssured.requestSpecification =
                idx.filter(document("contract/" + testInfo.getTestMethod().orElseThrow().getName()));
    }

    @AfterEach
    public void tearDown() {
        RestAssured.reset();
    }
}

But no record was added to my Allure report and, as I see in debugger, content of listener and runner is never used:( What am I doing wrong?但是我的 Allure 报告中没有添加任何记录,而且正如我在调试器中看到的那样,从未使用过侦听器和运行器的内容:(我做错了什么?

I learned that SCC does not use JUnit to run tests, but uses SpringBootTest.我了解到SCC并没有使用JUnit来跑测试,而是使用了SpringBootTest。

So I've created a SBT listener class:所以我创建了一个 SBT 监听器 class:

public class CustomTestExecutionListener implements TestExecutionListener, Ordered {

    public ByteArrayOutputStream request = new ByteArrayOutputStream();
    public ByteArrayOutputStream response = new ByteArrayOutputStream();

    public PrintStream requestVar = new PrintStream(request, true);
    public PrintStream responseVar = new PrintStream(response, true);

    public void beforeTestMethod(TestContext testContext) throws Exception {
        RestAssured.filters(new ResponseLoggingFilter(LogDetail.ALL, responseVar),
                new RequestLoggingFilter(LogDetail.ALL, requestVar));
    }

    public void afterTestMethod(TestContext testContext) throws Exception {
        logRequest(request);
        logResponse(response);
    }

    @Override
    public int getOrder() {
        return Integer.MAX_VALUE;
    }

    @Attachment(value = "Client RQ", type = "text/html")
    public byte[] logRequest(ByteArrayOutputStream stream) {
        return attach(stream);
    }

    @Attachment(value = "Client RS", type = "text/html")
    public byte[] logResponse(ByteArrayOutputStream stream) {
        return attach(stream);
    }

    public byte[] attach(ByteArrayOutputStream log) {
        byte[] array = log.toString().getBytes(StandardCharsets.UTF_8);
        log.reset();
        return array;
    }
}

And added that to my SCC base test class:并将其添加到我的 SCC 基础测试 class 中:

@TestExecutionListeners(
        value = { CustomTestExecutionListener.class },
        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)

Now it works and I have 2 log sections in my Allure report.现在它可以工作了,我的 Allure 报告中有 2 个日志部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生成的测试在Spring Cloud Contract中失败 - Generated tests fail in Spring Cloud Contract Spring Cloud Contract 在合约文件中使用 bodyFromFile 生成弱测试 - Spring Cloud Contract generates weak tests using bodyFromFile in contract file 使用spring-cloud-contract-oa3和spring cloud contract gradle插件时无法生成合约测试 - Unable to generate contract tests when using spring-cloud-contract-oa3 and spring cloud contract gradle plugin 如何在测试中启用Spring的自动配置报告? - How to enable Spring's auto-configuration report in tests? 如何在 spring-cloud-gateway 合约测试中从 spring-cloud-contract 中设置带有 StubRunner 端口的 url - How to set urls with port of StubRunner from spring-cloud-contract in spring-cloud-gateway contract tests 无法使用Spring的DelegatingFilterProxy过滤器部署JAX-RS - can not deploy JAX-RS with spring's DelegatingFilterProxy filter 如何在诱惑报告中附加自定义/现有屏幕截图? - How to attach custom/existing screenshot in allure report? Spring Cloud Contract 测试是否应该实际调用外部服务? - Should Spring Cloud Contract tests actually call an external service? Spring Cloud Contract 测试适用于 Maven,但不适用于 JUnit - Spring Cloud Contract tests work with Maven but not when run with JUnit Spring Cloud合约测试的在线/离线模式存在问题 - Problems with the online / offline mode of the Spring Cloud contract tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM