如果忽略某些测试(使用@Ignore
注释)并且不想重新发明轮子,我正在尝试找到失败Maven构建的最佳方法。到目前为止,我想到了四个选项:
maven-surefire-plugin
配置为使Maven构建失败。问题是我检查了插件的文档但找不到有关忽略测试的任何内容,所以它甚至可能吗?@Ignore("JIRA ticket ID : simple reason")
即可,但原始@Ignore
不是)。这样的插件是否存在?@Ignore
与@Ignore("JIRA ticket ID...")
区分开来)find ${project.build.testSourceDirectory} -name "*Test.java" | xargs grep -l '^@Ignore\s*$'
是否返回任何文件名,如果是这种情况则失败(也许这是最快,最干净的方法,它可以让我们改进regexp确保JIRA票证与此@Ignore注释相关联)你会做什么?
提前感谢您的帮助!
答案 0 :(得分:0)
这是我自己的问题的答案,也许它可以节省一些时间给其他人。
我最终决定编写一个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 !
优势:
可能的改进: