CodingNeed.

3 · Reliability and delivery · 45 MIN

Project · An API with integration tests

A test host should exercise routing and serialization together.

WebApplicationFactory starts the application inside a test host so HttpClient can exercise the real HTTP contract. The partial Program declaration makes the top-level entry point accessible to the test project. Add a project reference to the API and use a Microsoft.AspNetCore.Mvc.Testing package matching its target framework. These read-only tests need no database. When you add persistence, replace production configuration with an isolated test database and verify both successful writes and rejected ownership attempts.

Keep production database credentials out of test configuration.

Read the example

// API Program.cs
using Microsoft.AspNetCore.Builder;
var app=WebApplication.CreateBuilder(args).Build();
app.MapGet("/health",()=>new {ok=true});
app.Run();
public partial class Program {}
// HealthTests.cs in an xUnit project referencing the API
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net;
using Xunit;
public class HealthTests {
  [Fact] public async Task Health_is_available() {
    await using var factory=new WebApplicationFactory<Program>();
    using var client=factory.CreateClient();
    var response=await client.GetAsync("/health");
    Assert.Equal(HttpStatusCode.OK,response.StatusCode);
    Assert.Equal("application/json",response.Content.Headers.ContentType?.MediaType);
  }
}
Check the expected output
dotnet test reports a passing health integration test once the API reference and matching test packages are installed.

Your challenge

Add the validated preview endpoint, write successful and invalid-input tests, then test an owner-protected endpoint with two test identities.

Solution cost: Depends on the HTTP operation and test-host startup. time · Test-host process plus bounded fixture data. space

Common trap

Mocking every layer can hide routing, serialization and database-constraint failures.

Study the project implementation
// API Program.cs
using Microsoft.AspNetCore.Builder;
var app=WebApplication.CreateBuilder(args).Build();
app.MapGet("/health",()=>new {ok=true});
app.Run();
public partial class Program {}
// HealthTests.cs in an xUnit project referencing the API
using Microsoft.AspNetCore.Mvc.Testing;
using System.Net;
using Xunit;
public class HealthTests {
  [Fact] public async Task Health_is_available() {
    await using var factory=new WebApplicationFactory<Program>();
    using var client=factory.CreateClient();
    var response=await client.GetAsync("/health");
    Assert.Equal(HttpStatusCode.OK,response.StatusCode);
    Assert.Equal("application/json",response.Content.Headers.ContentType?.MediaType);
  }
}

Further reading: Official documentation

Essential cookies keep your account signed in. Optional analytics is not configured on this site. Your choice does not affect access to lessons.

Read the Privacy Policy. You can change this choice in the footer.