Skip to content

Instantly share code, notes, and snippets.

@wildmichael
Created September 5, 2013 09:06
Show Gist options
  • Save wildmichael/6447784 to your computer and use it in GitHub Desktop.
Save wildmichael/6447784 to your computer and use it in GitHub Desktop.
Run coverage analysis from MSBuild
<?xml version="1.0" encoding="utf-8"?>
<Project
DefaultTargets="Report"
ToolsVersion="4.0"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- versions of the required tools. ADJUST TO ACTUAL VERSION NUMBERS! -->
<xUnitVersion>1.9.2</xUnitVersion>
<OpenCoverVersion>4.5.1923</OpenCoverVersion>
<ReportGeneratorVersion>1.9.0.0</ReportGeneratorVersion>
<!-- locations of required tools -->
<nuGetDir>..\packages\</nuGetDir>
<xUnitRunnerDir>$(nuGetDir)xunit.runners.$(xUnitVersion)\tools\</xUnitRunnerDir>
<xUnitRunnerName>xunit.console.clr4.x86.exe</xUnitRunnerName>
<xUnitRunner>$(xUnitRunnerDir)$(xUnitRunnerName)</xUnitRunner>
<OpenCoverDir>$(nuGetDir)OpenCover.$(OpenCoverVersion)\</OpenCoverDir>
<ReportGeneratorDir>$(nuGetDir)ReportGenerator.$(ReportGeneratorVersion)\</ReportGeneratorDir>
<ReportGenerator>$(ReportGeneratorDir)ReportGenerator.exe</ReportGenerator>
<!-- property required by the Opencover.targets file. Note that
the path is *relative* to said file!-->
<OpenCoverMSBuildTasksPath>.\</OpenCoverMSBuildTasksPath>
<!-- Filters to be applied when creating the coverage report.
ADJUST TO YOUR NAMESPACE/ASSEMBLY NAMES! -->
<CoverageFilters>+[*]FOO.*;-[*]FOO.**.Annotations.*;
-[*.Test]*</CoverageFilters>
<!-- Output directory for the coverage information.
DANGER: The "Clean" target deletes this directory! -->
<ReportsPath>report\</ReportsPath>
<!-- Build configuration defaults-->
<Configuration
Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
</PropertyGroup>
<!-- Build configuration dependent properties -->
<PropertyGroup
Condition=" '$(OutputPath)' == '' And '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup
Condition=" '$(OutputPath)' == '' And '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
</PropertyGroup>
<!-- Get the definition of the OpenCover task -->
<Import Project="$(OpenCoverDir)MSBuild\Opencover.targets"/>
<ItemGroup>
<!-- Test projects -->
<TestProjects Include="..\*.Test\*.Test.*proj" />
</ItemGroup>
<!-- First build the tests -->
<Target Name="BuildTests" Outputs="@(TestProjectOutput)">
<MSBuild
Projects="@(TestProjects)"
Targets="Build"
Properties="Configuration=$(Configuration);Optimize=$(Optimize)">
<Output
ItemName="TestProjectOutputs"
TaskParameter="TargetOutputs"/>
</MSBuild>
</Target>
<!-- Then run them using the coverage tool, batch on target -->
<Target
Name="Coverage" DependsOnTargets="_ComputeCoverageOutput"
Inputs="%(CoverageOutputs.Input)"
Outputs="%(CoverageOutputs.Identity)">
<MakeDir Directories="$(ReportsPath)"/>
<OpenCover
Target="$(xUnitRunner)"
Output="%(CoverageOutputs.Identity)"
TargetArgs="%(CoverageOutputs.Input) /noshadow"
Filter="$(CoverageFilters)"
ToolPath="$(OpenCoverDir)"
/>
</Target>
<Target Name="_ComputeCoverageOutput" DependsOnTargets="BuildTests">
<ItemGroup>
<CoverageOutputs
Include="$(ReportsPath)%(TestProjectOutputs.Filename).opencover">
<!-- Add the assembly as "Input" meta-data -->
<Input>%(TestProjectOutputs.Identity)</Input>
</CoverageOutputs>
</ItemGroup>
</Target>
<!-- Finally generate the report -->
<ItemGroup>
<ReportOutput Include="$(ReportsPath)index.htm"/>
</ItemGroup>
<Target Name="Report"
Inputs="@(CoverageOutputs)"
DependsOnTargets="Coverage"
Outputs="@(ReportOutput)">
<Exec Command='$(ReportGenerator) "-reports:@(CoverageOutputs)" "-targetdir:$(ReportsPath)"' />
</Target>
<!-- And optionally show it -->
<Target Name="See" DependsOnTargets="Report">
<Exec Command="start %(ReportOutput.Identity)" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment