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:
- Temporary Directory: Each instance creates a unique temp directory with automatic cleanup via
IAsyncDisposable - global.json: Configures SDK version with
rollForward: latestMinorfor version stability - NuGet.config: Optional package source configuration with source mapping support
- Project Files: Generates
.csprojfrom fluent configuration - Source Files: Writes C# files to the project directory
SDK Version Management
ProjectBuilder automatically downloads and caches .NET SDK versions:NetSdkVersion:
Net100- .NET 10.0 (default)Net90- .NET 9.0Net80- .NET 8.0
Build Operations
| Method | Purpose |
|---|---|
BuildAsync() | Compiles the project |
RunAsync() | Builds and executes (for console apps) |
TestAsync() | Builds and runs tests |
PackAsync() | Creates NuGet package |
RestoreAsync() | Restores packages only |
ExecuteDotnetCommandAsync() | Run any dotnet command |
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.| Class | Purpose |
|---|---|
Tfm | Target framework monikers (Net100, NetStandard20) |
Prop | Property names (TargetFramework, OutputType) |
Val | Property values (Library, Exe, Enable) |
Item | Item names (PackageReference, Compile) |
Attr | Attribute names (Include, Version) |
DotNetSdkHelpers
Downloads and caches .NET SDK versions for testing.- 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: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:Troubleshooting
Viewing Build Output
PassITestOutputHelper to see all generated files and build commands:
Inspecting Binary Logs
BuildResult includes the binary log for detailed analysis:
Environment Isolation
ProjectBuilder automatically removes interfering environment variables:
CIGITHUB_*MSBuild*RUNNER_*DOTNET_ENVIRONMENT
