Maven 错误:/target/surefire-reports for the individual test results.

本文详细解析了Maven构建过程中测试失败导致的BUILDFAILURE错误,提供了具体的错误日志及原因分析,并给出了在pom文件中配置maven-surefire-plugin插件以忽略测试失败继续编译的解决方案。

错误日志

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.081 s
[INFO] Finished at: 2018-11-05T10:29:22+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project p2p-thirty: There are test failures.
[ERROR] 
[ERROR] Please refer to /Users/shanglishuai/ciyun-space/santian-p2p/p2p-thirty/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :p2p-thirty

解决方案

在pom文件中添加以下maven插件

<!-- 该插件用于测试出错并不影响项目的编译 -->
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
		<configuration>
				<testFailureIgnore>true</testFailureIgnore>
		</configuration>
</plugin>
Maven 项目中,测试结果通常由 Surefire 插件生成并保存在 `target/surefire-reports` 目录下。该目录包含每个测试类的详细输出文件,这些文件可以帮助开发人员定位和理解单个测试的结果。 ### 测试结果文件的结构 在 `target/surefire-reports` 目录中,Maven 会为每个测试类生成两个主要类型的文件: 1. **文本格式文件**:以 `.txt` 扩展名结尾,包含完整的测试执行日志,包括标准输出和标准错误流的内容。 2. **XML 格式文件**:以 `.xml` 扩展名结尾,提供机器可读的测试结果,通常用于集成到持续集成工具(如 Jenkins、GitLab CI)或报告系统中[^1]。 例如,如果有一个测试类名为 `com.example.MyTest`,则可能会看到以下文件: - `com.example.MyTest.txt` - `com.example.MyTest.xml` ### 理解 `.txt` 文件内容 `.txt` 文件记录了测试运行期间的所有控制台输出,包括 `System.out.println()` 和 `System.err.println()` 的调用结果。这对于调试失败的测试非常有用,因为可以查看详细的日志信息,从而找出问题所在[^1]。 ### 理解 `.xml` 文件内容 `.xml` 文件遵循特定的 XML Schema,描述了测试套件的执行情况,包括测试用例的总数、失败的测试数量、跳过的测试数量以及每个测试方法的详细结果。一个典型的 `.xml` 文件内容可能如下所示: ```xml <testsuite name="com.example.MyTest" tests="3" failures="1" errors="0" skipped="0" time="0.123"> <testcase name="testMethodOne" classname="com.example.MyTest" time="0.045"/> <testcase name="testMethodTwo" classname="com.example.MyTest" time="0.034"> <failure message="Expected 5 but got 6">...</failure> </testcase> <testcase name="testMethodThree" classname="com.example.MyTest" time="0.044"/> </testsuite> ``` 在这个例子中,`testMethodTwo` 失败了,并提供了具体的失败消息“Expected 5 but got 6”[^1]。 ### 使用工具解析测试结果 除了手动检查这些文件外,还可以使用各种工具来解析和展示测试结果。例如,Jenkins 可以自动收集这些 XML 文件,并生成可视化的测试报告,显示通过率、失败原因等信息。此外,IDE(如 IntelliJ IDEA 或 Eclipse)也支持直接打开 `.txt` 文件,并以友好的方式展示测试输出。 ### 自定义 Surefire 报告目录 如果需要更改默认的报告目录,可以在项目的 `pom.xml` 文件中配置 Surefire 插件,指定不同的输出路径: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <reportsDirectory>${project.build.directory}/custom-surefire-reports</reportsDirectory> </configuration> </plugin> </plugins> </build> ``` 这样,所有测试结果将被保存到新的目录 `target/custom-surefire-reports` 中[^1]。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值