Skip to content

Commit

Permalink
Re-enable xunit.console code coverage (#35894)
Browse files Browse the repository at this point in the history
Re-enable xunit console code coverage measurement until
coverlet-coverage/coverlet#834 is fixed.
  • Loading branch information
ViktorHofer committed May 6, 2020
1 parent 5eaac68 commit 85bfbfc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
8 changes: 7 additions & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
"version": 1,
"isRoot": true,
"tools": {
"coverlet.console": {
"version": "1.7.1",
"commands": [
"coverlet"
]
},
"dotnet-reportgenerator-globaltool": {
"version": "4.5.2",
"version": "4.5.8",
"commands": [
"reportgenerator"
]
Expand Down
4 changes: 2 additions & 2 deletions docs/workflow/building/libraries/code-coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ This runs the tests and generates the full code coverage report. The resulting i

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.

$(OutDir)\report\index.htm

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:

Expand Down
26 changes: 25 additions & 1 deletion eng/testing/coverage.targets
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,32 @@
</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('&quot;$(RunScriptHost)&quot;', ''))" --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" />
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- 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>
Expand Down

0 comments on commit 85bfbfc

Please sign in to comment.