If you’re using ASP.NET Web API 2 on .NET Framework, see the ASP.NET Classic testing guide instead.
TestServer, giving you in-memory HTTP testing with full dependency injection support.
Prerequisites
1
Install the package
Add the Breakdance AspNetCore package to your test project:
2
Reference your API project
Your test project needs a reference to the project containing your controllers:
Quick Start
Inherit fromAspNetCoreBreakdanceTestBase to get automatic TestServer management:
MyApiTests.cs
Configuration Methods
AspNetCoreBreakdanceTestBase provides several helper methods to configure your test environment:
AddApis()
Configures the test server with controller support (authorization, CORS, data annotations, formatter mappings):services.AddControllers() in your Startup.cs.
AddMinimalMvc()
Registers only the minimum MVC services needed to route requests and invoke controllers:AddViews()
Configures support for controllers with Razor views:AddRazorPages()
Configures support for Razor Pages:Custom Configuration
For more control, configure theTestHostBuilder directly:
Getting an HttpClient
UseGetHttpClient() to get a client connected to the in-memory test server:
Custom Route Prefix
Override the default route prefix:With Authentication
Add authentication headers:Accessing Services
Resolve services from the test server’s dependency injection container:Testing POST/PUT/PATCH Requests
Testing Error Responses
Using Static Helpers
For simpler scenarios, useAspNetCoreTestHelpers without inheriting from the test base:
Integration with FluentAssertions
Combine with FluentAssertions for more expressive tests:Comparison: TestServer vs Real Server
Lifecycle
Understanding the test lifecycle helps avoid common issues:Related Resources
ASP.NET Classic Testing
Test ASP.NET Web API 2 on .NET Framework
Response Snapshots
Capture and replay HTTP responses for deterministic tests