【问题标题】:How can I fail a maven build if some unit tests are ignored如果忽略某些单元测试,如何使 Maven 构建失败
【发布时间】:2014-12-03 09:13:28
【问题描述】:
如果某些测试被忽略(使用@Ignore 注释)并且不想重新发明轮子,我正在尝试找到使 Maven 构建失败的最佳方法。到目前为止,我想到了四个选项:
- 配置
maven-surefire-plugin 以在某些 JUnit 单元测试被忽略时使 Maven 构建失败。问题是我检查了插件的文档,但找不到任何关于忽略测试的信息,所以有可能吗?
- 如果使用@Ignore 检测到JUnit 单元测试而没有正确注释(使用
@Ignore("JIRA ticket ID : simple reason") 可以,但原始@Ignore 不行),则使用任何其他使构建失败的maven 插件。有这样的插件吗?
- 解析 surefire 结果文件以检测 JUnit 测试已被忽略(恕我直言,这不是最干净的方法,因为这不允许区分
@Ignore 和 @Ignore("JIRA ticket ID..."))
- 使用 Jenkins 预构建步骤检查
find ${project.build.testSourceDirectory} -name "*Test.java" | xargs grep -l '^@Ignore\s*$' 是否返回任何文件名,如果是则失败(也许这是最快、最干净的方法,它允许我们改进正则表达式以确保JIRA 票证与此 @Ignore 注释相关联)
你会怎么做?
提前感谢您的帮助!
【问题讨论】:
标签:
maven
jenkins
surefire
【解决方案1】:
这是我自己问题的答案,也许它可以为其他人节省一些时间。
我最终决定编写一个 shell 脚本来扫描src/test/java 目录(通过TEST_SOURCES_PATH 环境变量提供)并打印出每个@Ignore 注释缺少有效注释的错误。该脚本作为预构建步骤运行。
要求:
检测所有具有@Ignored 测试但没有注释或注释与给定正则表达式不匹配的java 测试文件。如果发现任何错误,则构建失败。
脚本:
#!/bin/sh
# Variables declaration
path=$TEST_SOURCES_PATH
ignoreAnnotationPattern="^\\s*@Ignore"
ignoreCommentDetectionPattern="^\\s*@Ignore\\s*(\\(\"(.*)\"\\))?\\s*$"
validIgnoreCommentPattern="^JIRA\\s[A-Z]*-[1-9][0-9]*\s:\s.*$"
errorsCounter=0
# Change internal field separator to iterate over newlines instead of whitespaces
IFS_backup=$IFS
IFS=$'\n'
# Start detection
echo "Detection of malformed @Ignore annotations in test files started..."
# First find all test files containing an ignored test
testFiles=$(find $path -name "*Test.java" | xargs egrep -l $ignoreAnnotationPattern)
if [ ! -z "$testFiles" ]
then
for testFile in $testFiles
do
# Then keep only lines with the @Ignore annotation
lines=$(egrep $ignoreAnnotationPattern $testFile)
for line in $lines
do
# try to extract the @Ignore comment
if [[ $line =~ $ignoreCommentDetectionPattern ]]
then
# if the comment is not valid then print out a message and increment errors counter
if [[ ! ${BASH_REMATCH[2]} =~ $validIgnoreCommentPattern ]]
then
echo "Test file '"$testFile"' contains a malformed @Ignore annotation: '"$line"'."
errorsCounter=`expr $errorsCounter + 1`
fi
fi
done
done
fi
# Restore initial IFS value
IFS=$IFS_backup
# Exit based on detection result
if [ "$errorsCounter" != 0 ]
then
echo $errorsCounter" errors were found, aborting build !"
exit 1
else
echo "No errors were found, resuming build."
fi
示例输出:
Detection of malformed @Ignore annotations in test files started...
Test file 'src/test/java/MySeventhTest.java' contains a malformed @Ignore annotation: '@Ignore("XXX-999")'.
Test file 'src/test/java/MySecondTest.java' contains a malformed @Ignore annotation: '@Ignore'.
Test file 'src/test/java/MySixthTest.java' contains a malformed @Ignore annotation: '@Ignore("See JIRA XXX-99")'.
Test file 'src/test/java/MyFourthTest.java' contains a malformed @Ignore annotation: '@Ignore("See JIRA XXX-999 : smart reason")'.
Test file 'src/test/java/MyTest.java' contains a malformed @Ignore annotation: '@Ignore("")'.
5 errors were found, aborting build !
优点:
- 在 java 构建之前完成的快速检测(快速反馈)
- 可以轻松修改有效的忽略注释模式
可能的改进:
- 提供有关错误的更多详细信息
- 调用 JIRA Web 服务以检查票证存在且未关闭