【发布时间】:2018-06-06 12:44:13
【问题描述】:
如何指示 JUnit(通过 Ant)在测试套件中为每个测试捕获 sdout 和 stderr?这甚至可能吗?
假设我有一个简单的 Ant 任务,它运行一套详细的 JUnit 测试。
<target name="integration-tests" depends="init">
<property name="test.reports" value="${build.dir}/test-reports" />
<mkdir dir="${test.reports}" />
<junit showoutput="true" printsummary="yes" fork="yes">
<formatter type="xml" />
<classpath>
<pathelement path="${run.test.classpath}" />
</classpath>
<test name="org.example.IntegrationTestsTestSuite" todir="${test.reports}" />
</junit>
</target>
当 CI(在我的例子中是 Jenkins)调用这个目标时,JUnit 会生成一个整洁的 XML 文件供 CI 进一步处理(发布)。不幸的是,所有的 stdout 和 stderr 都合并为两个 XML 元素的值。
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="0" failures="0" hostname="foo-host" name="org.example.IntegrationTestsTestSuite" skipped="0" tests="3" time="0.115" timestamp="2018-06-06T11:45:45">
<properties>
<!-- all properties used by Ant build script here -->
</properties>
<testcase classname="org.example.integration.tests.CommonStuff" name="testSomeFooDoingAThing" time="0.088" />
<testcase classname="org.example.integration.tests.CommonStuff" name="testOtherFooDoingAThing" time="0.023" />
<testcase classname="org.example.integration.tests.CommonStuff" name="testAnotherFooDoingAThing" time="0.004" />
<system-out><![CDATA[]]></system-out>
<system-err><![CDATA[]]></system-err>
</testsuite>
如果某个测试出现问题,CI 会立即显示整个详细输出,即使是成功的测试(查看单个失败的测试时)也是如此,从而违背了此类输出的预期目的。
在我们的 IDE (Netbeans) 中运行测试时也存在同样的问题。但我记得在其他项目中,至少 IntelliJ 能够在每个测试中显示如此详细的输出(但不确定它是否适用于测试套件)。 IntelliJ 是在做其他事情来发布/预处理输出还是这是 JUnit 功能?
【问题讨论】:
-
IntelliJ 捕获 std(out/err) 流并将它们拆分为单独的文本字段,它对您几乎没有帮助。看看 JUnit 的“@Rule”和“@BeforeClass”注解。
-
@MikhailKholodkov,你在这里的意思是,如果需要这样的东西,它需要由测试本身来处理。也许使用TestWatcher,使其将std保存到文件中,然后以某种方式将此文件链接到特定的测试用例?甚至可能以编程方式生成测试报告。