Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Sonar
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<table class="wysiwyg-macro" data-macro-name="info" data-macro-parameters="icon=false|title=Table of Contents" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2luZm86aWNvbj1mYWxzZXx0aXRsZT1UYWJsZSBvZiBDb250ZW50c30&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e3RvYzptYXhMZXZlbD0yfQ&locale=en_GB&version=2" data-macro-name="toc" data-macro-parameters="maxLevel=2"></p></td></tr></table><p><span style="font-size: 10.0pt;line-height: 13.0pt;">There are two different ways to feed Sonar with information on unit tests execution and code coverage:</span></p><ul><li><strong>Using its embedded engine </strong>to let Sonar directly launch the unit tests execution and code coverage. Then, the <a class="confluence-link" href="/display/SONAR/Sonar+Concepts#SonarConcepts-analyzer" data-anchor="analyzer" data-linked-resource-id="111706489" data-linked-resource-type="page" data-linked-resource-default-alias="Sonar Concepts#analyzer" data-base-url="http://docs.codehaus.org">analyzer</a> will automatically fed the database with data exported from the generated reports.</li><li><strong>Reusing existing reports</strong> that have been previously generated by external tools.</li></ul><p>Project samples covering the different cases below are available on github that can be <a class="external-link" href="https://github.com/SonarSource/sonar-examples" rel="nofollow">browsed</a> or <a class="external-link" href="https://github.com/SonarSource/sonar-examples/zipball/master" rel="nofollow">downloaded</a>: <span style="color: rgb(34,34,34);">projects/code-coverage/</span></p><div>See <a class="confluence-link" href="/display/SONAR/Resource+Viewer#ResourceViewer-viewingUnitTests" data-anchor="viewingUnitTests" data-linked-resource-id="111706389" data-linked-resource-type="page" data-linked-resource-default-alias="Resource Viewer#viewingUnitTests" data-base-url="http://docs.codehaus.org">Resource Viewer on Unit Tests</a> or <a class="confluence-link" href="/display/SONAR/Lack+of+Unit+Tests" data-linked-resource-id="229738278" data-linked-resource-type="page" data-linked-resource-default-alias="Lack of Unit Tests" data-base-url="http://docs.codehaus.org">Quality Flows > Lack of Unit Tests</a> to browse the results on the Sonar web interface.</div><div><span style="font-size: 10.0pt;line-height: 13.0pt;"><br /></span></div><div><span style="font-size: 10.0pt;line-height: 13.0pt;">See also the </span><a class="external-link" href="http://www.sonarsource.org/manage-code-coverage-by-unit-tests-with-sonar/" rel="nofollow" style="font-size: 10.0pt;line-height: 13.0pt;">Manage Code Coverage by Unit Tests with Sonar</a><span style="font-size: 10.0pt;line-height: 13.0pt;"> blog post.</span></div><h1>Using Sonar Embedded Engine</h1><p><a class="confluence-link" href="/display/SONAR/Analyzing+with+Maven" data-linked-resource-id="193888393" data-linked-resource-type="page" data-linked-resource-default-alias="Analyzing with Maven" data-base-url="http://docs.codehaus.org">Maven</a> supports this mode with the following code coverage tools: <a class="confluence-link" href="/display/SONAR/JaCoCo+Plugin" data-linked-resource-id="230397046" data-linked-resource-type="page" data-linked-resource-default-alias="JaCoCo Plugin" data-base-url="http://docs.codehaus.org">JaCoCo</a>, <a class="confluence-link" href="/display/SONAR/Cobertura+Plugin" data-linked-resource-id="230397053" data-linked-resource-type="page" data-linked-resource-default-alias="Cobertura Plugin" data-base-url="http://docs.codehaus.org">Cobertura</a>, <a class="confluence-link" href="/display/SONAR/Emma+Plugin" data-linked-resource-id="116359204" data-linked-resource-type="page" data-linked-resource-default-alias="Emma Plugin" data-base-url="http://docs.codehaus.org">Emma</a> and <a class="confluence-link" href="/display/SONAR/Clover+Plugin" data-linked-resource-id="194314488" data-linked-resource-type="page" data-linked-resource-default-alias="Clover Plugin" data-base-url="http://docs.codehaus.org">Clover</a>.</p><p>The <a class="confluence-link" href="/display/SONAR/Analyzing+with+Sonar+Ant+Task" data-linked-resource-id="210174228" data-linked-resource-type="page" data-linked-resource-default-alias="Analyzing with Sonar Ant Task" data-base-url="http://docs.codehaus.org">Sonar Ant Task</a> and the <a class="confluence-link" href="/display/SONAR/Analyzing+with+Sonar+Runner" data-linked-resource-id="192872482" data-linked-resource-type="page" data-linked-resource-default-alias="Analyzing with Sonar Runner" data-base-url="http://docs.codehaus.org">Sonar Runner</a> do not support this mode.</p><table class="wysiwyg-macro" data-macro-name="note" data-macro-parameters="title=Compile" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vdGU6dGl0bGU9Q29tcGlsZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>Before running a Sonar analysis with JaCoCo set as the code coverage tool, the source code has to be compiled.</p></td></tr></table><p> </p><p>First, tell Sonar to execute the unit tests and the code coverage tool:</p><p>With Maven in your pom file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre><sonar.dynamicAnalysis>true</sonar.dynamicAnalysis></pre></td></tr></table><h3>Maven Project</h3><p>With Maven, then, you just have to tell Sonar which code coverage engine you wish to use: jacoco or cobertura or emma or clover.</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre><sonar.java.coveragePlugin>jacoco or cobertura or emma or clover</sonar.java.coveragePlugin> Prior to Sonar 3.4, the property was sonar.core.codeCoveragePlugin.</pre></td></tr></table><p>Note that this property can be set at Sonar instance level through the web interface: <strong>Settings > Configuration > General Settings > Java</strong>.</p><p>Sample projects are available on github that can be <a class="external-link" href="https://github.com/SonarSource/sonar-examples" rel="nofollow">browsed</a> or <a class="external-link" href="https://github.com/SonarSource/sonar-examples/zipball/master" rel="nofollow">downloaded</a>:</p><ul><li><span style="color: rgb(34,34,34);"><span>Maven and JaCoCo: <span style="color: rgb(34,34,34);">projects/code-coverage/ut/maven/</span><span style="color: rgb(34,34,34);">ut-maven-jacoco-runTests</span></span></span></li><li><span style="color: rgb(34,34,34);"><span><span style="color: rgb(34,34,34);"><span style="color: rgb(34,34,34);">Maven and Cobertura: </span><span style="color: rgb(34,34,34);">projects/code-coverage/ut/maven/<span>ut-maven-cobertura-runTests</span></span></span></span></span></li><li><span style="color: rgb(34,34,34);"><span><span style="color: rgb(34,34,34);"><span style="color: rgb(34,34,34);"><span>Maven and Emma: <span style="color: rgb(34,34,34);">projects/code-coverage/ut/maven/</span><span style="color: rgb(34,34,34);">ut-maven-emma-runTests</span></span></span></span></span></span></li></ul><h3>Maven Multi-module Project and JaCoCo</h3><p><span style="color: rgb(34,34,34);">For multi-module Maven projects, compute overall code coverage by unit tests across modules. This case clearly lies between unit tests and integration tests but this is widely used.</span></p><p><span style="color: rgb(34,34,34);">Sample projects are available on github that can be <a class="external-link" href="https://github.com/SonarSource/sonar-examples" rel="nofollow">browsed</a> or <a class="external-link" href="https://github.com/SonarSource/sonar-examples/zipball/master" rel="nofollow">downloaded</a>: projects/code-coverage/combined ut-it/maven/combined-ut-it-multimodule-maven-<span style="color: rgb(34,34,34);">jacoco</span></span></p><p><span style="color: rgb(34,34,34);"><span style="color: rgb(34,34,34);">Note that this project structure is not compatible with the below feature: mapping of unit tests and covered code.</span></span></p><h3>Mapping of Unit Tests and Covered Code</h3><p><span style="color: rgb(34,34,34);">Since Sonar 3.5, it is possible to display the mapping of unit tests and covered code. The objective is to answer the following questions:</span></p><ul><li>Which files are covered by a given unit test?</li><li>How many lines of code are covered by a given unit test?</li><li>Which lines are covered by a given unit test?</li><li>Which tests do cover a given line of code?</li></ul><div>See <a class="confluence-link" href="/display/SONAR/Resource+Viewer#ResourceViewer-viewingUnitTests" data-anchor="viewingUnitTests" data-linked-resource-id="111706389" data-linked-resource-type="page" data-linked-resource-default-alias="Resource Viewer#viewingUnitTests" data-base-url="http://docs.codehaus.org">Resource Viewer on Unit Tests</a> for more details.</div><p>A sample project is available on github that can be <a class="external-link" href="https://github.com/SonarSource/sonar-examples" rel="nofollow">browsed</a> or <a class="external-link" href="https://github.com/SonarSource/sonar-examples/zipball/master" rel="nofollow">downloaded</a>: projects/code-coverage/ut/maven/ut-maven-jacoco-runTests. What is necessary to get this mapping is contained between 'BEGIN/END: Specific to mapping unit tests and covered code' tags in the pom file. Note that you have to run the analysis with the '<span style="font-size: 10.0pt;line-height: 13.0pt;">coverage-per-test' profile. Read the README file for more information.</span></p><p><span style="font-size: 10.0pt;line-height: 13.0pt;">Requires:</span></p><ul><li>JUnit 4.7+</li><li>Maven Surefire Plugin 2.4+</li></ul><p><span style="font-size: 10.0pt;line-height: 13.0pt;"> </span></p><h1>Reusing Existing Reports</h1><p>Every <a class="confluence-link" href="/display/SONAR/Sonar+Concepts#SonarConcepts-analyzer" data-anchor="analyzer" data-linked-resource-id="111706489" data-linked-resource-type="page" data-linked-resource-default-alias="Sonar Concepts#analyzer" data-base-url="http://docs.codehaus.org">analyzer</a> supports this reusing reports mode.</p><p>To be reused by Sonar:</p><ul><li>The tests execution reports have to comply to the JUnit XML format.</li><li>The code coverage reports have to be generated by either <a class="confluence-link" href="/display/SONAR/JaCoCo+Plugin" data-linked-resource-id="230397046" data-linked-resource-type="page" data-linked-resource-default-alias="JaCoCo Plugin" data-base-url="http://docs.codehaus.org">JaCoCo</a>, <a class="confluence-link" href="/display/SONAR/Emma+Plugin" data-linked-resource-id="116359204" data-linked-resource-type="page" data-linked-resource-default-alias="Emma Plugin" data-base-url="http://docs.codehaus.org">Emma</a>, <a class="confluence-link" href="/display/SONAR/Cobertura+Plugin" data-linked-resource-id="230397053" data-linked-resource-type="page" data-linked-resource-default-alias="Cobertura Plugin" data-base-url="http://docs.codehaus.org">Cobertura</a> or <a class="confluence-link" href="/display/SONAR/Clover+Plugin" data-linked-resource-id="194314488" data-linked-resource-type="page" data-linked-resource-default-alias="Clover Plugin" data-base-url="http://docs.codehaus.org">Clover</a>.</li></ul><div>Sample projects are available on github that can be <a class="external-link" href="https://github.com/SonarSource/sonar-examples" rel="nofollow">browsed</a> or <a class="external-link" href="https://github.com/SonarSource/sonar-examples/zipball/master" rel="nofollow">downloaded</a>:</div><div><ul><li><span>Sonar Runner and JaCoCo: projects/code-coverage/ut/sonar-runner/<span>ut-sonarRunner-jacoco-reuseReports</span></span></li><li><span>Maven and JaCoCo: projects/code-coverage/ut/maven/</span><span>ut-maven-jacoco-reuseReports</span></li><li>Maven and Emma: projects/code-coverage/ut/maven/ut-maven-emma-reuseReports<a class="js-slide-to js-rewrite-sha" href="https://github.com/SonarSource/sonar-examples/tree/master/projects/code-coverage/ut/ut-maven-emma-reuseReports" /></li><li><span>Ant and JaCoCo: projects/code-coverage/ut/ant/ut-ant-jacoco-reuseReports</span></li><li><span>Ant and JaCoCo (JaCoCo report generated with the JaCoCo Ant Task): projects/code-coverage/ut/ant/ut-ant-jacoco-runTests</span></li><li><span>Ant and JaCoCo, multi-module project (JaCoCo report generated with the JaCoCo Ant Task): projects/code-coverage/ut/ant/ut-ant-jacoco-modules-runTests</span></li></ul></div><p>First, tell Sonar to reuse existing reports:</p><p>With Maven in your pom.xml file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre><sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis></pre></td></tr></table><p>With Ant in your build.xml file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre><property name="sonar.dynamicAnalysis" value="reuseReports" /></pre></td></tr></table><p>With Sonar Runner in your sonar-project.properties file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>sonar.dynamicAnalysis=reuseReports</pre></td></tr></table><h3>Test Execution Reports</h3><p>Tell Sonar where your unit tests execution reports are: absolute or relative path to the directory containing your reports</p><p>With Maven in your pom file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre><sonar.surefire.reportsPath>[baseDir]/myReports/myExecutionReports</sonar.surefire.reportsPath></pre></td></tr></table><p>With Ant in your build file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre><property name="sonar.surefire.reportsPath" value="[baseDir]/myReports/myExecutionReports" /></pre></td></tr></table><p>With Sonar Runner in your sonar-project file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>sonar.surefire.reportsPath=[baseDir]/myReports/myExecutionReports</pre></td></tr></table><h3>Code Coverage Report </h3><p>First, tell Sonar which code coverage engine has been used to generate the reports: jacoco or cobertura or emma or clover.</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre><sonar.java.coveragePlugin>jacoco or cobertura or emma or clover</sonar.java.coveragePlugin> Prior to Sonar 3.4, the property was sonar.core.codeCoveragePlugin.</pre></td></tr></table><p><span style="font-size: 10.0pt;line-height: 13.0pt;">Then, tell Sonar where to get the code coverage reports:</span></p><div class="syntaxhighlighter nogutter html"><p><span style="font-size: 10.0pt;line-height: 13.0pt;">With Maven in your pom file:</span></p></div><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre><sonar.jacoco.reportPath>[baseDir]/myReports/myCodeCoverageReport</sonar.jacoco.reportPath></pre></td></tr></table><p>With Ant in your build file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre><property name="sonar.jacoco.reportPath" value="[baseDir]/myReports/myCodeCoverageReport" /></pre></td></tr></table><p>With Sonar Runner in your sonar-project file:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bGFuZ3VhZ2U9aHRtbC94bWx9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>sonar.jacoco.reportPath=[baseDir]/myReports/myCodeCoverageReport</pre></td></tr></table><p><span style="font-size: 10.0pt;line-height: 13.0pt;">Depending on the code coverage tool you used to generate your report, replace </span><em style="font-size: 10.0pt;line-height: 13.0pt;">jacoco</em><span style="font-size: 10.0pt;line-height: 13.0pt;"> by </span><em style="font-size: 10.0pt;line-height: 13.0pt;">emma</em><span style="font-size: 10.0pt;line-height: 13.0pt;">, </span><em style="font-size: 10.0pt;line-height: 13.0pt;">cobertura</em><span style="font-size: 10.0pt;line-height: 13.0pt;"> or </span><em style="font-size: 10.0pt;line-height: 13.0pt;">clover</em><span style="font-size: 10.0pt;line-height: 13.0pt;"> in the property name.</span></p><h1>FAQ</h1><h3>0% code coverage reported whereas unit tests are correctly executed</h3><p>This problem occurs while using the Maven Cobertura Plugin and a special configuration of the Maven Surefire Plugin preventing unit tests to be forked. This problem can be solved by removing the line "<forkMode>never</forkMode>" in the Maven configuration file (see <a href="http://jira.codehaus.org/browse/SONAR-1455">SONAR-1445</a> and <a href="http://jira.codehaus.org/browse/MCOBERTURA-70">MCOBERTURA-70</a>).</p><h3>java.lang.Error: Unable to access JaCoCo Agent - make sure that you use JaCoCo and version not lower than 0.6.2</h3><div><br />Make sure that you do <u>NOT</u> set 'forkMode' to 'never'. Otherwise, Surefire will not fork the process and no JaCoCo agent will be attached.</div><table class="wysiwyg-macro" data-macro-name="code" data-macro-parameters="language=html/xml|title=pom.xml" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6dGl0bGU9cG9tLnhtbHxsYW5ndWFnZT1odG1sL3htbH0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <forkMode>never</forkMode> <!-- Do not set forkMode to never --> <reportFormat>xml</reportFormat> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> ...</pre></td></tr></table><p> </p><p> </p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced