SONAR - 使用 Cobertura 测量代码覆盖率

发布于 2024-12-09 10:31:37 字数 374 浏览 0 评论 0原文

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

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

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

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

感谢您的帮助!

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

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?

Is there an easier way to use Cobertura in SONAR?

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

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

jJeQQOZ5 2024-12-16 10:31:38

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

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

单元测试和代码覆盖率

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

<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>

调用 Sonar

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

<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 行为的各个方面:

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 生成的代码覆盖率报告。

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

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

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

Unit test and code coverage

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>

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

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

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.

浅暮の光 2024-12-16 10:31:38

您必须将这些属性添加到 Sonar 的 pom.xml 中:(

<properties>
    <sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
    <sonar.phase>generate-sources</sonar.phase>
    <sonar.surefire.reportsPath>target/reports/test/</sonar.surefire.reportsPath>
    <sonar.cobertura.reportPath>../project/target/reports/coverage/coverage.xml</sonar.cobertura.reportPath>
</properties>

使用适合您环境的路径)

并运行:

mvn sonar:sonar

检查 用户列表了解更多详细信息。

You would have to add these properties to Sonar's pom.xml:

<properties>
    <sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
    <sonar.phase>generate-sources</sonar.phase>
    <sonar.surefire.reportsPath>target/reports/test/</sonar.surefire.reportsPath>
    <sonar.cobertura.reportPath>../project/target/reports/coverage/coverage.xml</sonar.cobertura.reportPath>
</properties>

(with paths appropriate to your environment)

And run:

mvn sonar:sonar

Check the user list for more details.

我一直都在从未离去 2024-12-16 10:31:38

如果您使用 Maven,则无需在 POM 文件中指定任何特殊内容。只需运行“mvn clean sonar:sonar”,Sonar 就会自动编译您的代码,使用 Cobertura(Sonar 中的默认覆盖引擎)运行您的测试,并将所有结果推送到数据库中。

如果您使用 Ant [1] 或简单的 java runner [2] 而不是 Maven,则相同。

我确实坚持这样一个事实:在运行 Sonar 之前,您不必手动运行 Cobertura(例如使用 Ant 任务)。

[1] http://docs.codehaus.org/display/ SONAR/Analyzing+with+Sonar+Ant+Task

[2] http://docs.codehaus.org/display/SONAR/Analyse+with+a+simple+Java+Runner

法布里斯,
声纳源

if you're using Maven, then you do not have anything special to specify in your POM file. Just run "mvn clean sonar:sonar" and Sonar will automatically compile your code, run your tests with Cobertura (which is the default coverage engine in Sonar) and push all the results in the DB.

Same if you're using Ant [1] or the simple java runner [2] instead of Maven.

I do insist on the fact that you do not have to manually run Cobertura (with an Ant task for instance) previously to running Sonar.

[1] http://docs.codehaus.org/display/SONAR/Analyzing+with+Sonar+Ant+Task

[2] http://docs.codehaus.org/display/SONAR/Analyse+with+a+simple+Java+Runner

Fabrice,
SonarSource

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文