SONAR - 使用 Cobertura 测量代码覆盖率 [英] SONAR - Measure Code Coverage using Cobertura

查看:44
本文介绍了SONAR - 使用 Cobertura 测量代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用声纳来测量代码质量.我不知道的一件事是使用 Cobertura 测量代码覆盖率的步骤.

I am using sonar to measure code quality. One thing that I do not know is the steps to measure code coverage using Cobertura.

我按照 http://cobertura.sourceforge.net/anttaskreference.html 中的步骤进行操作并且能够生成xml文件.如何将这些 xml 文件导入 SONAR?

I followed the steps from http://cobertura.sourceforge.net/anttaskreference.html and was able to generate xml files. How do I get these xml files into SONAR?

有没有更简单的方法在 SONAR 中使用 Cobertura?

Is there an easier way to use Cobertura in SONAR?

我在与我的 SONAR 服务器不同的服务器上运行代码覆盖 (Cobertura).两台服务器都在 LINUX 下运行.

I am running the code coverage (Cobertura) in a different server than my SONAR server. Both servers are running under LINUX.

感谢您的帮助!

推荐答案

您将 Sonar 任务配置为上传由构建逻辑的其他部分生成的单元测试和 cobertura 报告.

You configure the Sonar task to upload unit test and cobertura reports generated by other parts of your build logic.

这与具有 Sonar 能够利用的标准构建生命周期的 Maven 形成对比.

This is in contrast to Maven which has a standard build life-cycle that Sonar is able to leverage.

以下逻辑使用 cobertura 检测类运行单元测试.最后由 cobertura 生成 XML 覆盖率报告:

The following logic runs the unit tests with cobertura instrumented classes. An XML coverage report is generated by cobertura at the end:

<target name="instrument-classes" depends="compile-tests">
    <taskdef resource="tasks.properties" classpathref="test.path"/>
    <cobertura-instrument todir="${instrumented.classes.dir}" datafile="${build.dir}/cobertura.ser">
        <fileset dir="${classes.dir}"/>
    </cobertura-instrument>
</target>

<target name="junit" depends="instrument-classes">
    <junit printsummary="yes" haltonfailure="yes">
        <classpath>
            <path refid="test.path"/>
            <pathelement path="${instrumented.classes.dir}"/>
            <pathelement path="${test.classes.dir}"/>
        </classpath>

        <formatter type="xml"/>

        <batchtest fork="yes" todir="${test.reports.dir}">
            <fileset dir="${test.src.dir}">
                <include name="**/*Test*.java"/>
                <exclude name="**/AllTests.java"/>
            </fileset>
        </batchtest>
    </junit>
</target>

<target name="test" depends="junit">
    <cobertura-report format="xml" datafile="${build.dir}/cobertura.ser" destdir="${cobertura.reports.dir}"/> 
</target>

调用声纳

我通常使用一个非常简单的声纳目标:

Invoking Sonar

I normally use a very simple Sonar target:

<target name="sonar" depends="test">
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml" classpathref="sonar.path"/>

    <sonar:sonar key="${sonar.project.key}" version="${sonar.project.version}" xmlns:sonar="antlib:org.sonar.ant"/>
</target>

并使用属性文件来控制 Sonar 行为的各个方面:

And use a properties file to control all aspects of Sonar's behaviour:

sonar.project.key=org.demo:demo
sonar.project.version=1.0-SNAPSHOT
sonar.projectName=Demo project

sonar.host.url=http://myserver:9000
sonar.jdbc.url=jdbc:mysql://myserver:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar

sonar.sources=${src.dir}
sonar.tests=${test.src.dir}
sonar.binaries=${classes.dir}

sonar.dynamicAnalysis=reuseReports
sonar.surefire.reportsPath=${test.reports.dir}
sonar.java.coveragePlugin=cobertura
sonar.cobertura.reportsPath=${cobertura.reports.dir}/coverage.xml

演示如何配置 Sonar 以获取由 junit 创建的单元测试报告和由 cobertura 生成的代码覆盖率报告.

Demonstrates how Sonar can be configured to pick up the unit test reports created by junit and the code coverage report generated by cobertura.

构建不必与 Sonar 在同一台服务器上运行.在这种情况下,必须提供远程 Sonar URL 和 JDBC 凭据.

The build does not have to run on the same server as Sonar. In that case one must provide the remote Sonar URL and JDBC credentials.

这篇关于SONAR - 使用 Cobertura 测量代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆