Skip to main content
Integration testing infrastructure for testing with real dotnet build commands.

Overview

Unlike Roslyn’s in-memory testing, MSBuild testing creates actual project files, runs real builds, and validates the results. This is essential for:
  • Testing MSBuild SDK behavior
  • Validating analyzer packages in real builds
  • Testing build-time source generators
  • Verifying SARIF output and binlog contents
  • Testing Central Package Management (CPM) scenarios
  • Validating NuGet package source mapping

Core Components

ProjectBuilder

Fluent API for creating and building temporary .NET projects in isolated environments.

How ProjectBuilder Works

ProjectBuilder creates a complete isolated build environment:
  1. Temporary Directory: Each instance creates a unique temp directory with automatic cleanup via IAsyncDisposable
  2. global.json: Configures SDK version with rollForward: latestMinor for version stability
  3. NuGet.config: Optional package source configuration with source mapping support
  4. Project Files: Generates .csproj from fluent configuration
  5. Source Files: Writes C# files to the project directory

SDK Version Management

ProjectBuilder automatically downloads and caches .NET SDK versions:
Available SDK versions via NetSdkVersion:
  • Net100 - .NET 10.0 (default)
  • Net90 - .NET 9.0
  • Net80 - .NET 8.0

Build Operations

Each method returns a BuildResult with SARIF diagnostics and binary log.

BuildResult

Contains build output with fluent assertions.

MSBuild Constants

Type-safe constants for MSBuild properties, values, and items.

DotNetSdkHelpers

Downloads and caches .NET SDK versions for testing.
The SDK cache location:
  • macOS/Linux: ~/.dotnet-sdk-cache/
  • Windows: %USERPROFILE%\.dotnet-sdk-cache\

RepositoryRoot

Locates repository root for file access in tests.

ProjectBuilder Deep Dive

NuGet Configuration

Simple Local Source

With Package Source Mapping

For security and reproducibility, use source mapping:
This generates:

Custom NuGet Config

For complete control:

Directory.Build.props and Central Package Management

Shared Build Properties

Central Package Management (CPM)

SDK Selection

Custom MSBuild SDK

Microsoft Testing Platform (MTP)

Enable MTP mode for modern test execution:
WithMtpMode() updates global.json to:

GitHub Actions Simulation

Test CI-specific behavior:

Running Applications

For console application testing:

Executing Arbitrary Commands

File Access

Add arbitrary files to the project:

Complete Examples

Testing an Analyzer Package

Testing a Custom SDK

Testing Source Generators

Testing with Banned APIs

Extending ProjectBuilder

Create a derived builder for project-specific defaults:

Package Testing Infrastructure

For testing NuGet packages or MSBuild SDKs, use the specialized infrastructure that handles package pre-warming and SDK import patterns.

PackageProjectBuilder

Extends ProjectBuilder with SDK/package import styles and fixture integration.

Package Import Styles

Additional Features

NuGetPackageFixture

xUnit assembly fixture for pre-warming NuGet packages before tests run.
The fixture:
  • Creates an isolated package directory
  • Pre-warms specified packages to avoid restore delays during tests
  • In CI mode, copies packages from NUGET_DIRECTORY environment variable
  • Cleans up automatically via IAsyncDisposable

PackageTestBase

Base class with convenience methods for package testing.

Convenience Methods

Complete Package Testing Example

Inspecting Binary Logs

BuildResult includes the binary log for detailed analysis:

Environment Isolation

ProjectBuilder automatically removes interfering environment variables:
  • CI
  • GITHUB_*
  • MSBuild*
  • RUNNER_*
  • DOTNET_ENVIRONMENT
This ensures consistent builds regardless of the host environment.