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
| Aspect | TestServer (In-Memory) | Real Server |
|---|---|---|
| Speed | Fast (no network) | Slower (HTTP overhead) |
| Port conflicts | None | Possible |
| Full HTTP stack | Most features | Yes |
| SSL testing | Limited | Yes |
| Middleware | Full support | Full support |
| Authentication | Configurable | Full support |
| Best for | Unit tests, CI/CD | E2E tests |