Cloud concept · Updated

Code Coverage - Code coverage measures how much application code is executed while automated tests run.

In short

Code coverage identifies which parts of a program a test suite exercises and which remain untested. In CI/CD pipelines, coverage reports and enforceable thresholds help teams detect testing gaps before deployment, but coverage alone does not prove that software behaves correctly.

Cloud Concepts Hub →

6 key aspects of Code Coverage

Effective code coverage combines accurate execution measurement, explicit pipeline enforcement, and risk-based interpretation without mistaking coverage percentages for proof of correctness.

01
Execution-based measurement

Coverage records which code executes during a test run, not whether every behavior is correct. Include unexecuted application files in the measurement scope to avoid inflated results.

02
Multiple coverage metrics

Line, statement, function, and branch coverage expose different testing gaps. Branch coverage is particularly useful for finding untested decision outcomes and error paths.

03
Enforceable pipeline quality gates

Configure the coverage tool or a validation script to return a nonzero exit status when requirements are not met. Report publication alone does not block a build or deployment.

04
Risk-based coverage policies

Set thresholds appropriate to the application and prioritize critical logic and changed code. Review exclusions explicitly so they do not conceal untested production behavior.

05
Reproducible reporting and aggregation

Use consistent instrumentation, source revisions, and inclusion rules across builds. Merge execution data from parallel workers before calculating overall coverage rather than averaging percentages.

06
Complementary evidence of test quality

Combine coverage with meaningful assertions, integration tests, and reviews of boundary conditions. High coverage does not establish that cloud permissions, service integrations, or security controls work correctly.

Cloud services for Code Coverage

AWS CodePipeline coordinates the delivery workflow and invokes AWS CodeBuild to execute unit tests with a language-appropriate coverage tool. CodeBuild publishes supported coverage reports for inspection, while the test command or a separate validation script enforces the coverage threshold through its exit status. When that validation fails the build action, dependent deployment actions do not proceed. Amazon S3 can retain exported report data and artifacts beyond the immediate build workflow, allowing engineers to investigate regressions against the relevant source revision. Keep report generation, threshold enforcement, and artifact retention explicitly configured because each solves a different part of the coverage workflow.

AWS CodeBuildAWS CodeBuild

Runs unit tests and coverage tools in a managed build environment and collects supported coverage formats, such as JaCoCo XML and Cobertura XML, through buildspec report configuration. Enforce thresholds through commands that fail the build; report collection itself is not a coverage quality gate.

AWS CodePipelineAWS CodePipeline

Orchestrates source, build, test, and deployment actions. A failed CodeBuild action caused by failing tests or insufficient coverage prevents dependent downstream deployment actions from proceeding.

Amazon S3Amazon S3

Stores exported coverage data and build artifacts for longer-term retention and investigation. Associate stored reports with source revisions and apply access controls and lifecycle rules appropriate to their contents.

Code Coverage exam questions

Real practice exam questions on Code Coverage, with the correct answer and its explanation. Answer them with every option in the exam simulator.

DVA-C02

A developer uses Amazon Q Developer to generate tests for a Python shipping-cost calculator. All tests pass, and the CI coverage report shows 100% line coverage. A review finds that the tests execute each conditional expression but never exercise several alternative outcomes, including the surcharge for oversized packages. The team requires at least 90% coverage of decision outcomes before deployment. Which approach should the developer use?

Show answerCORRECT ANSWER

Ask Amazon Q Developer to generate additional tests for uncovered decision outcomes with assertions for expected charges. Enable branch measurement in the coverage tool and configure the test command to fail below 90% branch coverage, preserving its failing exit status in CI.

Branch coverage measures whether alternative decision outcomes are exercised, exposing gaps that line coverage can miss. Amazon Q Developer can help generate targeted tests, while assertions check expected behavior. An enforced branch-coverage threshold and an unmasked failure status prevent deployment when the requirement is not met.

DOP-C02

A company uses AWS CodePipeline with an AWS CodeConnections source action for a private GitHub repository. The source action produces a ZIP artifact that an AWS CodeBuild action uses to run unit tests and generate coverage data. Test failures already fail the build.

The company now requires at least 90% line coverage for application lines added or modified since the last approved release. A trusted process supplies that release's commit SHA to the build. A version-controlled coverage checker uses local Git history to identify changed lines and returns a nonzero exit code when their coverage is below 90%. The approved commit is an ancestor of the candidate commit.

The checker currently fails because the extracted source artifact has no Git metadata. Each pipeline execution must evaluate its own source revision, even if additional commits reach the branch while the execution is queued. The team wants to retain the existing checker and use native source-action integration rather than implement a separate repository checkout workflow.

Which solution meets these requirements?

Show answerCORRECT ANSWER

Configure the source action to use the full-clone output format, CODEBUILD_CLONE_REF. Grant the CodeBuild service role permission to use the connection. Let CodeBuild obtain the execution's source revision, ensure the approved commit is available in local history, and run the tests and coverage checker with failures propagated to the build.

The full-clone output format supplies a Git reference that a downstream CodeBuild action can use to access the repository and Git metadata for the pipeline's selected revision. The CodeBuild role needs permission to use the connection. With the approved commit available, the checker can identify changed lines and enforce their coverage threshold. A failed build blocks subsequent pipeline stages.

Practice Code Coverage in a full exam

Questions on Code Coverage in each practice exam, with every answer option and an explanation for each:

Start a free practice exam

What is Code Coverage

3 min read

Code coverage is a measurement of code execution during testing. A coverage tool instruments application code or collects runtime execution data, then maps that data back to the source. The resulting report highlights exercised and unexercised code and calculates percentages for particular coverage metrics. Engineers use these results to identify missing tests, review changes, and enforce testing standards in continuous integration and continuous delivery pipelines.

Different metrics reveal different gaps. Line coverage measures executed source lines, statement coverage measures executed statements, and function coverage measures invoked functions. Branch coverage measures whether the alternative outcomes of decisions have been exercised. A test that executes an if statement might cover its line without testing both its true and false outcomes. Consequently, a high line coverage percentage can coexist with untested error handling or decision paths.

A typical cloud pipeline checks out a specific source revision, installs pinned dependencies, runs unit tests with coverage enabled, evaluates coverage requirements, and publishes reports. For example, a Python build can run pytest --cov=src --cov-report=xml:coverage.xml --cov-fail-under=85. With pytest-cov installed, this command measures the specified application source, writes an XML report, and returns a failing exit status when total coverage falls below 85 percent. Test failures also cause the test command to fail. The build configuration must preserve these failures rather than masking them with shell commands that always return success.

In an AWS implementation, AWS CodeBuild can execute the tests and collect supported coverage reports through the reports section of buildspec.yml. Test-result reports and code coverage reports serve different purposes: the former describe test outcomes, while the latter describe exercised code. Publishing either report does not automatically enforce a coverage threshold. Configure the test runner, coverage tool, or a separate validation command to fail the build when the coverage policy is violated. AWS CodePipeline can then prevent downstream deployment actions from proceeding when the associated build action fails.

Thresholds should reflect application risk and the existing codebase. A team might require an overall minimum while also requiring stronger coverage for changed code, avoiding a situation where a large, well-tested module hides an untested new feature. For a payment calculation, tests should exercise valid amounts, rounding boundaries, invalid input, and failure behavior rather than merely calling the calculation once. Review uncovered branches alongside requirements and defect history instead of treating an arbitrary percentage as the sole release criterion.

Coverage has important limitations. Executing a line does not demonstrate that a test checks its result, and 100 percent coverage does not prove correctness, security, or complete input coverage. Unit tests that mock a cloud SDK can thoroughly exercise application logic without validating IAM permissions, network access, or actual service behavior. Integration and end-to-end tests remain necessary for those concerns. Mutation testing can provide additional evidence about assertion effectiveness, although it increases execution cost.

Reliable measurement also requires consistent configuration. Include application files that tests never import, document justified exclusions such as generated code, and use compatible source mappings for transpiled applications. Parallel test workers should produce mergeable coverage data for the same source revision; simply averaging their percentages is not a valid substitute for merging execution records. Instrumentation adds runtime and storage overhead, so teams commonly run fast unit coverage checks on each change and schedule more expensive suites separately. Retain reports with revision metadata so engineers can investigate regressions and compare results meaningfully.

Learn Code Coverage in lessons

1 short lesson on Code Coverage in 1 certification course.

Open the lessons