Integration Tests with Maven (Part 2): Test Coverage Reports

June 23rd, 2009 By velo

In the previous article, we demonstrated one way of running integration tests with Maven. In this article, we will demonstrate how to measure the test coverage of both integration and unit tests.

We will be using Emma to measure and report the coverage. EMMA is an open-source toolkit for measuring and reporting Java code coverage.

Unit test coverage

Let’s start by generating a coverage report for unit tests only. This will require 2 new plugins to be added to our pom. The first one:

37
38
39
40
41
42
43
44
45
46
47
48
49
<plugin>
  <groupId>org.sonatype.maven.plugin</groupId>
  <artifactId>emma-maven-plugin</artifactId>
  <version>1.1</version>
  <executions>
    <execution>
      <phase>process-classes</phase>
      <goals>
        <goal>instrument</goal>
      </goals>
    </execution>
  </executions>
</plugin>

This plugin will instrument classes at target/generated-class/emma/classes, this won’t change the original classes at target/classes. Then, we must tell surefire plugin to look for classes at new location:

51
52
53
54
55
56
57
58
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.4.3</version>
  <configuration>
    <classesDirectory>${project.build.directory}/generated-classes/emma/classes</classesDirectory>
  </configuration>
</plugin>

At this point, we do 50% of code coverage. Mainly because the psvm ( public static void main ) is never executed.

Here is the EMMA text report. Emma does produce xml and html reports in addition to this simple text report:

[EMMA v2.0.5312 report, generated Fri Jun 12 15:43:28 BRT 2009]
-------------------------------------------------------------------------------
OVERALL COVERAGE SUMMARY:
 
[class, %]	[method, %]	[block, %]	[line, %]	[name]
100% (1/1)	67%  (2/3)!	56%  (10/18)!	50%  (4/8)!	all classes

Note: at this point no report is generated, we will configure report generation after integration tests coverage reports are in place. Once you run Surefire, you may notice the coverage.ec in the project root directory. This file contains the coverage result in a binary format used by Emma.

Integration test coverage

So far, we have the unit tests results. As I demonstrated in the previous article in this series, the integration tests need the project artifact to run, so, we need to instrument this jar in order to get coverage result on ITs. Let’s see the pom:

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<plugin>
  <groupId>org.sonatype.maven.plugin</groupId>
  <artifactId>emma4it-maven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>instrument</id>
      <goals>
        <goal>instrument-project-artifact</goal>
      </goals>
      <configuration>
        <appendEmma>true</appendEmma>
      </configuration>
    </execution>
  </executions>
</plugin>

Note that is using a different plugin: emma4it-maven-plugin. This plugin provides some additional goals for instrumenting integrations tests. For this project we use instrument-project-artifact goal. This goal will add Emma instrumentation into the project jar. This is specific to tests running on integration-test phase, because it does requires that the packaged artifact be instrumented.

There is also the configuration appendEmma. When true, this goal will shade the Emma jar into the instrumented jar. This is very useful to test jars that will be launched from command line, like this project.

[EMMA v2.0.5312 report, generated Fri Jun 12 16:05:45 BRT 2009]
-------------------------------------------------------------------------------
OVERALL COVERAGE SUMMARY:
 
[class, %]	[method, %]	[block, %]	[line, %]	[name]
100% (1/1)	100% (3/3)	89%  (16/18)	88%  (7/8)	all classes

At this point we have a very decent code coverage, almost 90%, 7 of 8 lines of code, and on this project it is not possible to get 8/8. And why is that? System.exit(). when this line is invoked nothing else is executed, that includes Emma instrumented code, so Emma won’t be able to know if System.exit() was executed, our integration tests catch that, because we check the exit code of the process, but Emma can’t do the same.

Test Coverage Report

At this point we have all coverage data stored at coverage.ec, in Emma binary format. Let’s now convert that into something that is more readable.

Add the following execution on emma4it-maven-plugin:

74
75
76
77
78
79
80
81
82
83
84
85
86
87
<execution>
  <id>report</id>
  <phase>post-integration-test</phase>
  <goals>
    <goal>report</goal>
  </goals>
  <configuration>
    <sourceSets>
      <sourceSet>
        <directory>${project.build.sourceDirectory}</directory>
      </sourceSet>
    </sourceSets>
  </configuration>
</execution>

Now the text, HTML, and XML reports are generated at target/site/emma.

Emma HTML coverage report

Emma HTML coverage report

That is it for part 2. On the next part, I will show some ideas about testing more complex projects like flexmojos and nexus.

 

How-To, Maven , ,

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

  1. February 18th, 2010 at 13:40 | #1

    Понравилась темка, очень познавательно 5м

  2. February 25th, 2010 at 08:55 | #2

    Постарался атор над подбором темок, респект 6ц

  3. February 25th, 2010 at 13:16 | #3

    Мне очень понравились статьи, актуально 7ы

  4. February 26th, 2010 at 00:55 | #4

    Мне очень понравилось направление темы 0о

  5. February 26th, 2010 at 02:35 | #5

    Надо побольше таких сайтов 5п

  6. February 26th, 2010 at 03:06 | #6

    Очень понравилась темка 4а

  7. February 26th, 2010 at 05:33 | #7

    Получил удовольствие от прочтения 7р

  8. February 26th, 2010 at 05:44 | #8

    С огромным удовольствием просмотрел 7р

  9. February 26th, 2010 at 08:40 | #9

    Много интересных и актуальных моментов затронуто 6п

  10. February 26th, 2010 at 08:44 | #10

    Кое-что хотелось бы уточнить 7н

  11. February 26th, 2010 at 14:29 | #11

    Получил удовольствие от материала 7а

  12. February 27th, 2010 at 01:33 | #12

    Мне понравился материал и его компановка 9ш

  13. February 27th, 2010 at 01:37 | #13

    Очень понравилось, только некоторые моменты хотел уточнить 9о

  14. February 27th, 2010 at 03:41 | #14

    Здорово все получилось 9у

  15. February 27th, 2010 at 03:44 | #15

    Очень нравится авторский подход 6г

  16. February 27th, 2010 at 06:24 | #16

    Мне очень понравилось 6е

  17. February 27th, 2010 at 06:26 | #17

    Очень понравился стиль 9ш

  18. February 27th, 2010 at 08:37 | #18

    Получил удовольствие от подобранного материала 9ш

  19. February 27th, 2010 at 08:40 | #19

    Хорошо поработал автор 8г

  20. February 28th, 2010 at 01:35 | #20

    С нетерпением жду продолжения 9л

  21. February 28th, 2010 at 03:53 | #21

    Очень оригинальный материальчик попался 0ы

  22. February 28th, 2010 at 11:42 | #22

    Мне понравился стиль и подборочка неплохая 0а

  23. March 1st, 2010 at 00:43 | #23

    С удовольствием отмечу отличную подборку статей у автора 4п

  24. March 1st, 2010 at 03:12 | #24

    Получил удовольствие от прочитанного материала 3а

  25. March 1st, 2010 at 05:43 | #25

    Мне понравился подобранный материал, зачетно 0п

  26. March 1st, 2010 at 09:58 | #26

    Очень прикольно читать подборочку, респект автору 5п

  27. March 1st, 2010 at 12:33 | #27

    Некоторые моменты только хотелось бы уточнить 8п

  28. March 2nd, 2010 at 02:00 | #28

    Зачетный сайт, жду новенького материала 8ф

  29. March 2nd, 2010 at 02:04 | #29

    Респект автору, хорошо поработал 9ы

  30. March 2nd, 2010 at 04:53 | #30

    Мне очень понравилось, но некоторые моменты хочу уточнить 0ф

  31. March 2nd, 2010 at 04:57 | #31

    Достаточно полная подборка, молодец автор 5п

  32. March 2nd, 2010 at 07:28 | #32

    Очень хотелось бы увидеть продолжение 7м

  33. March 2nd, 2010 at 07:32 | #33

    Мне понравилось просматривать материал 8т

  34. March 2nd, 2010 at 11:07 | #34

    С удовольствием почитаю продолжение 6я

  35. March 5th, 2010 at 15:12 | #35

    Очень онравился стиль автора, прикольно читать 9ф

  36. March 21st, 2010 at 05:30 | #36

    Ну вот, глаза уже совсем закрываются. Завтра обязательно дочитаю :)

  1. March 10th, 2010 at 15:59 | #1
  2. March 18th, 2010 at 20:49 | #2
  3. March 19th, 2010 at 21:12 | #3