合并多个构建时错误的代码覆盖率



在我们的项目中,我们有一个问题,JaCoCo使用Jenkins或SonarQube计算错误的覆盖率。

这是我们通常的Jenkins构建脚本的例子:

<property name="demo"           location="."/>
<property name="exec_name"      value="build1.exec"/>
<import file=                   "${demo}/buildscripts/common/product_common_ant.xml"/>
<property name="coverage_data"  location="\serverjenkinsjobsTest-Coveragecoverage_dataJobName"/>
<target name="testrun" depends="compile" description="Execute tests with coverage">
    <jacoco:coverage destfile="${cov_res_file}" append="true">
        <testng classpathref="testrun_classpath" suitename="UnitTests" failureProperty="testng.failure" verbose="2" outputdir="${test-output}" workingDir="${demo}">
            <xmlfileset dir="${test}" includes="builds/unitTests.xml"/>
        </testng>
    </jacoco:coverage>
    <copy file="${test-output}/testng-results.xml" tofile = "${test-output}/unit-test-results.xml"/>
    <fail if="testng.failure"/>
    <jacoco:coverage destfile="${cov_res_file}" append="true">
        <testng classpathref="testrun_classpath" suitename="IntegrationTests" failureProperty="testng.failure" verbose="2" outputdir="${test-output}" workingDir="${demo}">
            <xmlfileset dir="${test}" includes="builds/integTests_Build.xml"/>
        </testng>
    </jacoco:coverage>
    <copy file = "${test-output}/testng-results.xml" tofile = "${test-output}/integ-test-results.xml"/>
    <fail if="testng.failure"/>
    <jacoco:coverage destfile="${cov_res_file}" append="true">
        <testng classpathref="testrun_classpath" suitename="BUildGUITests1" failureProperty="testng.failure" verbose="2" outputdir="${test-output}" workingDir="${demo}">
            <xmlfileset dir="${test}" includes="builds/funcTests_build_part01.xml"/>
        </testng>
    </jacoco:coverage>
    <copy file = "${test-output}/testng-results.xml" tofile = "${test-output}/gui-test-results.xml"/>
    <fail if="testng.failure"/>
</target>
<target name="mergeTestResults" depends="testrun, copyCoverageResults">
    <copy file="${buildscripts}/empty-test-results.xml" tofile="${test-output}/testng-merge.xml"/>
    <for param="xmlFile">
        <path>
            <fileset dir="${test-output}" >
                <include name="unit-test-results.xml" />
                <include name="integ-test-results.xml" />
                <include name="gui-test-results.xml" />
            </fileset>
        </path>
        <sequential>
            <copy   file="${test-output}/testng-merge.xml"  toFile="${test-output}/start-merge.xml"/>
            <xslt style="${buildscripts}/merge-test-results.xsl"
                        destdir="${test-output}"
                        in="${test-output}/start-merge.xml"
                        out="${test-output}/testng-merge.xml">
                <param name="with" expression="@{xmlFile}" />
            </xslt>
            <delete file="${test-output}/start-merge.xml" />
        </sequential>
    </for>
</target>

所以在这个构建中有3个套件运行,覆盖数据保存在

中。
\serverjenkinsjobsTest-Coveragecoverage_dataJobNamebuildX.exec

Coverage构建具有以下脚本

<project name="Product Test Coverage Build" default="report" basedir="...." xmlns:jacoco="antlib:org.jacoco.ant">
<echo>Current username: ${user.name}</echo>
<defaultexcludes echo="true"/>
<property name="demo"           location="."/>
<property name="src"            location="${demo}/src"/>
<property name="bin"            location="${demo}/bin"/>
<property name="lib1"           location="${demo}/lib"/>
<property name="lib2"           location="${demo}/lib64"/>
<property name="coverage"       location="${demo}/coverage"/>
<property name="pr11_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct-Part1-64"/>
<property name="pr12_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct-Part2-64"/>
<property name="pr13_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct-Part3-64"/>
<property name="pr14_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct-Part4-64"/>
<property name="pr15_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct-Part5-64"/>
<property name="pr16_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct-Part6-64"/>
<property name="pr17_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct-Part7-64"/>
<property name="pr18_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct-Part8-64"/>
<property name="pr19_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct-Part9-64"/>
<property name="pr21_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct2-Part1-64"/>
<property name="pr22_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct2-Part2-64"/>
<property name="pr23_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct2-Part3-32"/>
<property name="pr24_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct2-Part4-64"/>
<property name="pr25_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct2-Part5-64"/>
<property name="pr26_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct2-Part6-64"/>
<property name="pr27_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct2-Part7-32"/>
<property name="pr28_coverage"  location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct2-Part8-64"/>
<property name="pr210_coverage" location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct2-Part10-64"/>
<property name="pr31_coverage" location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct3-Unit-Integ-Tests"/>
<property name="pr32_coverage" location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct3-32"/>
<property name="pr33_coverage" location="\serverjenkinsjobsProduct-Test-Coveragecoverage_dataProduct3-Integration-1"/>
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml" classpathref="testrun_classpath"/>
<target name="merge" depends ="compile">
    <jacoco:merge destfile="${coverage}/merged.exec">
        <fileset dir="${pr11_coverage}" includes="*.exec"/>
        <fileset dir="${pr12_coverage}" includes="*.exec"/>
        <fileset dir="${pr13_coverage}" includes="*.exec"/>
        <fileset dir="${pr14_coverage}" includes="*.exec"/>
        <fileset dir="${pr15_coverage}" includes="*.exec"/>
        <fileset dir="${pr16_coverage}" includes="*.exec"/>
        <fileset dir="${pr17_coverage}" includes="*.exec"/>
        <fileset dir="${pr18_coverage}" includes="*.exec"/>
        <fileset dir="${pr19_coverage}" includes="*.exec"/>
        <fileset dir="${pr21_coverage}" includes="*.exec"/>
        <fileset dir="${pr22_coverage}" includes="*.exec"/>
        <fileset dir="${pr23_coverage}" includes="*.exec"/>
        <fileset dir="${pr24_coverage}" includes="*.exec"/>
        <fileset dir="${pr25_coverage}" includes="*.exec"/>
        <fileset dir="${pr26_coverage}" includes="*.exec"/>
        <fileset dir="${pr27_coverage}" includes="*.exec"/>
        <fileset dir="${pr28_coverage}" includes="*.exec"/>
        <fileset dir="${pr210_coverage}" includes="*.exec"/>
        <fileset dir="${pr31_coverage}" includes="*.exec"/>
        <fileset dir="${pr32_coverage}" includes="*.exec"/>
        <fileset dir="${pr33_coverage}" includes="*.exec"/>
    </jacoco:merge>
</target>
<property environment="env"/>
<property name="coverage_rep"   location="\serverjenkinsjobsProduct-Test-Coveragebuilds${env.BUILD_NUMBER}"/>
<target name="report" depends="merge">
    <jacoco:report>
        <executiondata>
            <file file="${coverage}/merged.exec"/>
        </executiondata>
        <structure name="Product Overall Code Coverage">
            <sourcefiles encoding="UTF-8">
                <fileset dir="${src}"/>
            </sourcefiles>
        </structure>
        <html destdir="${coverage}"/>
        <csv destfile="${coverage}/report.csv"/>
        <xml destfile="${coverage}/report.xml"/>
    </jacoco:report>
    <copy   file="${coverage}/index.html"   tofile="${coverage_rep}/index.html"/>
</target>

我去掉了编译和构建部分。

当Jenkins或SonarQube运行此脚本时,我们得到的代码覆盖率约为19%,而在Eclipse本地运行所有测试并使用Eclemma(与Jacoco)合并,我们得到的代码覆盖率约为65%(这是更可靠的结果)。

我们的构建在每次SVN提交时运行,并在每次运行之前进行编译。所有的测试服务器使用相同的Java版本,尽管32位和64位架构都在使用。

我们做错了什么?

一年多以后,突然代码覆盖率又恢复正常了。很奇怪,不幸的是,之前没有办法发现哪里出了问题。

相关内容

  • 没有找到相关文章

最新更新