Skip to content
Fresh

Run tests

Back

Guides

Testing an ASP.NET Core web app with Testcontainers

Learn how to test an ASP.NET Core web app using Testcontainers for .NET with a real Microsoft SQL Server instance instead of SQLite.

C# Testing with Docker

25 minutes

1

Create the project

2

Write tests

3

Run tests

« Back to all guides

Run tests and next steps

Copy as Markdown

Open MarkdownAsk Docs AIClaudeOpen in Claude

Table of contents


Run the tests

Run the tests from the solution root:

console
$ dotnet test ./RazorPagesProject.sln

The first run may take longer because Docker needs to pull the Microsoft SQL Server image. On subsequent runs, the image is cached locally.

You should see xUnit discover and run the tests, including the MsSqlTests.IndexPageTests class. Testcontainers starts a SQL Server container, the tests execute against it, and the container is stopped and removed automatically after the tests finish.

Summary

By replacing SQLite with a Testcontainers-managed Microsoft SQL Server instance, the integration tests run against the same type of database used in production. This approach catches database-specific issues early, such as differences in SQL dialect, transaction behavior, or data type handling between SQLite and SQL Server.

The MsSqlTests class uses IAsyncLifetime to manage the container lifecycle, and a nested CustomWebApplicationFactory wires the container's connection string into the application's service configuration. You can apply this same pattern to any database or service that Testcontainers supports.

To learn more about Testcontainers, visit the Testcontainers overview.

Further reading