{"slug": "code-coverage-net", "title": "Code Coverage .NET", "summary": "The article explains two primary methods for generating code coverage in .NET projects: using the built-in \"XPlat Code Coverage\" collector with `dotnet test --collect:\"XPlat Code Coverage\"`, or using the Coverlet MSBuild integration with `dotnet test /p:CollectCoverage=true`. After generating a Cobertura XML coverage file, developers can install and use the ReportGenerator tool to create an HTML report by running `reportgenerator -reports:coverage.cobertura.xml -targetdir:coveragereport`. Coverlet is noted as the more common approach in real .NET projects because it integrates well with CI/CD pipelines and allows setting coverage thresholds that can fail the build if coverage drops below a specified percentage.", "body_md": "For .NET projects, the most common manual way is using:\ncoverlet\ndotnet test --collect:\"XPlat Code Coverage\"\nSimplest first:\ndotnet test --collect:\"XPlat Code Coverage\"\nAfter running, you’ll see something like:\nAttachments:\n.../coverage.cobertura.xml\nThat XML file contains the coverage result.\nThen install the report generator globally:\ndotnet tool install -g dotnet-reportgenerator-globaltool\nGenerate an HTML report:\nreportgenerator -reports:**/coverage.cobertura.xml -targetdir:coveragereport\nThen open:\ncoveragereport/index.html\nYou’ll see:\nTypical enterprise flow:\ndotnet test\n-> code coverage\n-> stryker mutation test\n-> CI/CD quality gate\ndotnet test --collect:\"XPlat Code Coverage\"\nor\ndotnet test /p:CollectCoverage=true\nThen open:\ncoveragereport/index.html\nCoverlet is the more common approach in real .NET projects now because it integrates nicely with CI/CD and gives flexible reporting.\nHere’s the normal flow.\nInside your test project folder:\ndotnet add package coverlet.msbuild\nThis adds the MSBuild integration of Coverlet.\ndotnet test /p:CollectCoverage=true\nAfter running, you’ll see something like:\n+--------+------+--------+--------+\n| Module | Line | Branch | Method |\n+--------+------+--------+--------+\nand a generated file:\ncoverage.json\nUsually we use Cobertura format:\ndotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura\nThen generate HTML:\nreportgenerator -reports:coverage.cobertura.xml -targetdir:coveragereport\nOpen:\ncoveragereport/index.html\nWhenever code changes:\ndotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura\nthen:\nreportgenerator -reports:coverage.cobertura.xml -targetdir:coveragereport\nBuilt-in collector:\ndotnet test --collect:\"XPlat Code Coverage\"\nCoverlet MSBuild:\ndotnet test /p:CollectCoverage=true\nBoth are valid.\nBut Coverlet gives:\nExample threshold:\ndotnet test /p:CollectCoverage=true /p:Threshold=80\nThis FAILS the build if coverage goes below 80%.", "url": "https://wpnews.pro/news/code-coverage-net", "canonical_source": "https://dev.to/davinceleecode/code-coverage-net-2ai0", "published_at": "2026-05-23 13:17:45+00:00", "updated_at": "2026-05-23 13:33:42.179041+00:00", "lang": "en", "topics": ["developer-tools"], "entities": [".NET", "Coverlet", "ReportGenerator", "Stryker", "Cobertura"], "alternates": {"html": "https://wpnews.pro/news/code-coverage-net", "markdown": "https://wpnews.pro/news/code-coverage-net.md", "text": "https://wpnews.pro/news/code-coverage-net.txt", "jsonld": "https://wpnews.pro/news/code-coverage-net.jsonld"}}