A test suite that takes a few minutes to execute is usually manageable. Developers can make a code change, run the tests, and receive feedback quickly. However, as an application grows, the number of test cases, test projects, and integration dependencies also increases.
A test suite that once completed in three minutes may eventually take 20 or 30 minutes. Developers start waiting longer for pull request checks, releases become slower, and small changes require more time to validate.
The obvious solution is to run more tests in parallel. But simply increasing the number of concurrent jobs does not always improve performance. It can increase CPU and memory consumption, create database contention, and make failures harder to investigate.
The better approach is to understand where the time is being spent and optimize the CI pipeline accordingly.
GitHub Actions and the .NET testing ecosystem provide several ways to improve test execution, including dependency caching, parallel jobs, test distribution, build reuse, and better test isolation.
Understanding Why CI Test Suites Become Slow
Before changing a workflow, identify which part of the test process consumes the most time.
A typical .NET CI pipeline performs several operations:
Code Checkout
|
v
Install .NET SDK
|
v
Restore NuGet Packages
|
v
Build Solution
|
v
Execute Unit Tests
|
v
Execute Integration Tests
|
v
Generate Test Reports
|
v
Publish Results
Each operation introduces execution time.
For example, a workflow might spend 90 seconds restoring NuGet packages, 60 seconds building the solution, 120 seconds running unit tests, and another 180 seconds executing integration tests.
In this situation, optimizing only the unit tests will not significantly improve the complete pipeline.
Start by measuring the duration of individual steps and identifying the slowest test projects.
GitHub Actions provides job and step durations in workflow run logs. Test frameworks also provide execution summaries that help identify slow test cases.
For a .NET project, you can generate a test report using:
dotnet test MyApplication.Tests.csproj \
--configuration Release \
--logger "trx;LogFileName=test-results.trx"
The generated TRX file contains test execution information that can be used to investigate slow tests and failures.
Once the bottleneck is understood, choose the optimization that addresses it.
Avoid Restoring the Same Dependencies on Every Run
One common source of unnecessary CI time is repeatedly downloading NuGet packages.
GitHub-hosted runners start with a clean environment for each job. Without caching, the workflow may download the same dependencies every time it executes.
For a solution containing multiple test projects, package restoration can consume a significant portion of the total execution time.
GitHub Actions provides dependency caching to reuse downloaded packages between workflow runs.
The actions/setup-dotnet action supports NuGet caching through its cache option.
Consider the following workflow:
name: .NET Tests
on:
pull_request:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v6
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: '10.0.x'
cache: true
cache-dependency-path: '**/packages.lock.json'
- name: Restore dependencies
run: dotnet restore MyApplication.sln
- name: Run tests
run: dotnet test MyApplication.sln --configuration Release --no-restore
The cache-dependency-path option identifies the dependency lock files used to calculate the cache key.
If the dependency files remain unchanged, GitHub Actions can reuse the existing NuGet package cache.
When dependencies change, the cache key changes and the workflow creates a new cache entry.
For more predictable dependency restoration, consider enabling NuGet package lock files in your projects.
One important distinction is that dependency caching does not cache test results. Tests still execute, which is necessary to validate the current code.
Also, avoid storing credentials or other sensitive information in dependency caches. Treat restored cache contents as untrusted input, especially when workflows execute code from external pull requests.
Split Independent Test Projects Into Parallel Jobs
A large solution often contains several test projects.
For example:
MyApplication.sln
|
+---- UnitTests
|
+---- ApiTests
|
+---- IntegrationTests
|
+---- PaymentTests
|
+---- ReportingTests
Running all projects sequentially means the next project waits for the previous one to finish.
If these projects are independent, they can run in separate GitHub Actions jobs.
GitHub Actions supports matrix strategies for executing multiple variations of the same job.
The following workflow distributes test projects across parallel jobs:
name: Parallel .NET Tests
on:
pull_request:
push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
project:
- tests/UnitTests/UnitTests.csproj
- tests/ApiTests/ApiTests.csproj
- tests/IntegrationTests/IntegrationTests.csproj
- tests/PaymentTests/PaymentTests.csproj
steps:
- name: Checkout source
uses: actions/checkout@v6
- name: Setup .NET
uses: actions/setup-dotnet@v5
with:
dotnet-version: '10.0.x'
- name: Restore project
run: dotnet restore "${{ matrix.project }}"
- name: Run tests
run: |
dotnet test "${{ matrix.project }}" \
--configuration Release \
--no-restore
GitHub Actions creates a separate job for each project in the matrix.
If runner capacity is available, these jobs can execute concurrently.
For example, four projects that previously required approximately 20 minutes in a sequential workflow may finish much sooner when distributed across four runners.
Actual execution time depends on runner availability, project dependencies, and the duration of each test project.
The fail-fast setting is also important.
When fail-fast is true, GitHub Actions can cancel other in-progress matrix jobs after one job fails. Setting it to false allows the remaining projects to finish, which can provide a more complete picture of failures during a pull request.
However, parallel jobs are not automatically the best choice for every solution. Each job starts its own runner and may repeat SDK setup, dependency restoration, and compilation.
For smaller test projects, the additional startup and restoration overhead can outweigh the benefits.
Control Parallelism Instead of Increasing It Without Limits
Parallel execution can reduce test duration, but excessive concurrency can make a pipeline slower.
Consider a test suite that runs against a shared SQL database.
If ten test jobs execute simultaneously, they may compete for database connections, locks, CPU, and disk resources.
The result can be longer execution times or intermittent failures.
GitHub Actions allows you to limit the number of concurrently running matrix jobs using max-parallel.
For example:
strategy:
fail-fast: false
max-parallel: 3
matrix:
project:
- tests/UnitTests/UnitTests.csproj
- tests/ApiTests/ApiTests.csproj
- tests/IntegrationTests/IntegrationTests.csproj
- tests/PaymentTests/PaymentTests.csproj
- tests/ReportingTests/ReportingTests.csproj
Although five jobs are defined, GitHub Actions runs no more than three simultaneously.
This is useful when test execution depends on limited resources.
The ideal concurrency level depends on the workload. CPU-intensive unit tests may benefit from more parallelism, while integration tests that share databases or external services may need tighter limits.
Measure execution time at different concurrency levels rather than assuming that more jobs always mean faster CI.
Separate Unit Tests From Integration Tests
Unit tests and integration tests have different execution characteristics.
Unit tests generally execute quickly because they test isolated application logic. Integration tests may start containers, connect to databases, call APIs, or initialize external dependencies.
Combining both categories into one job can make feedback slower than necessary.
A better approach is to create separate jobs with appropriate execution strategies.
For example:
jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-dotnet@v5
with:
dotnet-version: '10.0.x'
- name: Run unit tests
run: dotnet test tests/UnitTests/UnitTests.csproj -c Release
integration-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-dotnet@v5
with:
dotnet-version: '10.0.x'
- name: Run integration tests
run: dotnet test tests/IntegrationTests/IntegrationTests.csproj -c Release
Both jobs can run independently.
This separation also makes it easier to apply different timeouts, environment variables, permissions, and resource requirements.
For example, integration tests may require a database connection string, while unit tests should not need access to production-like infrastructure.
You can also use job dependencies when a later stage should execute only after another stage succeeds.
Use Test Framework Parallelization Carefully
Parallelism can also happen inside a test project.
Test frameworks such as xUnit, NUnit, and MSTest provide their own mechanisms for executing tests concurrently.
This is different from running multiple GitHub Actions jobs.
There are several levels of parallel execution:
GitHub Actions runs independent jobs on separate runners.
The .NET test platform can run multiple test projects or test modules concurrently.
The test framework can run test classes or test methods concurrently.
For VSTest-based projects, the MaxCpuCount setting in a .runsettings file controls process-level parallelism across test containers.
For example:
<RunSettings>
<RunConfiguration>
<MaxCpuCount>4</MaxCpuCount>
</RunConfiguration>
</RunSettings>
The setting controls how many test containers can run simultaneously. It does not automatically make every test method inside an assembly execute in parallel. That behavior is controlled by the test framework.
For Microsoft.Testing.Platform-based projects, the .NET CLI provides the max-parallel-test-modules option.
For example:
dotnet test MyApplication.sln \
--max-parallel-test-modules 4
This controls the maximum number of test modules that can run concurrently.
Choose the appropriate option for the test platform used by the project. VSTest and Microsoft.Testing.Platform have different command-line options and configuration models.
Also, check whether tests share mutable state. Parallel execution can expose problems in tests that depend on execution order, shared files, static variables, or common database records.
A test that passes individually but fails under parallel execution usually needs better isolation, not simply more retries.
Avoid Building the Same Project More Than Necessary
The dotnet test command normally restores, builds, and executes tests.
This is convenient for local development, but CI workflows sometimes perform those operations more than once.
For example, a pipeline might build the solution in one step and then execute dotnet test in another step. If the second command builds the same projects again, it introduces unnecessary work.
When the workflow already restored and built the test projects successfully, use the appropriate options to avoid repeating those operations.
dotnet restore MyApplication.sln
dotnet build MyApplication.sln \
--configuration Release \
--no-restore
dotnet test MyApplication.sln \
--configuration Release \
--no-build \
--no-restore
This approach is useful when the build and test stages are executed in the same job and use the same build configuration.
Do not use --no-build when tests require binaries that have not been built for the current configuration or target framework.
Also, remember that separate GitHub-hosted jobs use separate runner environments. A build performed in one job is not automatically available in another job. If test jobs need binaries produced by a build job, pass them through workflow artifacts or use another deliberate build distribution strategy.
Collect Test Results Even When a Job Fails
Optimizing execution time is important, but developers also need useful feedback when tests fail.
A CI workflow should preserve test results rather than losing them when a test command returns a nonzero exit code.
For VSTest-based projects, TRX reports provide structured test results.
GitHub Actions can upload these reports as workflow artifacts:
- name: Run tests
run: |
dotnet test tests/UnitTests/UnitTests.csproj \
--configuration Release \
--logger "trx;LogFileName=unit-tests.trx"
continue-on-error: true
- name: Upload test results
if: always()
uses: actions/upload-artifact@v6
with:
name: unit-test-results
path: '**/TestResults/*.trx'
The continue-on-error setting allows the workflow to proceed to the upload step even when tests fail.
However, it also means the test step no longer fails the job by itself. A production workflow should explicitly preserve the test outcome and fail the job after reports are uploaded if any test failed.
One approach is to capture the test command's exit code, upload the results, and then return the original exit code.
This ensures that diagnostic information remains available without accidentally turning a failing test run into a successful CI job.
For projects using Microsoft.Testing.Platform, report generation uses its reporting extensions and options rather than assuming that every VSTest logger option is supported.
Identify and Fix Slow Tests Instead of Hiding Them
Parallelization can reduce the time required to execute a test suite, but it does not fix inefficient tests.
A single integration test that takes 90 seconds can still delay a pipeline, especially when other jobs depend on its result.
Review slow tests and look for common problems:
Repeated database initialization.
Unnecessary network requests.
Large test datasets.
Excessive application startup.
Uncontrolled external dependencies.
Repeated file-system operations.
Tests that wait for fixed delays instead of observable conditions.
For example, avoid using fixed delays to wait for asynchronous operations:
await Task.Delay(5000);
A fixed five-second delay makes the test wait even when the operation completes immediately.
A better approach is to wait for a specific condition with a reasonable timeout.
var timeout = TimeSpan.FromSeconds(10);
var stopwatch = Stopwatch.StartNew();
while (!await IsOperationCompleteAsync())
{
if (stopwatch.Elapsed > timeout)
{
throw new TimeoutException(
"Operation did not complete within the expected time.");
}
await Task.Delay(100);
}
This example checks the condition repeatedly and stops waiting as soon as the operation completes.
For production test infrastructure, prefer framework-supported asynchronous assertions or polling utilities where available.
Establish a Practical CI Optimization Strategy
Not every optimization needs to be introduced at once.
A useful implementation sequence is:
Measure the duration of restore, build, and test steps.
Enable dependency caching for NuGet packages.
Separate unit tests from integration tests.
Run independent test projects in parallel.
Limit concurrency according to available resources.
Enable test-level parallelization where tests are isolated.
Remove unnecessary restore and build operations.
Preserve test reports for failed runs.
Review slow and flaky tests regularly.
The team should also track the complete pull request feedback time, not just the duration of the test command.
A workflow with faster tests can still be slow if it spends too much time waiting for runners, downloading dependencies, or executing unnecessary jobs.
Summary
As an application grows, its CI test suite can become a major source of development delays. The solution is not simply to add more runners or execute every test simultaneously.
GitHub Actions provides matrix jobs and concurrency controls for distributing independent test projects. Dependency caching reduces repeated package downloads, while .NET testing tools provide options for controlling parallel execution within and across test modules.
Separating unit and integration tests, avoiding unnecessary builds, and preserving test reports also help improve the overall CI experience.
Most importantly, measure the pipeline before optimizing it. A well-designed test workflow should provide fast feedback without introducing resource contention, hiding failures, or sacrificing test reliability.
Join the conversation! Your thoughts help the community grow.