Given a .Net 5 solution with multiple xUnit test projects I can run dotnet test
from the root of the solution and it will run all the tests.
I would like to generate reports so based on https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=windows#integrate-with-net-test I run dotnet test --collect:"XPlat Code Coverage"
which generates a coverage.cobertura file per test project.
Based on https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=windows#generate-reports I know that I can install the dotnet-reportgenerator-globaltool
tool and get a visual HTML report based on each coverage.cobertura file.
But I want to add a CI pipeline where I want to make the pipeline fail when the line coverage is below x %.
Given the following sample Gitlab CI configuration
image: mcr.microsoft.com/dotnet/sdk:5.0
stages:
- tests
tests:
stage: tests
script:
- dotnet test --collect:"XPlat Code Coverage"
how can I gather all the generated coverage.cobertura.xml
files, read the line coverage and let the pipeline fail if the value is below e.g. 80%?
Example:
tests:
stage: tests
script:
- dotnet test --collect:"XPlat Code Coverage"
# for each coverage.cobertura file found in the test projects
# parse the file
# read the line coverage
# fail if the value is less than 80 percent
It would be nice if I don't have to reinvent the wheel if tools like xUnit already provide such functionality!
Edit:
I know that I could also use the allow_failure keyword to leave this stage in a warning state. This would be fine for me, I just want to know how to read the required information from the generated reports and validate them to decide if that stage should pass, fail or be unstable.