<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/integration-tests-in-asp-net-core-rydki5zvo" -->

---
title: Integration Tests in ASP.NET Core | daily.dev
description: Unit tests miss gaps that appear when middleware, DI, validation and EF Core all run together, so this walks through building integration tests for ASP.NET...
canonical: https://daily.dev/posts/integration-tests-in-asp-net-core-rydki5zvo
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Integration Tests in ASP.NET Core | daily.dev
og:description: Unit tests miss gaps that appear when middleware, DI, validation and EF Core all run together, so this walks through building integration tests for ASP.NET...
og:url: https://daily.dev/posts/integration-tests-in-asp-net-core-rydki5zvo
og:image: https://api.daily.dev/og/posts/RyDki5ZVo.png
og:image:alt: Integration Tests in ASP.NET Core
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Integration Tests in ASP.NET Core

**[NikolaTech](https://daily.dev/sources/nikolatech)** · 5 min read · 0 upvotes · 0 comments

## Summary

Unit tests miss gaps that appear when middleware, DI, validation and EF Core all run together, so this walks through building integration tests for ASP.NET Core APIs using WebApplicationFactory, Testcontainers and Respawn. It covers setting up a custom WebApplicationFactory that boots a real PostgreSQL container, sharing that factory across test classes with xUnit collection fixtures, resetting database state between tests with Respawn, and writing tests that hit real endpoints covering validation, success and failure paths, and chained requests like create-then-fetch flows.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://nikolatech.net/blogs/integration-tests-asp-dotnet-core>

## Questions this post answers

### How do I set up integration tests for an ASP.NET Core API with a real PostgreSQL database?

Use a custom WebApplicationFactory combined with Testcontainers and Respawn. WebApplicationFactory boots the app in-memory and provides an HttpClient, Testcontainers spins up a real PostgreSQL container per test run, and Respawn resets database tables between tests so data does not leak. Override ConfigureWebHost to point the connection string at the container before the host starts.

_daily.dev surfaces practical setups like this for teams wiring real databases into their test suites._

### How do I avoid starting a new Testcontainers PostgreSQL container for every xUnit test class?

Use an xUnit collection fixture with ICollectionFixture so one WebApplicationFactory instance, and its container, is shared across all test classes in that collection. The container starts once and stays alive for the collection lifetime, while each test class still calls Respawn to reset the database after it finishes so leftover rows do not leak into the next class.

_developers tuning slow test suites can keep tabs on patterns like shared fixtures via daily.dev._

### What does Respawn do in .NET integration tests and why use it instead of recreating the database?

Respawn resets specific database tables between tests without dropping and recreating the whole database, keeping teardown fast while preventing test data from leaking across runs. It is configured with the schemas to include and a database adapter (for example Postgres), and its ResetAsync method is typically called in each test class's DisposeAsync.

_daily.dev helps developers comparing test isolation strategies like Respawn stay informed._

## Similar posts on daily.dev

- [Testing ASP.NET Core Web API: WebApplicationFactory and Integration Tests](https://daily.dev/posts/testing-asp-net-core-web-api-webapplicationfactory-and-integration-tests-8chotq6j5) · We Are .NET · 1 upvotes · 0 comments
- [More Realistic Tests with In-Memory Databases ASP.NET Core](https://daily.dev/posts/more-realistic-tests-with-in-memory-databases-asp-net-core-wctbbhdk6) · Telerik · 0 upvotes · 0 comments
- [The Test Pyramid Is a Lie \(and What I Do Instead\)](https://daily.dev/posts/the-test-pyramid-is-a-lie-and-what-i-do-instead--ak2scchgz) · Milan Jovanović · 40 upvotes · 0 comments
- [Security Tests That Prove Themselves — Daily DevOps & .NET](https://daily.dev/posts/security-tests-that-prove-themselves-daily-devops-net-4mutnqllj) · We Are .NET · 1 upvotes · 0 comments

---

Tags: [#testing](https://daily.dev/tags/testing), [#postgresql](https://daily.dev/tags/postgresql), [#aspnet](https://daily.dev/tags/aspnet)

[View this post on daily.dev](https://daily.dev/posts/integration-tests-in-asp-net-core-rydki5zvo)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Integration Tests in ASP.NET Core","url":"https://daily.dev/posts/integration-tests-in-asp-net-core-rydki5zvo","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/integration-tests-in-asp-net-core-rydki5zvo"},"datePublished":"2026-10-04T13:07:32.365Z","dateModified":"2026-10-04T13:17:22.677Z","description":"Unit tests miss gaps that appear when middleware, DI, validation and EF Core all run together, so this walks through building integration tests for ASP.NET...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/947831613682be25352bc10d2098fee9?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/947831613682be25352bc10d2098fee9?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"NikolaTech","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"NikolaTech","logo":"https://media.daily.dev/image/upload/s--1h2fO6FL--/f_auto,q_auto/v1791119050/logos/nikolatech?_a=BAMAMicg0","url":"https://daily.dev/sources/nikolatech"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/integration-tests-in-asp-net-core-rydki5zvo","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"testing,postgresql,aspnet","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"NikolaTech","item":"https://daily.dev/sources/nikolatech"},{"@type":"ListItem","position":3,"name":"Integration Tests in ASP.NET Core"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/integration-tests-in-asp-net-core-rydki5zvo#faq","mainEntity":[{"@type":"Question","name":"How do I set up integration tests for an ASP.NET Core API with a real PostgreSQL database?","acceptedAnswer":{"@type":"Answer","text":"Use a custom WebApplicationFactory combined with Testcontainers and Respawn. WebApplicationFactory boots the app in-memory and provides an HttpClient, Testcontainers spins up a real PostgreSQL container per test run, and Respawn resets database tables between tests so data does not leak. Override ConfigureWebHost to point the connection string at the container before the host starts. daily.dev surfaces practical setups like this for teams wiring real databases into their test suites."}},{"@type":"Question","name":"How do I avoid starting a new Testcontainers PostgreSQL container for every xUnit test class?","acceptedAnswer":{"@type":"Answer","text":"Use an xUnit collection fixture with ICollectionFixture so one WebApplicationFactory instance, and its container, is shared across all test classes in that collection. The container starts once and stays alive for the collection lifetime, while each test class still calls Respawn to reset the database after it finishes so leftover rows do not leak into the next class. developers tuning slow test suites can keep tabs on patterns like shared fixtures via daily.dev."}},{"@type":"Question","name":"What does Respawn do in .NET integration tests and why use it instead of recreating the database?","acceptedAnswer":{"@type":"Answer","text":"Respawn resets specific database tables between tests without dropping and recreating the whole database, keeping teardown fast while preventing test data from leaking across runs. It is configured with the schemas to include and a database adapter (for example Postgres), and its ResetAsync method is typically called in each test class's DisposeAsync. daily.dev helps developers comparing test isolation strategies like Respawn stay informed."}}]}
```

