Skip to content

Commit

Permalink
Switch to @coverlet-coverage for test coverage
Browse files Browse the repository at this point in the history
This is due to OpenCover generating inconsistent reports: some LOCs were
shown not covered by tests, despite actually being covered. For example,
some edge cases in Models were covered by 0846e27 — but the coverage did
not go up for some unknown reason(s).

The global CLI tool, `coverlet.console`, has been used for this project.

There are now separate scripts for generating reports and uploading test
coverage — for Windows and Unix-like platforms.
  • Loading branch information
maacpiash committed Jul 23, 2020
1 parent 08edd49 commit 2f82d53
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,4 @@ src/global.json
tests/global.json
.env
report
coverage*xml
7 changes: 4 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ init:

before_build:
- dotnet --info
- choco install xunit opencover.portable codecov
- choco install codecov
- dotnet tool install --global coverlet.console
- dotnet restore

build:
Expand All @@ -27,8 +28,8 @@ artifacts:
- path: dist

test_script:
- OpenCover.Console.exe -register:user -target:"C:/Program Files/dotnet/dotnet.exe" -targetargs:test -filter:"+[KonSchool*]* -[KonSchool.Tests*]*" -excludebyfile:".\src\Services\SchoolService.cs" -output:".\KonSchool_coverage.xml" -oldstyle
- codecov -f "KonSchool_coverage.xml" -t %CODECOV_TOKEN%
- coverlet tests/bin/Debug/netcoreapp3.1/KonSchool.Tests.dll --target "dotnet" --targetargs "test tests/KonSchool.Tests.csproj --no-build" --format opencover
- codecov -f "coverage.opencover.xml" -t %CODECOV_TOKEN%

deploy:
provider: WebDeploy
Expand Down
34 changes: 21 additions & 13 deletions scripts/generate-test-coverage-report.ps1
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
# The following tools must be installed for this to work:
# `xunit`, `opencover.portable`, `codecov`, and `dotnet-reportgenerator-globaltool`
# `coverlet` and `dotnet-reportgenerator-globaltool`

# These can be installed via the following commands:
# choco install xunit opencover.portable codecov
# dotnet tool install --global coverlet.console
# 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
# First, build the projects
dotnet build

# Now, let's run the tests and collect the coverage
coverlet tests/bin/Debug/netcoreapp3.1/KonSchool.Tests.dll `
--target "dotnet" `
--targetargs "test tests/KonSchool.Tests.csproj --no-build" `
--format opencover

reportgenerator -reports:./coverage.opencover.xml -reporttypes:Html -targetdir:./report

# After generating the report, you can either open `report/index.html` file
# directly in your browser, or you can use `dotnet-serve` tool to serve
# the `report` directory.

# You can install the tool by the following command:
# dotnet tool install --global dotnet-serve

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"
Expand Down
31 changes: 31 additions & 0 deletions scripts/generate-test-coverage-report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# The following tools must be installed for this to work:
# `coverlet` and `dotnet-reportgenerator-globaltool`

# These can be installed via the following commands:
# dotnet tool install --global coverlet.console
# dotnet tool install --global dotnet-reportgenerator-globaltool

# First, build the projects
dotnet build

# Now, let's run the tests and collect the coverage
coverlet tests/bin/Debug/netcoreapp3.1/KonSchool.Tests.dll \
--target "dotnet" \
--targetargs "test tests/KonSchool.Tests.csproj --no-build" \
--format opencover

reportgenerator -reports:./coverage.opencover.xml -reporttypes:Html -targetdir:./report

# After generating the report, you can either open `report/index.html` file
# directly in your browser, or you can use `dotnet-serve` tool to serve
# the `report` directory.

# You can install the tool by the following command:
# dotnet tool install --global dotnet-serve

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 😁
4 changes: 4 additions & 0 deletions scripts/upload-test-coverage-report.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This requires Codecov CLI tool. Install it by the following command:
# choco install codecov

codecov -f "coverage.opencover.xml" -t "token"
2 changes: 2 additions & 0 deletions scripts/upload-test-coverage-report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export CODECOV_TOKEN="token"
bash <(curl -s https://codecov.io/bash)

0 comments on commit 2f82d53

Please sign in to comment.