【问题标题】:Can't read unit test success rate, can read unit test coverage generated by jacoco无法读取单元测试成功率,可以读取 jacoco 生成的单元测试覆盖率
【发布时间】:2013-08-20 09:51:36
【问题描述】:

我希望使用 sonar,jacoco,testng,ant 来展示单元测试成功率和单元测试覆盖率。我可以通过 jacoco 生成覆盖率报告并在 Sonar 中显示。但是 Sonar 无法显示单元测试的成功率。 “单元测试成功”始终等于 0,如下所示。

实际上,testng 可以生成测试报告(包括总测试用例数、失败测试用例数和成功测试用例数)。

声纳如何读取这些报告并在声纳仪表板中显示?

而我的 build.xml 如下所示:

<!-- ========= Define the main properties of this project ========= -->
<property name="src.dir" value="${basedir}/src" />
<property name="test.dir" value="${basedir}/test" />
<property name="lib.dir" value="${basedir}/lib" />
<property name="build.dir" value="${basedir}/build" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="dist.dir" value="${build.dir}/dist" />
<property name="resource" value="${basedir}/resource" />
<property name="jar-file" value="${dist.dir}/Calculate-1.0.jar" />
<property name="reports.dir" value="${build.dir}/reports" />
<property name="reports.jacoco.dir" value="${build.dir}/reports/jacoco" />

<path id="classpath">
    <fileset dir="${basedir}">
        <include name="lib/*.jar"/>
    </fileset>
    <fileset dir="${build.dir}">
        <include name="dist/*.jar" />
    </fileset>
</path> 

<!-- ========= Define "regular" targets: clean, compile, ... ========= -->
<target name="clean">
    <delete dir="${build.dir}" />
</target>

<target name="init">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${dist.dir}" />
    <mkdir dir="${classes.dir}" />
    <mkdir dir="${reports.dir}" />
    <mkdir dir="${reports.jacoco.dir}" />
</target>

<target name="compile" depends="init">
    <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="true" />
    <javac srcdir="${test.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="true" />
</target>

<target name="jar" depends="compile">
    <jar destfile="${jar-file}" basedir="${classes.dir}"/>
</target>

<target name="test" depends="jar">
    <taskdef name="testng" classname="com.beust.testng.TestNGAntTask">
            <classpath path="${lib.dir}/testng-6.8.5.jar"/>
    </taskdef> 
    <taskdef uri="antlib:org.jacoco.ant" resource="resource/antlib.xml"> 
        <classpath path="${lib.dir}/jacoco-ant-task-core-agent.jar" />
    </taskdef>
    <jacoco:coverage destfile="${reports.jacoco.dir}/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
        <testng classpath="${lib.dir}/testng-6.8.5.jar" classpathref="classpath" outputDir="${reports.dir}" haltOnFailure="false" >
            <xmlfileset dir="${basedir}" includes="testng.xml" />
            <sysproperty key="testdata" value="${test.dir}" />
            <jvmarg value="-Dtest.resources.dir=${test.dir}" />
        </testng>
    </jacoco:coverage>
</target>
-->
<!-- ========= Define Sonar target ========= -->
<property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" />
<property name="sonar.projectName" value="Simple Java Project analyzed with the Sonar Ant Task" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.language" value="java" />
<property name="sonar.sources" value="${src.dir}" />
<property name="sonar.tests" value="${test.dir}" />
<property name="sonar.binaries" value="${classes.dir}" />
<property name="sonar.sourceEncoding" value="UTF-8" />
<property name="sonar.dynamicAnalysis" value="reuseReports" />
<property name="sonar.java.coveragePlugin" value="jacoco" />
<property name="sonar.jacoco.reportPath" value="${reports.jacoco.dir}/jacoco.exec" />
<property name="sonar.host.url" value="http://localhost:9000" />
<property name="sonar.jdbc.url" value="jdbc:h2:tcp://localhost:9092/sonar" />
<!--<property name="sonar.surefire.reportsPath" value="${reports.dir}" />-->
<!--
  <property name="sonar.jdbc.username" value="..." />
  <property name="sonar.jdbc.password" value="..." />
-->
<target name="sonar" depends="compile">
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
        <classpath path="${lib.dir}/sonar-ant-task-2.1.jar" />
    </taskdef>      
    <!-- Execute Sonar -->
    <sonar:sonar />
</target>
<target name="all" depends="clean,init,compile,jar,test,sonar" />

【问题讨论】:

  • 您的属性控制读取您现有的报告已被注释掉。当你取消注释时发生了什么?
  • 据我所知,此属性仅用于 junit 报告,不适用于 tesng 报告。当我取消注释时,它不起作用。
  • 啊。根据this site,Sonar 不支持 TestNG,但您可以让 testNG 生成 JUnit 格式的报告。
  • 嗨,珍妮。我现在使用 ReportNG 生成 Junit 格式的 XML 报告。但“单元测试成功”仍然为 0。我修改“testng”taskdef 如下。

标签: java ant testng sonarqube jacoco


【解决方案1】:

感谢珍妮的回答。我现在解决了。正如珍妮所说,声纳不支持TestNG,它支持JUnit。所以我使用 ReportNG 来生成 xml 格式的测试报告。报告文件将命名为 com.abc.classTest1.xml。为了在声纳中成功显示单元测试成功,我们应该将其名称更改为 TEST-com.abc.classTest1.xml。下面是我的 build.xml 的最终版本。

<!-- ========= Define the main properties of this project ========= -->
<property name="src.dir" value="${basedir}/src" />
<property name="test.dir" value="${basedir}/test" />
<property name="lib.dir" value="${basedir}/lib" />
<property name="build.dir" value="${basedir}/build" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="dist.dir" value="${build.dir}/dist" />
<property name="resource" value="${basedir}/resource" />
<property name="jar-file" value="${dist.dir}/Calculate-1.0.jar" />
<property name="reports.dir" value="${build.dir}/reports" />
<property name="reports.jacoco.dir" value="${build.dir}/reports/jacoco" />
<property name="junitreport.dir" value="junitreport" />

<path id="classpath">
    <fileset dir="${basedir}">
        <include name="lib/*.jar"/>
    </fileset>
    <fileset dir="${build.dir}">
        <include name="dist/*.jar" />
    </fileset>
</path> 

<!-- ========= Define "regular" targets: clean, compile, ... ========= -->
<target name="clean">
    <delete dir=".sonar" />
    <delete dir="${build.dir}" />
    <delete dir="${junitreport.dir}" />
</target>

<target name="init">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${dist.dir}" />
    <mkdir dir="${classes.dir}" />
    <mkdir dir="${reports.dir}" />
    <mkdir dir="${reports.jacoco.dir}" />
    <mkdir dir="${junitreport.dir}" />
</target>

<target name="compile" depends="init">
    <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="true" />
    <javac srcdir="${test.dir}" destdir="${classes.dir}" classpathref="classpath" fork="true" debug="true" includeAntRuntime="true" />
</target>

<target name="jar" depends="compile">
    <jar destfile="${jar-file}" basedir="${classes.dir}"/>
</target>

<target name="test" depends="jar">
<!--    <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
        <classpath>
            <path refid="classpath"/>
        </classpath>
    </taskdef>  -->
    <taskdef name="testng" classname="com.beust.testng.TestNGAntTask">
            <classpath path="${lib.dir}/testng-6.8.5.jar"/>
    </taskdef> 
    <taskdef uri="antlib:org.jacoco.ant" resource="resource/antlib.xml"> 
        <classpath path="${lib.dir}/jacoco-ant-task-core-agent.jar" />
    </taskdef>
    <jacoco:coverage destfile="${reports.jacoco.dir}/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
        <testng classpath="${lib.dir}/testng-6.8.5.jar" classpathref="classpath" outputDir="${reports.dir}" 
        haltOnFailure="false" useDefaultListeners="false" listeners="org.uncommons.reportng.JUnitXMLReporter">
            <xmlfileset dir="${basedir}" includes="testng.xml" />
            <sysproperty key="testdata" value="${test.dir}" />
            <sysproperty key="org.uncommons.reportng.title" value="My Sonar Test Report" />
        </testng>
<!--        <junit fork="yes" dir="" failureProperty="test.failed">
            <classpath location="${classes.dir}" />
            <classpath refid="classpath" />

            <formatter type="xml" />
            <batchtest todir="${junitreport.dir}">
                <fileset dir="${test.dir}">
                    <include name="com/cisco/sonar/CalculateTestJunit.java" />
                </fileset>
            </batchtest>
        </junit>    -->
    </jacoco:coverage>
</target>   

<!-- ========= Define Sonar target ========= -->
<property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" />
<property name="sonar.projectName" value="Simple Java Project analyzed with the Sonar Ant Task" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.language" value="java" />
<property name="sonar.sources" value="${src.dir}" />
<property name="sonar.tests" value="${test.dir}" />
<property name="sonar.binaries" value="${classes.dir}" />
<property name="sonar.sourceEncoding" value="UTF-8" />
<property name="sonar.dynamicAnalysis" value="reuseReports" />
<property name="sonar.java.coveragePlugin" value="jacoco" />
<property name="sonar.jacoco.reportPath" value="${reports.jacoco.dir}/jacoco.exec" />
<property name="sonar.host.url" value="http://1.2.3.4:9000" />
<property name="sonar.jdbc.url" value="jdbc:h2:tcp://1.2.3.4:9092/sonar" />
<property name="sonar.surefire.reportsPath" value="${reports.dir}/xml" />
<!--
  <property name="sonar.jdbc.username" value="..." />
  <property name="sonar.jdbc.password" value="..." />
-->
<target name="sonar">
    <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
        <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
        <classpath path="${lib.dir}/sonar-ant-task-2.1.jar" />
    </taskdef>      
    <!-- Execute Sonar -->
    <sonar:sonar />
</target>
<target name="all" depends="clean,init,compile,jar,test,sonar" />

【讨论】:

    猜你喜欢
    • 2014-02-21
    • 2019-05-15
    • 2018-10-18
    • 2016-04-07
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多