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

时间:2016-11-06 13:43:26

标签: java jenkins ant sonarqube jacoco

在我们的项目中,我们遇到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="\\server\jenkins\jobs\Test-Coverage\coverage_data\JobName"/>

<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个套件将保存到

\\server\jenkins\jobs\Test-Coverage\coverage_data\JobName\buildX.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="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product-Part1-64\"/>
<property name="pr12_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product-Part2-64\"/>
<property name="pr13_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product-Part3-64\"/>
<property name="pr14_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product-Part4-64\"/>
<property name="pr15_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product-Part5-64\"/>
<property name="pr16_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product-Part6-64\"/>
<property name="pr17_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product-Part7-64\"/>
<property name="pr18_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product-Part8-64\"/>
<property name="pr19_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product-Part9-64\"/>

<property name="pr21_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product2-Part1-64\"/>
<property name="pr22_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product2-Part2-64\"/>
<property name="pr23_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product2-Part3-32\"/>
<property name="pr24_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product2-Part4-64\"/>
<property name="pr25_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product2-Part5-64\"/>
<property name="pr26_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product2-Part6-64\"/>
<property name="pr27_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product2-Part7-32\"/>
<property name="pr28_coverage"  location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product2-Part8-64\"/>
<property name="pr210_coverage" location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product2-Part10-64\"/>

<property name="pr31_coverage" location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product3-Unit-Integ-Tests\"/>
<property name="pr32_coverage" location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product3-32\"/>
<property name="pr33_coverage" location="\\server\jenkins\jobs\Product-Test-Coverage\coverage_data\Product3-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="\\server\jenkins\jobs\Product-Test-Coverage\builds\${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提交时运行,并在每次运行之前进行编译。尽管32位和64位架构都在使用,但所有测试服务器都使用相同的Java版本。

我们在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

经过一年多的时间,突然代码覆盖率再次变得正确。很奇怪,很遗憾没有办法找出之前的错误。