开发者

analysing CODE COVERAGE in SONAR 2.6

开发者 https://www.devze.com 2023-02-20 14:30 出处:网络
I am using sonar 2.6 with maven 3 I am using the default corbetura plugin for code coverage of my project, but it always shows 0% coverage, although I have written junit test cases using the junit-4.9

I am using sonar 2.6 with maven 3

I am using the default corbetura plugin for code coverage of my project, but it always shows 0% coverage, although I have written junit test cases using the junit-4.9b2.jar

This is my pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xs开发者_运维问答i="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.niit.karan</groupId>
  <artifactId>DataBlast</artifactId>
  <name>DataBlast</name>
  <version>1.0</version>

  <build>

<sourceDirectory>src</sourceDirectory> 
<outputDirectory>bin</outputDirectory> 

    <plugins>     
<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
     <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <excludes> 
            <exclude>**/*.*</exclude> 
        </excludes> 
    </configuration> 
</plugin>

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.3</version>
        <configuration>

        <forkMode>once</forkMode>

          <instrumentation>
            <ignores>
              <ignore>com.example.boringcode.*</ignore>
              </ignores>
            <excludes>
              <exclude>com/example/dullcode/**/*.class</exclude>
              <exclude>com/example/**/*Test.class</exclude>
            </excludes>

          </instrumentation>
        </configuration>

    <executions>
          <execution>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>


  <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>sonar-maven-plugin</artifactId>
      <version>2.0-beta-2</version>
          <configuration>
              <timeout>3600000</timeout> 
          </configuration>
       </plugin>
    </plugins>
  </build>
  <properties>
    <sonar.dynamicAnalysis>true</sonar.dynamicAnalysis>
  </properties>
</project>

And this is the test case I have written just to check the plugin:

package test;

import junit.framework.TestCase;

public class TestCalc extends TestCase{

    Calc calc = new Calc();
    public void testSum(){
        assertTrue(3 == calc.sum(1, 2));
        assertTrue(4 == calc.sum(2, 2));
    }
}

Someone please help considering I am a very new user of sonar.. Thanks in advance


The maven-compiler-plugin was configured to skip all source code, including tests. Remove the <excludes> session of plugin configuration, in order that Maven works properly, compiling your source code and tests.


I met the same problem before. In my case, it because as Surefire plugin property set wrong:

Maven project always use Surefire Plugin during the test phase of the build lifecycle to execute unit tests. Check your pom.xml if there has test configuration of Surefire. Set the and to "false", if they set as "true", Sonar won't compile and run your unit test cases, then there's always 0% coverage:

<plugin>                    
<groupId>org.apache.maven.plugins</groupId>             
<artifactId>maven-surefire-plugin</artifactId>              
<version>2.9</version>              
<configuration>             
    <includes>              
        <include>**/*Test*.java</include>       
    </includes>         
    <parallel>methods</parallel>            
    <threadCount>10</threadCount>           
    <testFailureIgnore>true</testFailureIgnore> 
    <skipTests>false</skipTests>        
    <skip>false</skip>          
</configuration>                
</plugin>

Hope it can help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消