0
votes

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:

https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-code-coverage?tabs=windows#generate-reports

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:

enter image description here

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://docs.microsoft.com/en-us/azure/devops/pipelines/test/review-code-coverage-results?view=azure-devops

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?

1
You can try to add --filter option to dotnet test command. For example: If you are using the MSTest framework, then you can use the ClassName property like this: dotnet test --filter ClassName=classA | ClassName=classB. Specify all test classes under Project.Web.Controllers namespace in the filter. Here is the document you can refer to.Hugh Lin
@HughLin-MSFT That only filters the tests that should be ran, not actual code coverage.Ogglas

1 Answers

1
votes

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:

enter image description here

Fail:

enter image description here