我有一个 JUnit5 集成测试项目,我在其中并行运行测试类。我的问题是我的日志消息在测试类完成执行后生成的 junit XML 报告中混合在一起。这使得调试失败的根本原因变得困难,因为有时包含失败测试的 xml 甚至没有失败测试的系统输出部分,因为另一个 xml 报告已经抓住了它们。
此 xml 报告显示了 4 个线程中的 3 个的系统输出。我如何得到它,以便它只在 xml 报告中显示正确的线程?
<testcase name="beforeEachFailure" classname="tests.BeforeEachTests" time="32.962">
<failure type="java.lang.AssertionError">
<![CDATA[
java.lang.AssertionError
at tests.BeforeEachTests.beforeEach(BeforeEachTests.java:19)
]]>
</failure>
<system-out>
<![CDATA[
10:45:23.227 [Thread-0] INFO StaticUtil - Sleep for 3s
10:45:23.228 [Thread-0] INFO BeforeAllTests - continue doing a thing
10:45:26.228 [Thread-1] INFO StaticUtil - Sleep for 3s
10:45:29.228 [Thread-1] INFO StaticUtil - Sleep for 3s
10:45:32.229 [Thread-1] INFO StaticUtil - Sleep for 3s
10:45:32.229 [Thread-1] INFO TestTests - half the things done
10:45:35.230 [Thread-2] INFO StaticUtil - Sleep for 3s
10:45:38.231 [Thread-2] INFO StaticUtil - Sleep for 3s
]]>
</system-out>
</testcase>
maven-surefire-plugin 的配置:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<statelessTestsetReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5Xml30StatelessReporter">
<version>3.0</version>
<usePhrasedTestCaseMethodName>true</usePhrasedTestCaseMethodName>
</statelessTestsetReporter>
<consoleOutputReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5ConsoleOutputReporter">
<encoding>UTF-8</encoding>
</consoleOutputReporter>
<statelessTestsetInfoReporter implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoReporter">
<usePhrasedClassNameInTestCaseSummary>true</usePhrasedClassNameInTestCaseSummary>
</statelessTestsetInfoReporter>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<reportsDirectory>${basedir}/target/surefire-reports</reportsDirectory>
<includes>
<include>**/*Test*.java</include>
</includes>
<properties>
<configurationParameters>
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = same_thread
junit.jupiter.execution.parallel.mode.classes.default = concurrent
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = 4
</configurationParameters>
</properties>
</configuration>
</plugin>
</plugins>
log4j2 配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %c{1} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>