Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enable xunit.console code coverage #35894

Merged
merged 1 commit into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you figure out why ArtifactsBinDir doesn’t contain a trailing \?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea

<CoverageReportDir>$(ArtifactsDir)coverage</CoverageReportDir>
<EnableCoverageSupport>true</EnableCoverageSupport>
</PropertyGroup>
Expand Down