You can also build and test with code coverage for a particular test project rather than for the whole repo with the `/p:Coverage=true` property:
- dotnet test -f net5.0 /p:Coverage=true
+ dotnet build /t:Test /p:Coverage=true
The results for this one library will then be available in this index.htm file, where $(OutDir) is the directory where the binaries were generated.
For example, to build, test, and get code coverage results for the System.Diagnostics.Debug library, from the root of the repo one can do:
- dotnet test src\libraries\System.Diagnostics.Debug -f net5.0 /p:Coverage=true
+ dotnet build src\System.Diagnostics.Debug\tests /t:Test /p:Coverage=true
And then once the run completes:
</PropertyGroup>
</Target>
+ <!-- TODO remove when https://github.com/coverlet-coverage/coverlet/issues/834 is fixed. -->
+ <Target Name="AddCoverageCommand"
+ BeforeTargets="GenerateRunScript"
+ DependsOnTargets="SetupCoverageFilter"
+ Condition="'$(Coverage)' == 'true'">
+ <PropertyGroup>
+ <CoverageOutputPath Condition="'$(CoverageOutputPath)' == ''">coverage.opencover.xml</CoverageOutputPath>
+ <CoverageReportInputPath Condition="'$(CoverageReportInputPath)' == ''">$(CoverageOutputPath)</CoverageReportInputPath>
+ <CoverageReportDir Condition="'$(CoverageReportDir)' == ''">$([MSBuild]::NormalizeDirectory('$(OutDir)', 'report'))</CoverageReportDir>
+
+ <RunScriptCommand>"$(DotNetTool)" tool run coverlet "$(TargetFileName)" --target "$(RunScriptHost)" --targetargs "$(RunScriptCommand.Replace('"$(RunScriptHost)"', ''))" --format "opencover" --output "$(CoverageOutputPath)" --verbosity "normal" --use-source-link</RunScriptCommand>
+ <RunScriptCommand Condition="'@(CoverageExcludeByFile)' != ''">$(RunScriptCommand) --exclude-by-file @(CoverageExcludeByFile -> '"%(Identity)"', ' --exclude-by-file ')</RunScriptCommand>
+ <RunScriptCommand Condition="'@(CoverageIncludeDirectory)' != ''">$(RunScriptCommand) --include-directory @(CoverageIncludeDirectory -> '"$(RunScriptHostDir)%(Identity)"', ' --include-directory ')</RunScriptCommand>
+ <RunScriptCommand>$(RunScriptCommand) --include @(CoverageInclude -> '"[%(Identity)]*"', ' --include ')</RunScriptCommand>
+ <CoverageReportCommandLine>"$(DotNetTool)" tool run reportgenerator "-reports:$(CoverageReportInputPath)" "-targetdir:$(CoverageReportDir.TrimEnd('\/'))" "-reporttypes:Html" "-verbosity:Info"</CoverageReportCommandLine>
+ </PropertyGroup>
+
+ <!-- Skip generating individual reports if a full report is generated. -->
+ <ItemGroup Condition="'$(BuildAllProjects)' != 'true' and '$(SkipCoverageReport)' != 'true'">
+ <PostRunScriptCommands Include="$(CoverageReportCommandLine)" />
+ </ItemGroup>
+ </Target>
+
+ <!-- Build a coverage report if building an individual library with Coverage true. -->
<Target Name="GenerateCoverageReport"
- Condition="'$(Coverage)' == 'true' and '$(SkipCoverageReport)' != 'true'"
+ Condition="'$(Coverage)' == 'true' and '$(BuildAllProjects)' != 'true' and '$(SkipCoverageReport)' != 'true'"
AfterTargets="VSTest">
<ItemGroup Condition="'$(CoverageReportInputPath)' == ''">
<CoverageOutputFile Include="$(OutDir)TestResults\*\coverage.opencover.xml" />
<!-- For tests we want to continue running if a test run failed. -->
<TestContinueOnError>ErrorAndContinue</TestContinueOnError>
<TraversalGlobalProperties>BuildAllProjects=true</TraversalGlobalProperties>
- <CoverageReportInputPath>$(ArtifactsBinDir)*.Tests\TestResults\*\coverage.opencover.xml</CoverageReportInputPath>
+ <CoverageReportInputPath>$(ArtifactsBinDir)\*.Tests\**\coverage.opencover.xml</CoverageReportInputPath>
<CoverageReportDir>$(ArtifactsDir)coverage</CoverageReportDir>
<EnableCoverageSupport>true</EnableCoverageSupport>
</PropertyGroup>