Lesson 92 of 93 · 6 min read · Updated
Running Testcontainers Tests in GitHub Actions: A Staged CI Pipeline for .NET
On this page
Container tests only protect you if they run on every change, not just on the laptop of the person who wrote them. GitHub-hosted Linux runners come with Docker already installed, so Testcontainers works there without extra setup. This lesson walks through a GitHub Actions workflow that runs fast unit tests first, then the slower browser and container tests, and explains the choices that keep the pipeline quick and stable.
Technical Requirements
- A GitHub repository with Actions enabled
ubuntu-latestrunners (Docker is preinstalled)- .NET 11 SDK through
actions/setup-dotnet@v4 - The Aspire CLI 17.0.0, because the solution contains an Aspire AppHost
- The test projects from the earlier lessons: Testcontainers tests and Aspire system tests
Triggers and the Three-Stage Pipeline
The workflow runs on pushes and pull requests that touch the test modules, and it can be started by hand:
.github/workflows/modules-ci.yml
The paths filter keeps unrelated changes, such as documentation edits elsewhere in the repository, from starting the pipeline. Including the workflow file itself means a change to the pipeline is always tested by the pipeline.
The jobs mirror the test pyramid:
| Job | What it runs | Needs |
|---|---|---|
hizli-testler | Every test that needs neither Docker nor a browser | Nothing |
e2e-testleri | Playwright browser tests | hizli-testler |
altyapi-testleri | Testcontainers and Aspire tests | hizli-testler |
The fast job gives feedback in seconds. The two slower jobs declare needs: hizli-testler, so they only start when the fast tests pass, and then they run in parallel. A broken unit test does not cost you the minutes it takes to pull a Kafka image. The browser job builds only the Playwright project and installs Chromium with its system libraries, so browser tests and container tests never compete for the same runner.
The Fast Job: Filter Out What Needs Docker
.github/workflows/modules-ci.yml
The NuGet cache key is a hash of Directory.Packages.props. Because all package versions live in that one file (central package management), the cache is reused until a version changes. The comment states the rule: cache dependencies, never container state. Every test run must start from fresh containers, or the pipeline stops proving anything.
Pitfall: the solution contains the Aspire AppHost, so even the fast job, which runs no Aspire test, must install the Aspire CLI before building. Without it the build fails with ASPIRE009. The CLI version is pinned to match the Aspire packages.
The test step then excludes everything that needs Docker or a browser:
FullyQualifiedName!~M10 skips every test whose namespace contains M10, which covers both container projects. Kapsam!=E2E skips tests that carry the trait [Trait("Kapsam", "E2E")], which marks the Playwright tests. The & combines both conditions. The --collect option gathers code coverage data; turning it into a report is a topic of its own.
The Infrastructure Job: Pre-Pull Pinned Images
.github/workflows/modules-ci.yml
There is no services: section. Many CI examples declare a PostgreSQL service container in the workflow. With Testcontainers that is not needed: the tests start their own containers through the runner's Docker daemon, exactly as they do locally. The pipeline and the developer machine run the same setup code.
Pulling the images in a separate step has two benefits. The pull time shows up as its own step in the log, so a slow registry is easy to spot. And the tests no longer spend their timeouts on downloads; a Kafka image pull inside a fixture can take longer than the test's own time limit.
Common mistake: pinning postgres:17-alpine in the fixture but pulling postgres:latest in the workflow. The pre-pull then downloads an image nobody uses, and the fixture pulls its own image anyway. Keep the tags in the workflow identical to the tags in the fixtures.
The Testcontainers tests and the Aspire tests run as separate steps with separate TRX files. If the Aspire step fails, you see immediately which kind of test broke. The upload step uses if: always(), so the result files are kept even when a test step fails, which is exactly when you need them.
Summary
- GitHub-hosted Ubuntu runners have Docker, so Testcontainers tests run without a
services:section. - Split the pipeline into a fast job and slower jobs that depend on it with
needs. - Exclude Docker and browser tests from the fast job with a
--filteron names and traits. - Cache NuGet packages keyed on
Directory.Packages.props; never cache container state. - Pre-pull the exact pinned image tags, and upload test results with
if: always().