Growing .NET organisations often use multiple CI systems to build and test the same ASP.NET Core application. One team may use Jenkins, another may use GitHub Actions, while an acquired team continues to run GitLab CI.
Over time, each system can develop its own version of the API tests. One pipeline starts a SQL Server container, another connects to a shared staging database, and a third skips integration tests because its runners do not support Docker.
This creates an inconsistent testing environment. A test might pass in one pipeline and fail in another, making it difficult to identify whether the problem is in the application, the test configuration, or the environment.
A better approach is to maintain one API test suite that can run consistently across all three CI systems.
In this article, we will explore four open-source approaches for testing ASP.NET Core APIs and compare their setup requirements, portability, and limitations.
What Makes an API Test Suite Portable?
Before comparing the available approaches, it is important to understand what portability means in a multi-CI environment.
A portable API test suite should meet five requirements.
One command everywhere: Every CI pipeline should execute the same test command. Test logic should remain in the repository rather than being duplicated in YAML files or Jenkinsfiles.
No shared live environment: Tests should not depend on a shared staging database or third-party sandbox that multiple pipelines can modify simultaneously.
Minimal runner requirements: Ideally, the test runner should need only the .NET SDK and a few additional tools. Every dependency adds configuration and maintenance work across CI systems.
Testing at the HTTP boundary: Tests should verify the behavior that API consumers depend on, including HTTP status codes, response headers, request handling, and JSON structures.
Maintainability as the API grows: Adding new endpoints should not require developers to manually maintain a large number of repetitive test cases.
These requirements provide a useful basis for comparing different API testing strategies.
Option 1: In-Process API Testing with WebApplicationFactory
ASP.NET Core provides WebApplicationFactory through the Microsoft.AspNetCore.Mvc.Testing package.
It allows developers to host an ASP.NET Core application in memory and send HTTP requests through an HttpClient.
Requests pass through the application's routing, middleware, model binding, and response serialization without requiring the application to open a network port.
Consider the following example:
public class OrdersApiTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly HttpClient _client;
public OrdersApiTests(WebApplicationFactory<Program> factory)
{
_client = factory.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
// Replace the real database registration with a test double here
});
}).CreateClient();
}
[Fact]
public async Task Unknown_order_returns_404()
{
var response = await _client.GetAsync("/orders/does-not-exist");
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
}
With minimal APIs, the test project may need access to the application's Program class. One common solution is to add the following declaration to the API project:
public partial class Program { }
Runner Requirements
This approach is highly portable because the tests can run using:
dotnet test
The runner generally needs only the .NET SDK and the required NuGet packages.
It works well with GitHub Actions, GitLab CI, and Jenkins without requiring a Docker daemon or an externally hosted application.
Advantages
Runs without opening a network port.
Tests real application routing and middleware.
Supports replacing services through dependency injection.
Integrates naturally with the .NET testing ecosystem.
Requires minimal CI infrastructure.
Limitations
The main challenge is managing external dependencies.
For example, if the application uses SQL Server, the tests need an alternative database or test environment. Developers can replace the database registration with SQLite in-memory or another suitable test implementation.
However, different database providers can behave differently. The EF Core InMemory provider does not reproduce relational database behavior, including constraints and transaction semantics.
External HTTP services also need to be replaced with test doubles or mocked responses.
Another limitation is that developers must write and maintain the test cases manually.
WebApplicationFactory is a strong choice for testing application behavior, but it does not automatically solve every database integration or test coverage problem.
Option 2: Database Integration Testing with Testcontainers for .NET
Testcontainers for .NET allows developers to start real dependencies in containers during test execution.
For an ASP.NET Core application, this could include SQL Server, PostgreSQL, Redis, or other infrastructure services.
When combined with WebApplicationFactory, the application can run in memory while connecting to a real database container.
This makes it possible to test database behavior using actual database constraints, transactions, and migrations.
For example, a test can start a SQL Server container, obtain its connection string, and configure the application's DbContext to use that database.
Runner Requirements
The main requirement is access to a Docker-compatible container runtime.
The exact configuration depends on the CI system.
GitHub Actions: Linux-hosted runners commonly provide Docker support. The selected runner and job configuration must still be compatible with the required container image.
GitLab CI: Jobs can use Docker-in-Docker or a runner configured with access to a Docker daemon. This requires appropriate runner configuration and security controls.
Jenkins: Agents need a compatible container runtime, and the Jenkins process must have permission to use it.
The test command can remain the same across all three systems, but the infrastructure setup is not identical.
Container image downloads and startup can also increase test execution time.
Outbound calls to third-party services should still be isolated. Tools such as HTTP mocks or local service simulators can provide predictable responses.
Advantages
Tests against real database engines.
Supports database migrations and relational constraints.
Reduces differences between test and production database behavior.
Can test multiple infrastructure dependencies.
Works alongside WebApplicationFactory.
Limitations
Requires a Docker-compatible runtime.
Runner configuration differs between CI systems.
Container startup and image downloads add execution time.
Container permissions introduce additional security considerations.
External services still require separate test strategies.
Testcontainers is particularly useful when database behavior is an important part of the application's correctness.
Option 3: API Testing with Postman Collections and Newman
Postman is widely used for designing, documenting, and testing HTTP APIs.
A Postman collection can contain requests, environment variables, and JavaScript assertions. Newman is its open-source command-line collection runner.
Newman allows teams to execute Postman collections from CI pipelines.
For example, a collection might contain tests for:
Retrieving all orders.
Retrieving an order by ID.
Creating a new order.
Updating an existing order.
Handling invalid requests.
Returning the expected HTTP status codes.
A simple Newman command looks like this:
newman run OrdersApi.postman_collection.json
The command can be included in GitHub Actions, GitLab CI, or Jenkins.
Runner Requirements
Newman requires Node.js and a reachable API instance.
Unlike WebApplicationFactory, it does not host the ASP.NET Core application in memory.
The pipeline must provide an application environment to test. This could be a deployed test environment or an application instance started as part of the pipeline.
If the tests use a shared staging environment, simultaneous pipeline executions can interfere with each other.
To avoid this, teams can provision an isolated application and database for each test run. However, that introduces additional infrastructure and configuration requirements.
Advantages
Tests APIs through ordinary HTTP requests.
Supports reusable collections and environment variables.
Allows QA engineers to contribute without writing C#.
Provides command-line execution for CI pipelines.
Useful for smoke testing deployed applications.
Limitations
Requires a running API instance.
Needs Node.js on the CI runner.
Requires an isolated environment to avoid shared-state problems.
Assertions are maintained separately from the C# application code unless the collection is version-controlled with the repository.
Test coverage depends on the requests and assertions developers create.
Postman and Newman are useful when teams need a shared collection of HTTP tests or want to validate an API after deployment.
Option 4: Record-and-Replay Testing
Record-and-replay testing uses previously captured application interactions to create repeatable test scenarios.
Instead of manually writing every request and expected response, a recording process observes interactions with the application and stores information that can be used during later test executions.
Depending on the implementation, recordings may include:
Incoming HTTP requests.
HTTP responses.
Requests made to external services.
Responses received from those services.
Selected database interactions.
During replay, the test environment sends requests to the application and provides recorded responses for supported dependencies.
This can reduce the need to connect to live external services during every test run.
How It Works
A typical record-and-replay workflow has four stages.
Record application interactions: Run the application and exercise its API endpoints using representative requests.
Review the recordings: Inspect captured requests, responses, and dependency interactions. Remove sensitive information and verify that the recorded behavior is suitable for testing.
Store test data: Keep the approved recordings in the repository so that changes can be reviewed alongside application code.
Replay the scenarios: Execute the recorded requests against a new application build and compare the responses with the expected behavior.
The exact implementation depends on the selected tool. Some solutions capture network traffic, while others use application instrumentation or other integration techniques.
Runner Requirements
The requirements vary depending on the recording and replay technology.
Some implementations require a local runtime or command-line utility. Others may require additional services or operating-system support.
If dependency responses can be replayed locally, the CI job may not need access to the original external services.
However, teams should verify the tool's supported operating systems, .NET versions, dependency integrations, and execution model before adopting it.
Advantages
Can reduce dependence on shared external environments.
Reuses previously captured API interactions.
Helps create repeatable test scenarios.
Can reduce the amount of manually written request and response test data.
Recorded test cases can be version-controlled.
Limitations
Recordings need review before being accepted as expected behavior.
Captured interactions can become outdated when application behavior changes.
Sensitive information must be removed or protected.
Replay does not necessarily validate real database constraints or migrations.
Coverage depends on the quality and variety of recorded scenarios.
Tool capabilities differ, so compatibility must be evaluated before implementation.
Record-and-replay testing can complement traditional API tests, particularly when an application communicates with several external services.
It should not be treated as a complete replacement for database integration tests or manually designed assertions.
Comparing the Four Approaches
The following table summarizes the main differences between the four testing strategies.
Approach | One Command Everywhere | No Shared Environment | Runner Requirements | HTTP Boundary Testing | Maintenance Effort |
|---|
WebApplicationFactory | Yes, using dotnet test | Yes, with test doubles | .NET SDK | Yes, in-process | Manual C# test maintenance |
Testcontainers + WebApplicationFactory | Yes, with runner setup | Yes | .NET SDK and Docker-compatible runtime | Yes, in-process | Manual C# test maintenance |
Postman + Newman | Yes, using Newman | Only with an isolated environment | Node.js and a running API | Yes, over HTTP | Manual JavaScript assertions |
Record-and-replay | Depends on the tool | Can avoid shared dependencies | Depends on the implementation | Depends on the recording and replay mechanism | Recording review and maintenance |
No single approach covers every testing requirement.
WebApplicationFactory is useful for application-level API testing. Testcontainers provides stronger database integration coverage. Postman and Newman support HTTP testing from a separate client environment. Record-and-replay can make selected interactions repeatable without contacting the original dependencies.
Keeping CI Pipeline Files Thin
Regardless of the testing approach, the most important part of portability is keeping test logic in the repository.
Instead of implementing different test steps in each CI configuration, create a shared script that runs the required checks.
For example, create a file named ci/api-tests.sh:
#!/usr/bin/env bash
set -euo pipefail
dotnet restore
dotnet build --no-restore
dotnet test --no-build
This is a basic example for a solution using .NET tests. Additional steps can be added for publishing the API, starting dependencies, running external HTTP tests, or executing replay scenarios.
The script should return a non-zero exit code when a required test fails. The CI system can then mark the job as failed.
GitHub Actions
Create .github/workflows/api-tests.yml:
on: [pull_request]
jobs:
api-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '10.0.x'
- run: chmod +x ./ci/api-tests.sh
- run: ./ci/api-tests.sh
GitLab CI
Add the following job to .gitlab-ci.yml:
api-tests:
tags: [linux]
script:
- chmod +x ./ci/api-tests.sh
- ./ci/api-tests.sh
The selected GitLab runner must have the required .NET SDK installed.
Jenkins
Add the following pipeline to Jenkinsfile:
pipeline {
agent {
label 'linux'
}
stages {
stage('API tests') {
steps {
sh 'chmod +x ./ci/api-tests.sh'
sh './ci/api-tests.sh'
}
}
}
}
The Jenkins agent must also have the required .NET SDK installed.
These examples keep the actual test execution logic in one shared script. Changes to the test workflow can be made in the repository without maintaining separate implementations in three CI systems.
Choosing the Right Approach
The best testing strategy depends on the application's architecture, dependencies, and CI infrastructure.
If the application has relatively few dependencies and the team already maintains good WebApplicationFactory tests, there may be no reason to introduce additional tooling.
If database behavior is important, Testcontainers can provide more realistic integration coverage. The team must account for Docker availability and container configuration across its runners.
If QA engineers need to maintain reusable HTTP request collections, Postman and Newman can be a practical option. However, the pipeline must provide an isolated application environment.
If the application has many external dependencies, record-and-replay testing may help make selected scenarios repeatable. The team should evaluate recording quality, dependency support, and replay limitations before relying on it.
For organisations using GitHub Actions, GitLab CI, and Jenkins, a layered approach can provide a good balance:
Run WebApplicationFactory tests on every pull request.
Use recorded API scenarios for repeatable tests where they provide value.
Run Testcontainers-based database integration tests on pull requests or scheduled builds, depending on execution time.
Use Postman and Newman for selected API smoke tests after deployment.
This approach separates application testing, database integration testing, and deployed API validation instead of expecting one tool to handle everything.
Conclusion
Maintaining consistent API tests across GitHub Actions, GitLab CI, and Jenkins becomes challenging when each pipeline has different dependencies and execution rules.
ASP.NET Core developers have several open-source options for addressing this problem. WebApplicationFactory provides fast in-process testing, Testcontainers supports realistic database integration, Postman and Newman provide external HTTP testing, and record-and-replay techniques help reproduce selected application interactions.
Each approach has its own strengths and limitations. The right choice depends on the level of testing required and the infrastructure available to the team.
The most important principle is to keep test logic independent of the CI platform. When all pipelines execute the same test scripts and use controlled dependencies, test results become easier to reproduce, compare, and trust.
Join the conversation! Your thoughts help the community grow.