当 JaCoCo 确定没有值得运行的测试时,Ant 构建会不必要地失败



对于持续集成构建,我们使用JcCoCo来最小化要运行的测试数量。 但是,在某些提交中,它确定根本没有值得运行的测试。 例如,如果仅更改了图像。

以下是build.xml的片段:

<fileset id="int.tests" dir="${build.inttest.source}/java">
  <include name="**/*Test.java"/>
</fileset>
<taskdef name="testng" classname="org.testng.TestNGAntTask"
  classpath="${build.jars.test}/testng.jar"/>
<jacoco:coverage destfile="./inttest-jacoco.exec">
    <testng outputDir="./reports/intTest" failureproperty="testNGFailed" haltonfailure="false"
      verbose="2" workingDir="${build.dir}" classfilesetref="int.tests">
      <classpath>
        <path refid="build.inttest.classpath"/>
      </classpath>
    </testng>
</jacoco:coverage>

如果未运行任何测试,则属性testNGFailed设置为 true,后续生成将失败。

此方案中的日志记录如下所示:

13:16:49,116 INFO  -    [testng] ===============================================
13:16:49,116 INFO  -    [testng] Ant suite
13:16:49,116 INFO  -    [testng] Total tests run: 0, Failures: 0, Skips: 0
13:16:49,116 INFO  -    [testng] ===============================================
13:16:49,116 INFO  - 
13:16:49,191 WARN  -    [testng] [TestNG] No tests found. Nothing was run

当没有要运行的测试时,如何使构建通过,但在任何测试失败时失败?

我可以让Jacoco始终至少运行一次测试吗?

我可以让 TestNG 仅在测试失败时设置失败属性吗?

首先 - JaCoCo 不执行您的测试!测试由TestNG或JUnit或任何测试框架执行 - JaCoCo只是从此执行中收集覆盖率信息。

根据 http://testng.org/doc/ant.html:

classfilesetref - 对包含要运行的测试的资源集合的引用

而在您的情况下,它指向包含.java文件的目录,因此 TestNG 找不到测试似乎是合乎逻辑的。

最新更新