Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 6 of 8

Code Coverage & Test Project Setup

Code coverage measures what percentage of your production code is exercised by tests. Coverlet integrates with `dotnet test` to generate coverage reports. ReportGenerator converts them to HTML. The goal for production APIs: 80%+ coverage on service and controller layers.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Test Project Setup & Coverage

Test Project Setup & Coverage
# ━━ Test project structure ━━
# MyApp.Tests/                  ← unit tests (Moq, no external dependencies)
# MyApp.IntegrationTests/       ← integration tests (WebApplicationFactory)
# MyApp.Architecture.Tests/     ← NetArchTest.Rules (enforce Clean Architecture rules)

# ━━ Create test projects ━━
dotnet new xunit -n MyApp.Tests --framework net8.0
dotnet new xunit -n MyApp.IntegrationTests --framework net8.0
dotnet sln add MyApp.Tests/MyApp.Tests.csproj
dotnet sln add MyApp.IntegrationTests/MyApp.IntegrationTests.csproj

# Add project references
dotnet add MyApp.Tests/ reference MyApp.Application/
dotnet add MyApp.IntegrationTests/ reference MyApp.Api/

# ━━ Required packages ━━
dotnet add MyApp.Tests/ package xunit
dotnet add MyApp.Tests/ package xunit.runner.visualstudio
dotnet add MyApp.Tests/ package Moq
dotnet add MyApp.Tests/ package FluentAssertions
dotnet add MyApp.Tests/ package coverlet.collector
dotnet add MyApp.Tests/ package Microsoft.NET.Test.Sdk

dotnet add MyApp.IntegrationTests/ package Microsoft.AspNetCore.Mvc.Testing
dotnet add MyApp.IntegrationTests/ package Testcontainers.PostgreSql
dotnet add MyApp.IntegrationTests/ package FluentAssertions

# ━━ Run tests with coverage ━━
dotnet test --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura

# Generate HTML report
dotnet tool install --global dotnet-reportgenerator-globaltool
reportgenerator -reports:"**/coverage.cobertura.xml" -targetdir:coverage-report -reporttypes:Html

# ━━ Coverage enforcement - fail CI if below 80% ━━
# In MyApp.Tests.csproj:
# <PropertyGroup>
#   <CollectCoverage>true</CollectCoverage>
#   <CoverletOutputFormat>cobertura</CoverletOutputFormat>
#   <Threshold>80</Threshold>        ← fail if coverage drops below 80%
#   <ThresholdType>line</ThresholdType>
#   <ExcludeByFile>**/Migrations/*.cs,**/Program.cs</ExcludeByFile>
# </PropertyGroup>

# ━━ Architecture tests (NetArchTest.Rules) ━━
dotnet add MyApp.Architecture.Tests/ package NetArchTest.Rules

// Architecture enforcement:
[Fact]
public void Domain_ShouldNotDependOnInfrastructure()
{
    var result = Types.InAssembly(typeof(Product).Assembly)
        .ShouldNot().HaveDependencyOn("MyApp.Infrastructure")
        .GetResult();

    result.IsSuccessful.Should().BeTrue(
        because: "Domain layer must not reference Infrastructure");
}

Tip

Tip

Practice Code Coverage Test Project Setup in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

.NET provides a unified platform for building various application types

Practice Task

Note

Practice Task - (1) Write a working example of Code Coverage Test Project Setup from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Code Coverage Test Project Setup is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.

Topics in This Module

0/8 done