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:Package Testing Infrastructure
For testing NuGet packages or MSBuild SDKs, use the specialized infrastructure that handles package pre-warming and SDK import patterns.PackageProjectBuilder
ExtendsProjectBuilder with SDK/package import styles and fixture integration.
Package Import Styles
| Style | Description |
|---|---|
Default | Uses configured root SDK |
ProjectElement | <Project Sdk="Package/Version"> |
SdkElement | <Sdk Name="Package" Version="..."/> |
SdkElementDirectoryBuildProps | SDK element in Directory.Build.props |
Additional Features
NuGetPackageFixture
xUnit assembly fixture for pre-warming NuGet packages before tests run.- Creates an isolated package directory
- Pre-warms specified packages to avoid restore delays during tests
- In CI mode, copies packages from
NUGET_DIRECTORYenvironment variable - Cleans up automatically via
IAsyncDisposable
| Property | Description |
|---|---|
PackageDirectory | Path to the isolated package cache |
Version | Package version from env or default |
PackageTestBase
Base class with convenience methods for package testing.Convenience Methods
| Method | Description |
|---|---|
CreateProject(style?, name?) | Create PackageProjectBuilder with config |
QuickBuild(code, tfm, props) | Build library with minimal config |
BuildLibrary(code, tfm) | Build as library |
BuildExe(code, tfm) | Build as executable |
Complete Package Testing Example
Inspecting Binary Logs
BuildResult includes the binary log for detailed analysis:
Environment Isolation
ProjectBuilder automatically removes interfering environment variables:
CIGITHUB_*MSBuild*RUNNER_*DOTNET_ENVIRONMENT
