Launch price: every exam for $29.99 a year, for the first 300 buyers. See the price

CertStreak
Menu

Implement code coverage analysis

A worked example

Single answer

A .NET 8 test project references the coverlet.msbuild package, and no runsettings file is used. An earlier pipeline step has already built the solution with --configuration Release. The next script step must meet all of these requirements:

  • It must run the existing tests without rebuilding.
  • It must collect code coverage while the tests run.
  • It must write the coverage report in OpenCover format.

Which command should the script step run?

Choose one answer
Why

Code coverage is captured while tests execute, so the step must use dotnet test, with --no-build so it reuses the earlier Release build. With Coverlet's MSBuild integration, /p:CollectCoverage=true enables coverage analysis and /p:CoverletOutputFormat=opencover sets the report format. Each wrong option misses one requirement: the wrong format, coverage not enabled, or no test run at all.

  • Correct: dotnet test --no-build --configuration Release /p:CollectCoverage=true /p:CoverletOutputFormat=opencover. This command runs the already-built tests. CollectCoverage=true turns on Coverlet coverage analysis, and CoverletOutputFormat=opencover sets the report format, so it meets every requirement.
  • Not correct: dotnet test --no-build --configuration Release /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura. This command collects coverage, but the output format argument sets Cobertura instead of the required OpenCover format.
  • Not correct: dotnet test --no-build --configuration Release /p:CoverletOutputFormat=opencover. Coverage analysis has to be enabled explicitly with /p:CollectCoverage=true. Without that argument, setting only the output format does not collect coverage.
  • Not correct: dotnet build --configuration Release /p:CollectCoverage=true /p:CoverletOutputFormat=opencover. dotnet build compiles the solution again and does not run tests. Coverage is measured on code executed during automated tests, so no test coverage is produced.

Practise this topic until it's automatic.

Ten questions a day, every answer explained, and misses that come back until you beat them.

Start free