TeamCity中使用EMMA

TeamCity对于JAVA的代码覆盖率我觉得弄得不好。

TeamCity开启EMMA测试会“智能”的对运行测试的Build Step加入部分步骤来完成代码覆盖的统计。其中有一个Coverage instrumentation parameters:可以设置,这个将是我们折腾的核心,一开始在使用它的默认值-ix -*Test*

注:如果开启EMMA后运行运行测试抛出java.lang.VerifyError异常可以参考,上一篇文章《java.lang.VerifyError运行时异常及处理方法

我们来看一下TeamCity开启EMMA后会做些什么

在编译后TeamCity会自动帮我们加入EMMA: Instrumenting classes

具体行为类似

   1: [08:29:22][compile] EMMA: Instrumenting classes
   2: [08:29:22][EMMA: Instrumenting classes] EMMA commandline: [-ix, -*Test*, -cp, C:\TeamCity\buildAgent\work\7c011ec2a3494774\test\UnitTest\build, -m, overwrite]
   3: [08:29:22][EMMA: Instrumenting classes] EMMA: processing instrumentation path ...
   4: [08:29:22][EMMA: Instrumenting classes] EMMA: instrumentation path processed in 47 ms
   5: [08:29:23][EMMA: Instrumenting classes] EMMA: [3 class(es) instrumented, 0 resource(s) copied]
   6: [08:29:23][EMMA: Instrumenting classes] EMMA: metadata merged into [C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage.em] {in 60 ms}

核心当然是EMMA commandline部分

之后在运行代码后会加入EMMA: Create Report和三个Publishing artifacts过程。具体行为类似于

   1: [08:29:24][Step 2/3] EMMA: Create Report
   2: [08:29:24][EMMA: Create Report] EMMA commandline: [-r, html,txt, -in, C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage.em,C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage.ec, -Dreport.html.out.file=coverage/index.html, -Dreport.txt.out.file=C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage.txt, -sp, C:\TeamCity\buildAgent\work\7c011ec2a3494774\test\UnitTest\src]
   3: [08:29:24][EMMA: Create Report] EMMA: processing input files ...
   4: [08:29:24][EMMA: Create Report] EMMA: 2 file(s) read and merged in 1 ms
   5: [08:29:24][EMMA: Create Report] EMMA: writing [html] report to [C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage\index.html] ...
   6: [08:29:24][EMMA: Create Report] EMMA: writing [txt] report to [C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage.txt] ...
   7: [08:29:24][Step 2/3] Process exited with code 0
   8: [08:29:24][Step 2/3] Publishing artifacts (1s)
   9: [08:29:24][Publishing artifacts] Collecting files to publish: [C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage.ec=>.teamcity/coverage_emma]
  10: [08:29:26][Publishing artifacts] Sending coverage.ec
  11: [08:29:24][Step 2/3] Publishing artifacts (1s)
  12: [08:29:24][Publishing artifacts] Collecting files to publish: [C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage.em=>.teamcity/coverage_emma]
  13: [08:29:26][Publishing artifacts] Sending coverage.em
  14: [08:29:24][Step 2/3] Publishing artifacts (1s)
  15: [08:29:24][Publishing artifacts] Collecting files to publish: [C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage.txt=>.teamcity/coverage_emma]
  16: [08:29:26][Publishing artifacts] Sending coverage.txt
  17: [08:29:24][Step 2/3] Publishing artifacts (1s)
  18: [08:29:24][Publishing artifacts] Collecting files to publish: [C:\TeamCity\buildAgent\work\7c011ec2a3494774\coverage\coverage.zip=>.teamcity/coverage_emma]
  19: [08:29:26][Publishing artifacts] Sending coverage/coverage.zip

 

有了这些日志我们就可以根据自己的需要进行部分修改了。

比如说我将源代码的编译放在一个build step中,而测试代码的编译和运行放在另外一个build step中。这样在默认情况下就无法完成正确的Instrumenting classes

所以我将Coverage instrumentation parameters修改成-ix -*Test* -cp %teamcity.build.workingDir%\WebContent\WEB-INF\classes。

还有更极限的做法应该可以再ANT中直接完成EMMA的工作,而不要让TeamCity自作聪明的去处理

你可能感兴趣的:(it)