In our project we have a requirement that all controller methods should have at least one test.
Our build fails if we have a test that fails but right now we are using this guide to check code coverage manually:
Basically this means running two commands:
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput="Path\To\TestProject\TestResults\coverage.cobertura.xml"
reportgenerator "-reports:Path\To\TestProject\TestResults\coverage.cobertura.xml" "-targetdir:coveragereport" -reporttypes:Html
Note: If you run dotnet test --collect:"XPlat Code Coverage"
you can get a coverage.cobertura.xml
file but if you use msbuild with /p:CollectCoverage=true
you will have to add the package dotnet add package coverlet.msbuild
to the test project once.
https://github.com/coverlet-coverage/coverlet/issues/201
We then get a report like this:
Our line coverage in this case is not great but our controllers have 100% line coverage. It would be OK to check that a specific namespace like Project.Web.Controllers
has 100% line coverage.
We can not use the normal code coverage results to fail a build since we only want to fail a build if controllers are not tested.
https://gunnarpeipman.com/azure-devops-check-code-coverage/
https://stackoverflow.com/a/60894835/3850405
Is there any way to do this nicely or do we need to read the coverage.cobertura.xml
file and look at <class name="Project.Web.Controllers
for example?
Found a command for it!
The key was using /p:Include="[*]Project.Web.Controllers.*" /p:Threshold=100 /p:ThresholdType=line
Complete command:
dotnet test /p:CollectCoverage=true /p:Include="[*]Project.Web.Controllers.*" /p:Threshold=100 /p:ThresholdType=line
https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md
Pass:
Fail: