Search code examples
c#azure-devopscode-coveragexunitcoverlet

Require 100% code coverage for ASP.NET Core web application controllers in Azure DevOps build pipeline


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://learn.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://learn.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?


Solution

  • 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