2014-12-03 54 views
0

如果忽略某些测试(使用@Ignore注释)并且不想重新发明方向盘,我试图找到失败Maven构建的最佳方法。到目前为止,我想到了四个选项:如果某些单元测试被忽略,我该如何失败maven版本

  1. 配置maven-surefire-plugin失败一个Maven构建,如果一些JUnit测试被忽略。问题是我检查了插件的文档,但找不到任何关于忽略的测试,甚至有可能?
  2. 如果使用@Ignore检测到没有适当注释的JUnit单元测试(使用@Ignore("JIRA ticket ID : simple reason")没问题,但是原始的@Ignore没有),则使用任何其他未通过构建的maven插件。这样的插件是否存在?
  3. 为了检测JUnit测试解析万无一失的结果文件都被忽略(恕我直言没有,因为这最干净的方式不允许区分@Ignore@Ignore("JIRA ticket ID...")
  4. 使用詹金斯预生成一步,如果find ${project.build.testSourceDirectory} -name "*Test.java" | xargs grep -l '^@Ignore\s*$'返回该检查任何文件名,并且如果是这种情况则失败(可能是最快,最干净的方式,并且它允许我们改进正则表达式以确保JIRA票据与此@Ignore注释相关联)

你会做什么?

非常感谢您的帮助!

回答

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 ! 

优点:

  • 快速检测在Java之前完成建立(快速反馈)
  • 有效忽略意见式样可以轻松地修改

可能的改进:

  • 提供有关错误的更多细节
  • ,以检查呼叫JIRA Web服务该票存在并且未关闭
+0

听起来像Checkstyle的工作? – khmarbaise 2014-12-04 08:46:11