The Intent.AspNetCore.IntegrationTesting module generates complete integration test infrastructure for testing your ASP.NET Core Web API endpoints end-to-end. It creates test fixtures, handles authentication, manages test databases, and provides helpers for API testing.
The module generates a custom WebApplicationFactory that configures a test environment:
IntegrationTestWebApplicationFactory.cs
using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Mvc.Testing;using Microsoft.EntityFrameworkCore;using Microsoft.Extensions.DependencyInjection;public class IntegrationTestWebApplicationFactory : WebApplicationFactory<Program>{ protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureServices(services => { // Remove the app's DbContext registration var descriptor = services.SingleOrDefault( d => d.ServiceType == typeof(DbContextOptions<ApplicationDbContext>)); if (descriptor != null) { services.Remove(descriptor); } // Add DbContext using in-memory database for testing services.AddDbContext<ApplicationDbContext>(options => { options.UseInMemoryDatabase("IntegrationTestDb"); }); // Build service provider var sp = services.BuildServiceProvider(); // Create database and seed test data using var scope = sp.CreateScope(); var scopedServices = scope.ServiceProvider; var db = scopedServices.GetRequiredService<ApplicationDbContext>(); db.Database.EnsureCreated(); SeedTestData(db); }); } private void SeedTestData(ApplicationDbContext context) { // Add test data here }}
Pros: Fast, no setup required Cons: Doesn’t test real database features (constraints, triggers)
More realistic database with SQLite:
var connection = new SqliteConnection("DataSource=:memory:");connection.Open();services.AddDbContext<ApplicationDbContext>(options =>{ options.UseSqlite(connection);});
Pros: Tests real SQL, constraints work Cons: Slightly slower, some SQL Server features missing
Full database in Docker container:
var container = new SqlServerBuilder() .WithImage("mcr.microsoft.com/mssql/server:2022-latest") .Build();await container.StartAsync();services.AddDbContext<ApplicationDbContext>(options =>{ options.UseSqlServer(container.GetConnectionString());});
Pros: Full SQL Server features Cons: Slower, requires Docker
Issue: Tests fail due to data from previous testsSolution:
public class BaseIntegrationTest : IAsyncLifetime{ public async Task InitializeAsync() { // Setup } public async Task DisposeAsync() { // Clean up database using var scope = Factory.Services.CreateScope(); var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); await context.Database.EnsureDeletedAsync(); }}
Authentication Not Working
Issue: Authenticated endpoints return 401Solution: Configure test authentication scheme: