Lesson 92 of 93 · 6 min read · Updated

Running Testcontainers Tests in GitHub Actions: A Staged CI Pipeline for .NET

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

  1. A GitHub repository with Actions enabled
  2. ubuntu-latest runners (Docker is preinstalled)
  3. .NET 11 SDK through actions/setup-dotnet@v4
  4. The Aspire CLI 17.0.0, because the solution contains an Aspire AppHost
  5. 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

on:
push:
paths: [ 'Modules/**', '.github/workflows/modules-ci.yml' ]
pull_request:
paths: [ 'Modules/**', '.github/workflows/modules-ci.yml' ]
workflow_dispatch:

env:
DOTNET_NOLOGO: true
DOTNET_CLI_TELEMETRY_OPTOUT: true

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:

JobWhat it runsNeeds
hizli-testlerEvery test that needs neither Docker nor a browserNothing
e2e-testleriPlaywright browser testshizli-testler
altyapi-testleriTestcontainers and Aspire testshizli-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

hizli-testler:
name: Fast tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install the .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: '11.0.x'

# Cache the DEPENDENCIES, NOT the container state.
- name: NuGet cache
uses: actions/cache@v4
with:
path: ~/.nuget/packages
key: nuget-${{ runner.os }}-${{ hashFiles('Modules/Directory.Packages.props') }}
restore-keys: nuget-${{ runner.os }}-

# Aspire 17 "Project v2": while M10.AppHost builds, DCP and the dashboard are
# taken from the Aspire CLI bundle; without the CLI the build stops with ASPIRE009.
- name: Install the Aspire CLI
run: dotnet tool install -g Aspire.Cli --version 17.0.0

- name: Restore
run: dotnet restore UnitTestLesson.sln

- name: Build
run: dotnet build UnitTestLesson.sln --no-restore -c Release

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:

- name: Test (modules that do not need Docker) + coverage
run: |
dotnet test UnitTestLesson.sln \
--no-build -c Release \
--filter "FullyQualifiedName!~M10&Kapsam!=E2E" \
--logger "trx;LogFileName=test-results.trx" \
--collect:"XPlat Code Coverage" \
--results-directory ./TestResults

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

altyapi-testleri:
name: Infrastructure tests (Testcontainers)
runs-on: ubuntu-latest
needs: hizli-testler
steps:
# (checkout, SDK and NuGet cache steps are the same as above)

# Pulling the images IN ADVANCE speeds up the first run and reduces
# timeout-related flakiness. The tags are PINNED and must match the
# tags in the fixture exactly.
- name: Pull container images in advance
run: |
docker pull postgres:17-alpine
docker pull redis:7-alpine
docker pull rabbitmq:4.1-alpine
docker pull confluentinc/cp-kafka:7.9.0

- name: Install the Aspire CLI
run: dotnet tool install -g Aspire.Cli --version 17.0.0

- name: Restore & Build
run: |
dotnet restore UnitTestLesson.sln
dotnet build UnitTestLesson.sln --no-restore -c Release

- name: Testcontainers tests
run: |
dotnet test Modules/Modul10-Testcontainers/M10.ContainerApi.Tests \
--no-build -c Release \
--logger "trx;LogFileName=testcontainers-results.trx" \
--results-directory ./TestResults

- name: Aspire system tests
run: |
dotnet test Modules/Modul10-Testcontainers/M10.AppHost.Tests \
--no-build -c Release \
--logger "trx;LogFileName=aspire-results.trx" \
--results-directory ./TestResults

- name: Upload results
if: always()
uses: actions/upload-artifact@v4
with:
name: altyapi-test-sonuclari
path: TestResults/**/*.trx

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

  1. GitHub-hosted Ubuntu runners have Docker, so Testcontainers tests run without a services: section.
  2. Split the pipeline into a fast job and slower jobs that depend on it with needs.
  3. Exclude Docker and browser tests from the fast job with a --filter on names and traits.
  4. Cache NuGet packages keyed on Directory.Packages.props; never cache container state.
  5. Pre-pull the exact pinned image tags, and upload test results with if: always().