Getting a Report from PartCover displayed


I'm currently trying to run my unittests from Gallio with PartCover to generate a unit test.
The idea is that Gallio runs the unit tests and that PartCover generates the coverage report. After long search I was able to come up with the following scripts:

  <target name="TestWithPartCover">
    <!-- Defines the properties needed for this test -->
    <property name="Test.MinimumCoverage" value="0.9" />
    <!-- Run the test with PartCover -->
    <echo message="Starting test with coverage for ${Tests.TestedAssemblyName}" />
    <echo message="-- including: [*{Tests.TestedAssemblyName*]*" />
    <exec program="${Paths.Tools}\PartCover\Partcover.exe" failonerror="true">
      <arg line="--register" />                                                       <!-- Register the COM components to the current user. -->
      <arg line="--target &quot;${Paths.Tools}\Gallio\Gallio.Echo.exe&quot;" />       <!-- target is the path to Gallio.Echo that will run the unit test. -->
      <arg line="--target-work-dir &quot;${AssemblyToTestLocation}&quot;"/>           <!-- target-work-dir is the path to the folder containing the test DLL. -->
      <arg line="--target-args &quot;/r:IsolatedAppDomain ${AssemblyToTest}&quot;" /> <!-- target-args is the name of the DLL to test. -->
      <arg line="--include [*${Tests.TestedAssemblyName}*]*" />                       <!-- include defines the namespaces and classes to include into the test. -->
      <arg line="--output &quot;${Paths.Output}\Coverage.xml&quot;" />                <!-- output is the location we will save the report file. -->
      <arg line="--report &quot;${Paths.Tools}\PartCover\xlst\Report By Assembly.xslt&quot;=>&quot;${Paths.Output}\CoverageSummary.xml&quot;" />

  <!-- Generate the report and put it on TeamCity -->
  <target name="PartCoverReport">
    <!-- Read out the amount of lines covered and divide it by the number of hits, more than 90 is passed. -->
    <xmlpeek file="${Paths.Output}\Coverage.xml" xpath="sum(//Type/Method/pt/@len)" property="Test.NumLines1" failonerror="false"/>
    <xmlpeek file="${Paths.Output}\Coverage.xml" xpath="sum(//Type/Method[count(pt)=0]/@bodysize)" property="Test.NumLines2" failonerror="false"/>
    <xmlpeek file="${Paths.Output}\Coverage.xml" xpath="sum(//Type/Method/pt[@visit>0]/@len)" property="Test.LinesCovered" failonerror="false"/>
    <property name="Test.ActualCoverage" value="${double::parse(Test.LinesCovered) / (double::parse(Test.NumLines1) + double::parse(Test.NumLines2)}" />
    <fail if="${double::parse(Test.ActualCoverage) &lt; double::parse(Test.MinimumCoverage)}" message="The solution currently has ${double::parse(Test.ActualCoverage) * 100}% coverage, less than the required 90%" />

    <!-- Check if we need to include the querycount -->
    <if test="${Tests.CountQueries == 'yes'}">
      <readregistry property="TotalQueryCount" key="Software\Tenforce\TestStatistics\TotalQueryCount" hive="LocalMachine" />
      <echo message="##teamcity[buildStatisticValue key='totalQueryCount' value='${TotalQueryCount}']" />
      <readregistry property="AverageQueryCountPerTestRounded" key="Software\Tenforce\TestStatistics\AverageQueryCountPerTestRounded" hive="LocalMachine" />
      <echo message="##teamcity[buildStatisticValue key='averageQueryCountPerTest' value='${math::round(AverageQueryCountPerTestRounded)}']" />
      <readregistry property="AverageQueryCountPerTest" key="Software\Tenforce\TestStatistics\AverageQueryCountPerTest" hive="LocalMachine" />
      <echo message="##teamcity[buildStatus text='{build.status.text}, coverage: ${CoveragePercent}%, queries: ${TotalQueryCount}-${AverageQueryCountPerTest}']" />

    <!-- If no query count is needed, then just display the output -->
    <if test="${Tests.CountQueries != 'yes'}">
      <echo message="##teamcity[buildStatus text='{build.status.text}, coverage: ${Test.ActualCoverage}%']" />

This script runs the unit tests and generates the PartCover xml output file containing all the information. I'm not able to run the xmlpeek commands as they don't seem to like the sum() function.
Now I want to transform the outputted XML file with an XLST file into a really basic XML so I can readout the coverage. How would I do this in a nant task?

1 comment
Comment actions Permalink


TeamCity can gather statistics from PartCover automatically.
Look at Manually Configuring Reporting Coverage page.
In your case it can be done by next service message

##teamcity[importData type='dotNetCoverage' tool='partcover' path='${Paths.Output}\Coverage.xml']


Please sign in to leave a comment.