Lesson 1 of 3 Automated Testing in Pipelines

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 stageTestsEnvironment
Pull request buildLinting, unit tests, static analysis, dependency scanningCodeBuild container, no deployed infrastructure
Build stage after mergeThe same fast tests plus coverage reporting and image scanningCodeBuild
Test stageIntegration and API testsA test environment deployed by the pipeline
Staging stageEnd-to-end, load and performance testsA production-like environment
Production stageSmoke tests and monitoring of alarms during a bake periodProduction

The following diagram maps the pyramid onto the pipeline's stages.

The test pyramid and where each level runs. Many fast unit tests form the base, fewer integration tests the middle and a small number of slow end-to-end tests the top. In the pipeline, linting, unit tests and static analysis run in the pull request build and again after merge, integration and API tests run in a test environment, end-to-end and load tests run in staging, and smoke tests with an alarm bake period verify production. Fast tests run first so a change fails early and cheaply.
Slower tests run only for changes that passed the faster ones.
Put in order

Fail fast

Exam skills 1.2.4 · 1.2.1

Put the checks in the order a fail-fast pipeline runs them.

Log in to try this activity Interactive activities are free with an account.

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.

Pull request build
Started byPull request created, updated or reopened
PurposePrevent defects from being merged
Typical AWS mechanismCodeBuild webhook with pull request event filters
DeploysUsually no, or a temporary preview environment
Choose when Fast feedback to reviewers before merge.
Merge build
Started byPush or merge to the main branch
PurposeVerify the integrated code and produce the release artifact
Typical AWS mechanismCodePipeline source action or V2 push trigger
DeploysYes, into the delivery pipeline
Choose when Producing and promoting a release.

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.

Scenario questions

Design the test stages

Exam skills 1.2.1 · 1.2.4

Practise test environment and quality gate choices.

Log in to try this activity Interactive activities are free with an account.

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.

GateTypical toolsWhere it runs
Unit tests and coverage thresholdTest framework with a coverage tool; CodeBuild test and coverage reportsPull request build and Build stage
Static application security testing and dependency scanningAmazon Inspector code security, open-source scanners in CodeBuildPull request build
Container image vulnerabilitiesAmazon ECR enhanced scanning with Amazon InspectorAfter the image is pushed
IaC policycfn-lint, CloudFormation Guard, cdk-nag, CloudFormation HooksBuild stage and deployment time
Deployment healthCloudWatch alarms during a bake period, deployment rollbackEach 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.

Worked example

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.

  1. Separate by speed

    Run linting and unit tests in a pull request build and again in the Build stage. They finish in minutes.

  2. 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.

  3. 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.

  4. Verify production

    Run smoke tests after the production deployment and watch alarms in a bake period.

Check yourself

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.