$npx -y skills add wshaddix/dotnet-skills --skill aspire-integration-testingWrite integration tests using .NET Aspire's testing facilities with xUnit. Covers test fixtures, distributed application setup, endpoint discovery, and patterns for testing ASP.NET Core apps with real dependencies. Use when writing integration tests for .NET Aspire applications,
| 1 | # Integration Testing with .NET Aspire + xUnit |
| 2 | |
| 3 | ## When to Use This Skill |
| 4 | |
| 5 | Use this skill when: |
| 6 | - Writing integration tests for .NET Aspire applications |
| 7 | - Testing ASP.NET Core apps with real database connections |
| 8 | - Verifying service-to-service communication in distributed applications |
| 9 | - Testing with actual infrastructure (SQL Server, Redis, message queues) in containers |
| 10 | - Combining Playwright UI tests with Aspire-orchestrated services |
| 11 | - Testing microservices with proper service discovery and networking |
| 12 | |
| 13 | ## Core Principles |
| 14 | |
| 15 | 1. **Real Dependencies** - Use actual infrastructure (databases, caches) via Aspire, not mocks |
| 16 | 2. **Dynamic Port Binding** - Let Aspire assign ports dynamically (`127.0.0.1:0`) to avoid conflicts |
| 17 | 3. **Fixture Lifecycle** - Use `IAsyncLifetime` for proper test fixture setup and teardown |
| 18 | 4. **Endpoint Discovery** - Never hard-code URLs; discover endpoints from Aspire at runtime |
| 19 | 5. **Parallel Isolation** - Use xUnit collections to control test parallelization |
| 20 | 6. **Health Checks** - Always wait for services to be healthy before running tests |
| 21 | |
| 22 | ## High-Level Testing Architecture |
| 23 | |
| 24 | ``` |
| 25 | ┌─────────────────┐ ┌──────────────────────┐ |
| 26 | │ xUnit test file │──uses────────────►│ AspireFixture │ |
| 27 | └─────────────────┘ │ (IAsyncLifetime) │ |
| 28 | └──────────────────────┘ |
| 29 | │ |
| 30 | │ starts |
| 31 | ▼ |
| 32 | ┌───────────────────────────┐ |
| 33 | │ DistributedApplication │ |
| 34 | │ (from AppHost) │ |
| 35 | └───────────────────────────┘ |
| 36 | │ exposes |
| 37 | ▼ |
| 38 | ┌──────────────────────────────┐ |
| 39 | │ Dynamic HTTP Endpoints │ |
| 40 | └──────────────────────────────┘ |
| 41 | │ consumed by |
| 42 | ▼ |
| 43 | ┌─────────────────────────┐ |
| 44 | │ HttpClient / Playwright│ |
| 45 | └─────────────────────────┘ |
| 46 | ``` |
| 47 | |
| 48 | ## Required NuGet Packages |
| 49 | |
| 50 | ```xml |
| 51 | <ItemGroup> |
| 52 | <PackageReference Include="Aspire.Hosting.Testing" Version="$(AspireVersion)" /> |
| 53 | <PackageReference Include="xunit" Version="*" /> |
| 54 | <PackageReference Include="xunit.runner.visualstudio" Version="*" /> |
| 55 | <PackageReference Include="Microsoft.NET.Test.Sdk" Version="*" /> |
| 56 | </ItemGroup> |
| 57 | ``` |
| 58 | |
| 59 | ## CRITICAL: File Watcher Fix for Integration Tests |
| 60 | |
| 61 | When running many integration tests that each start an IHost, the default .NET host builder enables file watchers for configuration reload. This exhausts file descriptor limits on Linux. |
| 62 | |
| 63 | **Add this to your test project before any tests run:** |
| 64 | |
| 65 | ```csharp |
| 66 | // TestEnvironmentInitializer.cs |
| 67 | using System.Runtime.CompilerServices; |
| 68 | |
| 69 | namespace YourApp.Tests; |
| 70 | |
| 71 | internal static class TestEnvironmentInitializer |
| 72 | { |
| 73 | [ModuleInitializer] |
| 74 | internal static void Initialize() |
| 75 | { |
| 76 | // Disable config file watching in test hosts |
| 77 | // Prevents file descriptor exhaustion (inotify watch limit) on Linux |
| 78 | Environment.SetEnvironmentVariable("DOTNET_HOSTBUILDER__RELOADCONFIGONCHANGE", "false"); |
| 79 | } |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | **Why this matters:** `[ModuleInitializer]` runs before any test code executes, setting the environment variable globally for all IHost instances created during tests. |
| 84 | |
| 85 | ## Reference Documentation |
| 86 | |
| 87 | For detailed patterns and examples, see the following reference files: |
| 88 | |
| 89 | - **[Test Fixtures](./reference/test-fixtures.md)** - xUnit test fixtures, test class organization, fixture lifecycle patterns, and database reset strategies |
| 90 | - **[Testing Patterns](./reference/testing-patterns.md)** - Testing individual services, service-to-service communication, real databases, message brokers, and Playwright UI tests |
| 91 | - **[Configuration Examples](./reference/configuration-examples.md)** - Resource configuration, environment variable configuration, and wait strategies |
| 92 | |
| 93 | ## Common Patterns Summary |
| 94 | |
| 95 | | Pattern | Use Case | |
| 96 | |---------|----------| |
| 97 | | Basic Fixture | Simple HTTP endpoint testing | |
| 98 | | Endpoint Discovery | Avoid hard-coded URLs | |
| 99 | | Database Testing | Verify data access layer | |
| 100 | | Playwright Integration | Full UI testing with rea |