Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

MSBuild Support

Konstantin Veretennicov edited this page May 3, 2016 · 1 revision

You can use standard Exec MSBuild task to run OpenCover from your MSBuild script, but it will require from you to craft command carefully taking into account that you need to escape parameters. To simplify use of OpenCover from MSBuild there is a custom task to run OpenCover.

Integration with MSBuild

Custom task assembly could be found in MSBuild sub-folder in OpenCover distribution. To add task to your MSBuild script, attach task assembly using UsingTask task

<UsingTask AssemblyFile="<PUT YOUR PATH TO ASSEMBLY HERE>\OpenCover.MSBuild.dll" TaskName="OpenCover.MSBuild.OpenCover" />

or Import OpenCover.targets file from MSBuild folder and provide path to task assembly using OpenCoverMSBuildTasksPath property.

<PropertyGroup>
  <OpenCoverMSBuildTasksPath>PUT YOUR PATH TO ASSEMBLY HERE</OpenCoverMSBuildTasksPath>
</PropertyGroup>
<Import Project="$(OpenCoverMSBuildTasksPath)\OpenCover.targets" />

After that you can call OpenCover from your MSBuild script using OpenCover task. Example:

<OpenCover
            ReturnTargetCode="True"
            ToolPath="$(ToolsFolder)\OpenCover\"
            Filter="+[*]* -[*.Tests]* -[Autofac*]* -[Newtonsoft.Json]*"
            ExcludeByAttribute="System.ObsoleteAttribute"
            ToolExe="OpenCover.Console.exe"
            Register="True"
            ShowUnvisited="True"
            Target="$(ToolsFolder)\nunit\nunit3-console.exe"
            TargetWorkingDir="$(TestsBuildOutputFolder)"
            TargetArgs="@(TestAssemblies->'&quot;$(TestsBuildOutputFolder)\%(filename)%(extension)&quot;', ' ') --result=&quot;$(TestResultsFolder)\unit-test-results.xml&quot; --dispose-runners $(Traits)"
            Output="$(CoverageResultsFolder)\coverage-results.xml" >
            <Output TaskParameter="ExitCode" PropertyName="TestExitCode"/>
        </OpenCover>

Task Parameters

Except base ToolTask properties specified here, there are a lot of OpenCover-specific parameters. All of them map to one of OpenCover.Console.exe parameters and for details you should read Usage document.

  • Target (string) - mappend to target command-line parameter.
  • ReturnTargetCode (bool) - True will add returntargetcode command-line parameter. Default is False.
  • TargetCodeOffset (int) - mapped to opencoverreturncodeoffset command-line parameter. Not valid without ReturnTargetCode=True.
  • TargetArgs (string) - mapped to targetargs command-line parameter. Don't forget to quote separate arguments with &quot; if they could contain spaces.
  • TargetWorkingDir (string) - mapped to targetdir command-line parameter.
  • Service (bool) - True will add service command-line parameter. Default is False.
  • ShowUnvisited (bool) - True will add showunvisited command-line parameter. Default is False.
  • Register (bool) - True will add register:user command-line parameter. Default is False.
  • Output (string) - mapped to output command-line parameter.
  • MergeByHash (bool) - True will add mergebyhash command-line parameter.
  • Filter (list) - list of filters, mapped to filter command-line parameter.
  • ExcludeByFile (list) - list of files, mapped to excludebyfile command-line parameter.
  • ExcludeByAttribute (list) - list of filters, mapped to excludebyattribute command-line parameter.
  • CoverByTest (list) - list of filters, mapped to coverbytest command-line parameter.
  • DefaultFilters (bool) - False will add nodefaultfilters command-line parameter. Default is False.

Missing Parameters

You could've noticed that OpenCover task doesn't support some command-line parameters. If you need any of them, feel free to send pull request or create an issue.