Designing a Test Strategy for CI/CD Pipelines
Automated tests replace the manual verification that once made releases slow. To do this well, tests must be organised by speed and scope and placed at the right points of the pipeline. This lesson explains the types of automated tests, how they map to pipeline stages, how pull request and merge builds differ, and how temporary test environments are created and removed.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the main types of automated tests and what each one verifies.
- Explain how the test pyramid guides the balance and placement of tests in a pipeline.
- Distinguish between the purposes of pull request builds and merge builds.
- Explain how ephemeral test environments are created and removed by a pipeline.
- Describe quality gates and the tools that implement them, including the current status of Amazon CodeGuru.
Types of Automated Tests
Automated tests differ in what they exercise, how long they take and how reliable their results are. A pipeline usually combines several types, described below.
- Unit tests
- Test a single function or class in isolation, replacing dependencies with mocks. They run in milliseconds, need no infrastructure and run on every commit.
- Static analysis
- Linting, type checking, IaC template validation and static application security testing (SAST). These inspect code without running it.
- Dependency and image scanning
- Software composition analysis (SCA) checks libraries for known vulnerabilities; image scanning checks container images, for example with Amazon Inspector.
- Integration tests
- Test that components work together with real dependencies, such as a database, a queue or another service. They need a deployed environment.
- End-to-end and acceptance tests
- Exercise the system through its public interface as a user would, such as calling APIs or driving a browser, to confirm business behaviour.
- Performance, load and stress tests
- Measure latency and throughput under expected load (load), find the breaking point (stress) and compare results between versions (benchmarking).
- Smoke tests
- A short set of checks run immediately after a deployment to confirm the new version basically works, such as a health endpoint returning success.
The Test Pyramid and Pipeline Placement
The test pyramid describes the healthy proportion of tests. The wide base consists of many fast unit tests; the middle has fewer integration tests; the narrow top has a small number of slow end-to-end tests. An inverted pyramid, with most coverage coming from end-to-end tests, gives slow feedback and flaky results, because end-to-end tests depend on many components and are sensitive to timing.
The pyramid maps directly onto pipeline stages. Fast tests run first, so a change fails as early and cheaply as possible; slower tests run only for changes that passed the fast ones. A typical sequence is shown below.
| Pipeline stage | Tests | Environment |
|---|---|---|
| Pull request build | Linting, unit tests, static analysis, dependency scanning | CodeBuild container, no deployed infrastructure |
| Build stage after merge | The same fast tests plus coverage reporting and image scanning | CodeBuild |
| Test stage | Integration and API tests | A test environment deployed by the pipeline |
| Staging stage | End-to-end, load and performance tests | A production-like environment |
| Production stage | Smoke tests and monitoring of alarms during a bake period | Production |
The following diagram maps the pyramid onto the pipeline's stages.

Fail fast
Pull Request Builds and Merge Builds
A pull request build runs before code is merged. Its purpose is to stop a defective change from entering the main branch, so it must be fast and must report its result back to the pull request. On AWS, it is usually a CodeBuild project started by a webhook for pull request created and updated events, as described in the Source Control module. It runs against the merge result of the branch and its target where the provider supports it, so that the test result reflects the code that will exist after merging.
A merge build runs after the change has been merged. It repeats the fast tests on the integrated main branch, because two independently valid pull requests can conflict when combined, and it starts the delivery pipeline that builds the release artifact. In CodePipeline, the source action or a V2 push trigger on the main branch starts this build.
Ephemeral Test Environments
Integration and end-to-end tests need deployed infrastructure. Sharing one long-lived test environment between many changes causes interference: one change's test data affects another's results, and a broken deployment blocks everyone. An ephemeral environment is created from the IaC templates for one pipeline execution or one pull request, used for tests and deleted afterwards.
In CodePipeline, a CloudFormation action deploys a stack whose name includes #{codepipeline.PipelineExecutionId} or the branch name, a CodeBuild test action runs the tests against the stack outputs, and a CloudFormation action in DELETE_ONLY mode removes the stack. If the tests fail, the delete action does not run because the execution stops, which keeps the environment available for investigation; a scheduled cleanup function or an on-failure process should remove abandoned stacks to control cost. Because ephemeral environments are created from the same templates as production, they also continuously test the templates themselves.
Design the test stages
Quality Gates and Code Analysis Tools
A quality gate is a check that stops the pipeline when a measurable condition is not met: a failed test, coverage below a threshold, a critical vulnerability, a linting error or a policy violation in an IaC template. A gate is enforced by the exit code of the step that evaluates it, so any tool that returns a non-zero exit code on failure can act as a gate in a CodeBuild action. Gates should be objective and fast, because a gate that is often wrong or slow is soon bypassed.
Typical gates and the AWS or open-source tools that implement them are listed below. The later lessons of this module and the policy-as-code lesson describe them in detail.
| Gate | Typical tools | Where it runs |
|---|---|---|
| Unit tests and coverage threshold | Test framework with a coverage tool; CodeBuild test and coverage reports | Pull request build and Build stage |
| Static application security testing and dependency scanning | Amazon Inspector code security, open-source scanners in CodeBuild | Pull request build |
| Container image vulnerabilities | Amazon ECR enhanced scanning with Amazon Inspector | After the image is pushed |
| IaC policy | cfn-lint, CloudFormation Guard, cdk-nag, CloudFormation Hooks | Build stage and deployment time |
| Deployment health | CloudWatch alarms during a bake period, deployment rollback | Each deployment stage |
Service status
Amazon CodeGuru appears in the DOP-C02 list of in-scope services, but CodeGuru Reviewer has accepted no new repository associations since November 2025, and CodeGuru Security has been discontinued. For automated code and dependency scanning in new pipelines, use Amazon Inspector code security or open-source analysers run in CodeBuild. If an exam scenario names CodeGuru Reviewer for automated code review on pull requests, recognise the role it plays, which is automated review comments on code changes.
Restructuring a Slow Test Suite
A team's single CodeBuild action runs 2,000 unit tests, 300 integration tests against a shared test database and 20 browser tests, taking 50 minutes. Developers wait almost an hour to learn about a typo, and the shared database often causes failures unrelated to the change.
-
Separate by speed
Run linting and unit tests in a pull request build and again in the Build stage. They finish in minutes.
-
Isolate integration tests
Deploy an ephemeral stack with its own database per execution in a Test stage, run the integration tests against it, then delete it.
-
Move slow tests later
Run the browser tests in the Staging stage only for changes that passed the earlier stages, sharded across a CodeBuild batch to reduce duration.
-
Verify production
Run smoke tests after the production deployment and watch alarms in a bake period.
Why should a merge build rerun the unit tests that already passed in the pull request build?
Show answer
Because two pull requests that each pass on their own can conflict when both are merged. The merge build verifies the integrated main branch, which neither pull request build tested.
Exam Tips
Remember for the exam
- Order tests from fastest to slowest so that pipelines fail fast: static analysis and unit tests first, integration tests next, end-to-end and load tests last.
- Pull request builds (CodeBuild webhooks) prevent defects from being merged; merge builds verify the integrated branch and produce the release artifact.
- Ephemeral environments created from IaC per execution avoid interference between test runs; remove them with a DELETE_ONLY CloudFormation action.
- Smoke tests and an alarm bake period verify a deployment in production.
- A quality gate fails the pipeline through a non-zero exit code; CodeGuru Reviewer takes no new repositories, so use Amazon Inspector code security or open-source scanners in CodeBuild.