java - Junit ant 任务未将断言错误报告为失败
问题描述
我正在尝试将我的 Junit 测试结果集成到我们的 gitlab 管道中。我有一个成功的 ant 构建文件,它使用 Junit 任务和 JUnit 报告任务来创建将与 gitlab 集成的 xml 文件。
我正在使用:java 1.8、ant 1.7.0、junit 4.4
我正在使用本教程中的一个简单示例测试类进行测试(https://www.tutorialspoint.com/junit/junit_basic_usage.html),但我添加了一个 assertTrue(false)。
单元测试:
import org.junit.Test;
import org.junit.Assert.*;
public class MyUnitTest{
@Test
public void testConcatenate() {
MyUnit myUnit= new MyUnit();
String result = myUnit.concatenate("one","two");
assertEquals("twoone", result);
}
@Test
public void testAgain() {
fail();
}
}
我的构建脚本如下所示:
<project name ="JUnitTest" default = "test" basedir = "." >
<property name = "testdir" location = [location of dir} />
<property name = "full-compile" value = "true" />
<path ide - "classpath.test" >
<pathelement location = "junit-4.4.jar" />
<pathelement location = "${testdir}" />
</path>
<target name = compile" >
<javac srcdir = "${srcdir}" destdir = "${testdir}" >
<classpath refid ="classpath.test" />
</javac>
</target>
<target name = "test" depends = "compile" >
<junit haltonfailure ="no" >
<classpath [my classpath here]>
<formatted type = "xml" />
<test name = "MyUnitTest"
todir = "${testdir}" />
</junit>
</target>
我正在使用以下命令运行:
ant -buildfile testBuild.xml
命令行的输出将测试报告为 FAILED。但是,当我查看 xml 报告时,它显示 0 个失败和 1 个错误。当我在命令行中使用 JUnitCore 运行时,这也会报告失败而不是错误。我已验证所有 ort.junit.Assert 语句都会导致断言错误,该错误在测试报告中报告为错误而不是失败。我能找到的大多数文档都说这应该输出失败。fail() 确实导致测试失败。
我希望失败的断言语句导致报告测试失败。这使我能够区分失败的测试或单元测试代码中有错误的测试。有没有办法配置生成的 xml 报告以将断言错误报告为失败?
编辑:我发现一种解决方法是在测试用例周围添加一个 try 块
catch (AssertionError ae) {
fail(ae.toString());
}
但是,从文档中,我读到这应该是不必要的。另外,由于我正在从事一个大型项目,因此将其添加到每个测试中都需要很长时间。此外,我会担心无法捕捉到真正的错误以及在 ant 之外运行的兼容性问题。此外,要找到故障的根源并不容易。所以我希望能找到更好的解决方案。
解决方案
您的 ant 文件中有一些小错误,因此您必须仔细阅读 ant 手册以获取所有信息。
如果你调用你的 ant 文件build.xml
,你可以ant
在不给出-buildfile
标志的情况下调用。
我在我的项目目录中创建了以下目录
build
java
junit-results
lib
test
并将junit-4.4.jar
文件放入lib
目录中。
如果可能,我确实建议使用maven 目录结构,但我猜你有一个旧项目并且无法更改结构。
<project name="JUnitTest" default="test" basedir=".">
<property name="srcdir" location="java" />
<property name="testdir" location="test" />
<property name="classdir" location="build" />
<property name="resultdir" location="junit-results" />
<property name="full-compile" value="true" />
<path id="classpath.test">
<pathelement location="${classdir}" />
<pathelement location="lib/junit-4.4.jar" />
</path>
<target name="compile">
<javac srcdir="${srcdir}" destdir="${classdir}">
</javac>
</target>
<target name="compileTests" depends="compile">
<javac srcdir="${testdir}" destdir="${classdir}">
<classpath refid="classpath.test" />
</javac>
</target>
<target name="test" depends="compileTests">
<junit haltonfailure="no">
<classpath refid="classpath.test" />
<formatter type="xml" />
<test name="MyUnitTest" todir="${resultdir}" />
</junit>
</target>
</project>
推荐阅读
- java - 缺少具有匹配变量名称的 URI 模板变量
- javascript - Next.js API 在错误或没有 HTTP 方法匹配时路由默认响应
- javascript - 将 List 转换为 JSON 数组
- javascript - 有没有办法检测何时使用 JavaScript 在 iPad 上打开底部应用程序托盘
- javascript - 将 Javacript 在 DOM 中所做的更改保存到 html 文件中
- appinsights - TelemetryClient.Context.Device.Id 属性是否记录在 AppInsights 中?
- python - 盐循环.until_no_eval:
- python - 'ForkAwareLocal' 对象没有属性 'connection' python 多处理
- html - 在同一行显示下拉菜单和图标
- java - 为什么java中同时存在Externalizable和Serializable接口?