简体   繁体   English

在SONAR 2.6中分析代码覆盖率

[英]analysing CODE COVERAGE in SONAR 2.6

I am using sonar 2.6 with maven 3 我在Maven 3中使用声纳2.6
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 我正在使用默认的corbetura插件进行项目的代码覆盖率,但是尽管我已经使用junit-4.9b2.jar编写了junit测试用例,但它始终显示0%的覆盖率

This is my pom.xml file: 这是我的pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="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. maven-compiler-plugin配置为跳过所有源代码,包括测试。 Remove the <excludes> session of plugin configuration, in order that Maven works properly, compiling your source code and tests. 删除插件配置的<excludes>会话,以使Maven正常工作,并编译源代码和测试。

I met the same problem before. 我以前遇到过同样的问题。 In my case, it because as Surefire plugin property set wrong: 就我而言,这是因为Surefire插件属性设置错误:

Maven project always use Surefire Plugin during the test phase of the build lifecycle to execute unit tests. Maven项目在构建生命周期的测试阶段始终使用Surefire插件来执行单元测试。 Check your pom.xml if there has test configuration of Surefire. 检查您的pom.xml是否具有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: 将和设置为“ false”,如果将它们设置为“ true”,Sonar不会编译和运行您的单元测试用例,那么覆盖率始终为0%:

<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. 希望它能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM