Skip to content

Commit

Permalink
Add a script for generating test coverage reports
Browse files Browse the repository at this point in the history
The script runs the unit tests via OpenCover, which also generates a
report in XML format. Then some HTML files are generated by parsing the
XML file, which are then served by `dotnet serve` tool.

The script is pretty self-explanatory. It depends on a few CLI tools:
xUnit, OpenCover (portable), codecov. These are available on Chocolatey.
It also requires a .NET Core global tool (which can be installed via
.NET Core CLI): `dotnet-reportgenerator-globaltool`. These are explained
in the script in the comments at the top.

The script is, unfortunately, for Windows only. This is due to OpenCover
being available only for Windows. I have tried some cross-platform
coverage tools (for example, @coverlet-coverage), but was not satisfied.
The problem with Coverlet is that after generating coverage for tests
covering multiple namespaces, it calculates the coverage by calculating
the simple arithmetic mean — not a weighted arithmetic mean, which would
take the number of LOC of each namespace into account.
  • Loading branch information
maacpiash committed Jul 16, 2020
1 parent 50c0d56 commit 98c3d45
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Created by https://www.gitignore.io/api/linux,macos,windows,aspnetcore
# Edit at https://www.gitignore.io/?templates=linux,macos,windows,aspnetcore

Expand Down Expand Up @@ -345,3 +344,4 @@ $RECYCLE.BIN/
src/global.json
tests/global.json
.env
report
23 changes: 23 additions & 0 deletions scripts/generate-test-coverage-report.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# The following tools must be installed for this to work:
# `xunit`, `opencover.portable`, `codecov`, and `dotnet-reportgenerator-globaltool`

# These can be installed via the following commands:
# choco install xunit opencover.portable codecov
# dotnet tool install --global dotnet-reportgenerator-globaltool

OpenCover.Console.exe `
-register:user `
-target:"C:\Program Files\dotnet\dotnet.exe" `
-targetargs:test `
-filter:"+[KonSchool*]* -[KonSchool.Tests*]*" `
-excludebyfile:"SchoolService.cs" `
-output:".\report\KonSchool_coverage.xml" `
-oldstyle

reportgenerator -reports:.\report\KonSchool_coverage.xml -reporttypes:Html -targetdir:.\report
dotnet serve -d .\report -p 5050

# The following command opens the report in the default web browser:
# Invoke-Expression “cmd.exe /C start http://localhost:5050"

# Or, just ctrl-click on the link above 😁

0 comments on commit 98c3d45

Please sign in to comment.