<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sonatype Blog &#187; emma</title>
	<atom:link href="http://www.sonatype.com/people/tag/emma/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sonatype.com/people</link>
	<description>Sonatype is transforming software development with tools, information and services that enable organizations to build better software, faster, using open-source components.</description>
	<lastBuildDate>Wed, 23 May 2012 14:24:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Integration Tests with Maven (Part 2): Test Coverage Reports</title>
		<link>http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/</link>
		<comments>http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 16:05:17 +0000</pubDate>
		<dc:creator>Marvin Froeder</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[emma]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=2475</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/' addthis:title='Integration Tests with Maven (Part 2): Test Coverage Reports '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>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. [...]]]></description>
			<content:encoded><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/' addthis:title='Integration Tests with Maven (Part 2): Test Coverage Reports '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div><p>In the <a href="http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-1-failsafe-plugin/">previous article</a>, 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.</p>

<p><span id="more-2475"></span></p>

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

<h3>Unit test coverage</h3>

<p>Let&#8217;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:</p>

<pre style="padding-left: 30px;">&lt;plugin&gt;
  &lt;groupId&gt;org.sonatype.maven.plugin&lt;/groupId&gt;
  &lt;artifactId&gt;emma-maven-plugin&lt;/artifactId&gt;
  &lt;version&gt;1.1&lt;/version&gt;
  &lt;executions&gt;
    &lt;execution&gt;
      &lt;phase&gt;process-classes&lt;/phase&gt;
      &lt;goals&gt;
        &lt;goal&gt;instrument&lt;/goal&gt;
      &lt;/goals&gt;
    &lt;/execution&gt;
  &lt;/executions&gt;
&lt;/plugin&gt;</pre>

<p><span>This plugin will instrument classes at target/generated-class/emma/classes, this won&#8217;t change the original classes at target/classes.   Then, we must tell surefire plugin to look for classes at new location:</span></p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  org.apache.maven.plugins
  maven-surefire-plugin
  2.4.3
&nbsp;
    ${project.build.directory}/generated-classes/emma/classes</pre></div></div>


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

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


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">[EMMA v2.0.5312 report, generated Fri Jun 12 15:43:28 BRT 2009]
-------------------------------------------------------------------------------
OVERALL COVERAGE SUMMARY:
&nbsp;
[class, %]	[method, %]	[block, %]	[line, %]	[name]
100% (1/1)	67%  (2/3)!	56%  (10/18)!	50%  (4/8)!	all classes</pre></div></div>


<p><strong>Note:</strong> 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 <em>coverage.ec</em> in the project root directory.  This file contains the coverage result in a binary format used by Emma.</p>

<h3>Integration test coverage</h3>

<p>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&#8217;s see the pom:</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  org.<span style="color: #006633;">sonatype</span>.<span style="color: #006633;">maven</span>.<span style="color: #006633;">plugin</span>
  emma4it<span style="color: #339933;">-</span>maven<span style="color: #339933;">-</span>plugin
  <span style="color: #cc66cc;">1.3</span>
&nbsp;
      instrument
&nbsp;
        instrument<span style="color: #339933;">-</span>project<span style="color: #339933;">-</span>artifact
&nbsp;
        <span style="color: #000066; font-weight: bold;">true</span></pre></div></div>


<p>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 <strong>instrument-project-artifact</strong> 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.</p>

<p>There is also the configuration <em>appendEmma</em>.  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.</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">[EMMA v2.0.5312 report, generated Fri Jun 12 16:05:45 BRT 2009]
-------------------------------------------------------------------------------
OVERALL COVERAGE SUMMARY:
&nbsp;
[class, %]	[method, %]	[block, %]	[line, %]	[name]
100% (1/1)	100% (3/3)	89%  (16/18)	88%  (7/8)	all classes</pre></div></div>


<p>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&#8217;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&#8217;t do the same.</p>

<h3>Test Coverage Report</h3>

<p>At this point we have all coverage data stored at coverage.ec, in Emma binary format.  Let&#8217;s now convert that into something that is more readable.</p>

<p>Add the following execution on emma4it-maven-plugin:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  report
post-integration-test
&nbsp;
    report
&nbsp;
        ${project.build.sourceDirectory}</pre></div></div>


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

<div id="attachment_2481" class="wp-caption alignnone" style="width: 160px"><img class="size-thumbnail wp-image-2481" title="coveragereport" src="http://www.sonatype.com/people/wp-content/uploads/2009/06/coveragereport-150x150.png" alt="Emma HTML coverage report" width="150" height="150" /><p class="wp-caption-text">Emma HTML coverage report</p></div>

<p>That is it for part 2.  On the next part, I will show some ideas about testing more complex projects like <a href="http://flexmojos.sonatype.org/">flexmojos</a> and <a href="http://nexus.sonatype.org/">nexus</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sonatype.com/people/2009/06/integration-tests-with-maven-part-2-test-coverage-reports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

