Testing in CI

suggest change

Automated testing in Continuous Integration (CI) pipelines ensures that code modifications do not introduce regressions, break public contracts, or degrade code coverage. In modern .NET, dotnet test serves as the universal test execution runner across GitHub Actions, Azure Pipelines, and GitLab CI.


1. The dotnet test Command

The .NET CLI executes test projects (built with xUnit, NUnit, or MSTest) via dotnet test:

# Basic Release execution with readable test output
dotnet test --configuration Release --verbosity normal

# Fail if any test fails, without restoring packages or rebuilding (if previously built)
dotnet test --configuration Release --no-build --no-restore

Filtering Tests in CI

In multi-stage pipelines, you can run fast unit tests on pull requests and defer slow integration tests to nightly builds using test filters:

# Filter by test category / trait
dotnet test --filter "Category=Unit"

# Exclude slow tests
dotnet test --filter "Category!=Integration"

# Filter by namespace or class name
dotnet test --filter "FullyQualifiedName~OrderServiceTests"

In code, categorize your tests using framework-specific attributes:


2. Generating Test Result Reports

CI servers parse test result files to display visual test summaries and track failing tests over time.

Producing TRX (Visual Studio / Azure DevOps) Reports

dotnet test --logger "trx;LogFileName=test-results.trx" --results-directory ./TestResults

Reporting Results Directly to GitHub Actions

Install the GitHub Actions test logger NuGet package in your test project:

<PackageReference Include="GitHubActionsTestLogger" Version="3.0.5" />

Then run:

dotnet test --logger "GitHubActions"

3. Collecting Code Coverage with Coverlet

Coverlet is the cross-platform code coverage library for .NET. --collect:"XPlat Code Coverage" needs the coverlet.collector package in the test project; the /p:CollectCoverage properties need coverlet.msbuild:

# Collect coverage in Cobertura format
dotnet test --collect:"XPlat Code Coverage" --results-directory ./TestResults

# Enforce a minimum coverage threshold (e.g. 80%)
dotnet test /p:CollectCoverage=true /p:Threshold=80 /p:ThresholdType=line

If test coverage drops below the threshold, dotnet test exits with a non-zero exit code, automatically failing the CI pipeline.


4. Complete GitHub Actions CI Workflow

Here is a production-ready .github/workflows/ci.yml configuration:

name: .NET CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build-and-test:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout Code
      uses: actions/checkout@v4

    - name: Setup .NET SDK
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: '10.0.x'

    - name: Cache NuGet Packages
      uses: actions/cache@v4
      with:
        path: ~/.nuget/packages
        key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/*.props') }}
        restore-keys: |
          ${{ runner.os }}-nuget-

    - name: Restore Dependencies
      run: dotnet restore

    - name: Build Solution
      run: dotnet build --no-restore --configuration Release /p:TreatWarningsAsErrors=true

    - name: Run Tests with Coverage
      run: >
        dotnet test --no-build --configuration Release
        --logger "trx;LogFileName=results.trx"
        --collect:"XPlat Code Coverage"
        --results-directory ./TestResults

    - name: Publish Test Results
      uses: dorny/test-reporter@v1
      if: always()
      with:
        name: .NET Test Report
        path: 'TestResults/*.trx'
        reporter: visualstudio-trx

5. Integration Testing in CI with Testcontainers

For integration tests that depend on databases (such as SQL Server, PostgreSQL, or Redis), use Testcontainers for .NET to spin up ephemeral Docker containers automatically during CI:

using System.Threading.Tasks;
using Testcontainers.MsSql;
using Xunit;

public class DatabaseIntegrationTest : IAsyncLifetime
{
    private readonly MsSqlContainer _dbContainer =
        new MsSqlBuilder("mcr.microsoft.com/mssql/server:2022-latest").Build();

    public async Task InitializeAsync()
    {
        // Spins up Docker container inside CI runner
        await _dbContainer.StartAsync();
    }

    [Fact]
    public async Task ShouldConnectAndQueryDatabase()
    {
        string connectionString = _dbContainer.GetConnectionString();
        // Run database migrations and execute assertions against live database...
        Assert.NotNull(connectionString);
    }

    public async Task DisposeAsync()
    {
        // Shuts down and cleans up container
        await _dbContainer.DisposeAsync();
    }
}

6. CI Best Practices for .NET

  1. Treat Warnings as Errors: Pass /p:TreatWarningsAsErrors=true during dotnet build to ensure compiler warnings block merges.
  2. Deterministic Tests: Avoid Thread.Sleep(), hardcoded dates (DateTime.Now), or non-mocked external network endpoints that cause flaky test failures.
  3. Cache NuGet Dependencies: Cache the ~/.nuget/packages folder in your CI configuration to reduce build times by up to 50%.
  4. Use Matrix Builds: Test across multiple operating systems (ubuntu-latest, windows-latest, macos-latest) if distributing cross-platform libraries or CLI tools.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents