Skip to main content
This guide covers testing ASP.NET Web API 2 controllers that run on the .NET Framework (4.6.2+).
If you’re using ASP.NET Core (running on .NET 6+), see the ASP.NET Core testing guide instead.
Testing Web API controllers traditionally requires running a full HTTP server, which is slow and can have port conflicts. Breakdance provides helpers that create an in-memory HTTP pipeline, letting you test your controllers directly without network overhead.

Prerequisites

1

Install the package

Add the Breakdance WebApi package to your test project:
2

Reference your Web API project

Your test project needs a reference to the project containing your controllers:

Quick Start

The simplest approach uses WebApiTestHelpers to get a fully configured HttpClient:
MyApiTests.cs
The ExecuteTestRequest extension method handles building the request URL and headers for you. It defaults to http://localhost/api/test as the base path.

How It Works

When you call GetTestableHttpClient(), Breakdance creates:
  1. An HttpConfiguration with attribute routing enabled
  2. An HttpServer that processes requests in-memory
  3. An HttpClient connected to that server
Your controllers are discovered automatically from referenced assemblies.

Configuration Options

Custom HttpConfiguration

If your API requires specific configuration (like custom routes or services), use the extension methods on HttpConfiguration:

Changing the Route Prefix

The default route prefix is api/test. Override it to match your API’s routes:

Custom Host

If localhost conflicts with something on your machine:

Sending Request Bodies

For POST, PUT, and PATCH requests, pass a payload object:
The payload is automatically serialized to JSON using Newtonsoft.Json.

Reading Response Content

Testing Error Responses

In-memory testing surfaces the same error responses your API would return in production:
Because IncludeErrorDetailPolicy.Always is set, you’ll get detailed error messages in the response body. This is helpful for debugging test failures but should not be enabled in production.

Comparison: In-Memory vs Real Server Testing

AspectIn-Memory TestingReal Server Testing
SpeedFast (no network)Slower (HTTP overhead)
Port conflictsNonePossible
Full HTTP stackNo (no network layer)Yes
SSL testingNoYes
Message handlersFull supportFull support
Best forUnit tests, CI/CDIntegration tests, E2E

Limitations

The Breakdance.WebApi package is specifically for ASP.NET Web API 2 on .NET Framework. It does not support:

ASP.NET Core Testing

Test ASP.NET Core APIs with TestServer

Response Snapshots

Capture and replay HTTP responses for deterministic tests