User Tools

Site Tools


technical_notes:public:open-cover-configuration

Parent

Open Cover - Configuration with Msbuild

Open cover is used first to calculate the code coverage of unit test. This task exclude the code marked with the attribute ExcludeFromCodeCoverage.

You get a report like this :

Visited Classes 750 of 897 (83.6120401337793)
Visited Methods 4117 of 5006 (82.241310427487)
Visited Points 27429 of 31059 (88.3125664058727)
Visited Branches 4550 of 11833 (38.4517873742922)

==== Alternative Results (includes all methods including those without corresponding source) ====
Alternative Visited Classes 1012 of 1549 (65.3324725629438)
Alternative Visited Methods 6064 of 10226 (59.299823978095)

Thanks to this report the msbuild task will display a warning message or an error message according to the error and warning level setted.

Set a msbuild task for Open Cover

  <Target Name="RunOpenCover">
    <PropertyGroup>
      <Path_Bin Condition=" '$(Path_Bin)' == ''"></Path_Bin>
      <Path_DropFolder Condition=" '$(Path_DropFolder)' == ''"></Path_DropFolder>
      <Path_VS Condition=" '$(Path_VS)' == ''">$(MSBuildProgramFiles32)\Microsoft Visual Studio 10.0</Path_VS>
      <Nuget_OpenCover>$([System.IO.Directory]::GetDirectories("packages", "OpenCover*"))</Nuget_OpenCover>
      <Path_OpenCover Condition=" '$(Path_OpenCover)' == ''">$(MSBuildThisFileDirectory)\$(Nuget_OpenCover)\OpenCover.Console.exe</Path_OpenCover>
      <TestContainer Condition=" '$(TestContainer)' == ''"></TestContainer>
      <Filter Condition=" '$(Filter)' == ''"></Filter>
      <CoverageError Condition=" '$(CoverageError)' == ''">30</CoverageError>
      <CoverageWarning Condition=" '$(CoverageWarning)' == ''">85</CoverageWarning>
      <Output_Log>$(Path_DropFolder)\opencover.log</Output_Log>
      <Output_Coverage>$(Path_DropFolder)\coverage.xml</Output_Coverage>
    </PropertyGroup>
 
    <Exec Command="$(Path_OpenCover) -mergebyhash -target:&quot;$(Path_VS)\Common7\IDE\mstest.exe&quot; -targetargs:&quot;/testcontainer:$(Path_Bin)\$(TestContainer)&quot; -filter:&quot;$(Filter)&quot; -register:user -excludebyattribute:*.ExcludeFromCodeCoverage* -output:$(Output_Coverage) > $(Output_Log)" >
    </Exec>
 
    <PropertyGroup>
      <OutputOfExec>$([System.IO.File]::ReadAllText($(Output_Log)))</OutputOfExec>
      <Cover_Log>$([System.Text.RegularExpressions.Regex]::Matches($(OutputOfExec), "Visited.+", System.Text.RegularExpressions.RegexOptions.Singleline))</Cover_Log>
      <Cover_Methods>$([System.Text.RegularExpressions.Regex]::Match($(OutputOfExec), "Visited Methods [0-9]+ of [0-9]+ (.*)"))</Cover_Methods>
      <Cover_Methods_Rate_1>$([System.Text.RegularExpressions.Regex]::Match($(Cover_Methods), "\([0-9\.]+\)"))</Cover_Methods_Rate_1>
      <Cover_Methods_Rate>$([System.Text.RegularExpressions.Regex]::Match($(Cover_Methods_Rate_1), "[0-9]+"))</Cover_Methods_Rate>
    </PropertyGroup>
 
    <Message Text="$(TestContainer)" />
    <Message Text="------------------------" />
    <Message Text="$(Cover_Log)" />
    <Warning Text="[$(TestContainer)] COUVERTURE DE CODE (Methods) LIMITE : $(Cover_Methods_Rate)% (INFERIEURE A $(CoverageWarning)%)" Condition="$([System.Int32]::Parse($(Cover_Methods_Rate))) &lt; $([System.Int32]::Parse($(CoverageWarning))) and $([System.Int32]::Parse($(Cover_Methods_Rate))) &gt; $([System.Int32]::Parse($(CoverageError)))"/> 
    <Error Text="[$(TestContainer)] COUVERTURE DE CODE (Methods) DESASTREUSE : $(Cover_Methods_Rate)%(INFERIEURE A $(CoverageError)% )" Condition="$([System.Int32]::Parse($(Cover_Methods_Rate))) &lt; $([System.Int32]::Parse($(CoverageError)))"/>
  </Target>

You can use this command line

msbuild PDTUGS.targets /t:RunOpenCover /p:Path_DropFolder="bin\Debug\TU-Coverage" /p:Path_Bin="bin\Debug" /p:TestContainer="Projet.Tests.dll" /p:CoverageError=83 /p:CoverageWarning=85 /p:Path_VS="C:\Program Files (x86)\Microsoft Visual Studio 11.0"
technical_notes/public/open-cover-configuration.txt · Last modified: 2014/02/12 15:02 by Fabien Arcellier