Integration tests check that different parts of your app work together as expected.
🧩 What is an Integration Test?
- Tests multiple components together (e.g., API + database)
- More realistic than unit tests
🏗️ Example: Testing a Web API
- Add Microsoft.AspNetCore.Mvc.Testing
1
dotnet add package Microsoft.AspNetCore.Mvc.Testing
- Sample Test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class ApiIntegrationTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly HttpClient _client;
public ApiIntegrationTests(WebApplicationFactory<Program> factory)
{
_client = factory.CreateClient();
}
[Fact]
public async Task Get_Endpoint_ReturnsSuccess()
{
var response = await _client.GetAsync("/api/products");
response.EnsureSuccessStatusCode();
}
}
⚠️ Tips
- Use an in-memory or test database.
- Clean up data between tests.
Next: Domain-Driven Design (DDD)!