Cloud concept · Updated

Unit Testing - Unit testing verifies the behavior of a small, independently testable piece of code while controlling its dependencies.

In short

Unit tests check functions, methods, or small modules against explicit expectations using controlled inputs and dependencies. They provide fast feedback during development and help CI/CD pipelines reject regressions before code reaches deployed data, application, or machine learning workflows.

Cloud Concepts Hub →

6 key aspects of Unit Testing

Effective unit tests isolate meaningful behavior, run reliably and quickly, and enforce reviewed expectations within a broader testing strategy.

01
Small behavioral scope

Test a coherent function, method, or module contract rather than an entire deployed workflow. Favor assertions about observable behavior so internal refactoring does not unnecessarily break tests.

02
Controlled dependencies

Use dependency injection, stubs, mocks, or fakes to control external systems and nondeterministic inputs. Keep real service compatibility checks in a separate integration test suite.

03
Fast and reproducible feedback

Use small fixtures, controlled clocks and randomness, and independent test state. Fast, repeatable tests can run on every proposed change without provisioning cloud resources.

04
Automated quality gates

Run tests in CI and propagate failures to the pipeline. Enforce required coverage thresholds through the test or coverage tooling rather than assuming report publication blocks deployment.

05
Meaningful assertions and coverage

Check expected results, invalid inputs, boundary conditions, and failure handling. Use statement and branch coverage to identify gaps, not as proof that the software is correct.

06
Layered validation for data and AI

Unit-test deterministic transformations and application logic, including code around model calls. Add integration tests, model evaluations, security checks, and production monitoring for risks that isolated tests cannot establish.

Cloud services for Unit Testing

Engineers can use Amazon Q Developer to help create tests and separate business logic from cloud dependencies, including dependencies in AWS Lambda applications. AWS CodePipeline can invoke AWS CodeBuild to run the versioned test suite, generate coverage data, and publish supported reports. Configure the build commands to fail on unsuccessful tests or unmet coverage requirements so normal pipeline progression stops before deployment. Later stages should run integration tests against real cloud resources and, for AI workloads, appropriate model evaluations. This separates fast checks of code behavior from checks of deployed configuration, service compatibility, and model quality.

AWS CodeBuildAWS CodeBuild

Runs test frameworks and coverage tools in managed build environments using commands defined in a build specification. It can collect supported test and coverage reports, while configured commands must return failure when tests or required coverage checks fail.

AWS CodePipelineAWS CodePipeline

Orchestrates delivery stages and can invoke CodeBuild to execute unit tests before later deployment stages. A failed test action blocks normal pipeline progression; CodePipeline does not itself execute unit-test assertions.

Amazon Q

Can assist with writing tests, identifying edge cases, and refactoring code to make dependencies easier to isolate. Generated tests still require review for meaningful assertions, incorrect assumptions, and missing failure paths.

AWS LambdaAWS Lambda

Function handler code and supporting business logic can be unit-tested locally with synthetic events and substituted SDK clients. These tests do not verify deployed IAM permissions, event source configuration, or actual service interactions.

Unit Testing exam questions

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

MLA-C02 (beta)

An ML team uses AWS CodeBuild to test a Python component that submits training jobs. The component implements an application-level retry policy: after throttling errors, it waits 30, 60, and 120 seconds between attempts, with a maximum of four total attempts. It must immediately propagate nonretryable validation errors. Existing tests rely on a live service producing throttling errors and sometimes take several minutes. The team needs repeatable tests that verify retry counts, backoff intervals, and error propagation in less than 5 seconds without changing the production retry policy. Which approach meets these requirements?

Show answerCORRECT ANSWER

Inject a stubbed API client and a recording replacement for the sleep function. Test scripted sequences of throttling errors, successful responses, and validation errors, asserting call counts, requested delays, and propagated exceptions.

Controlled API responses make every retry scenario repeatable. Replacing sleep with a function that records requested delays eliminates waiting while preserving execution of the production retry logic. Assertions can verify recovery, the four-attempt limit, exact backoff intervals, and immediate propagation of validation errors.

AIP-C01

A team uses Amazon Q Developer to implement a response-cache module for a generative AI application. The module constructs a cache key from the tenant ID, prompt-template version, and normalized question. It returns a cached response only when the current time is strictly earlier than the entry's expiration time; otherwise, it invokes a model client and stores the new response.

A previous defect returned another tenant's cached response. Another defect served entries at their exact expiration time. Developers need fast, repeatable regression tests that exercise the module's actual key-construction and expiration logic without AWS credentials or network access.

Which test design best meets these requirements?

Show answerCORRECT ANSWER

Use Amazon Q Developer to generate parameterized tests with an injected clock, an in-memory cache store, and a stub model client. Execute the real cache module with fixtures that vary tenant ID and prompt-template version independently. Test immediately before, exactly at, and after expiration, asserting returned values and model-client invocation counts.

This design isolates external dependencies while exercising the behavior under test. Independently varying key fields detects cache-key collisions. A controlled clock tests expiration boundaries without waiting, and distinct cached and generated responses plus invocation assertions distinguish cache hits from misses.

DOP-C02

A company uses AWS CodeBuild to validate a Python AWS Lambda authorizer before deployment. To reduce latency, the authorizer retains a module-level cache of tenant authorization policies between invocations. A production incident occurred when a request from one tenant was evaluated with another tenant's cached policy after the execution environment was reused.

Existing tests reload the authorizer module before every simulated invocation. All tests pass. The team must add a regression gate that detects cross-tenant policy leakage during execution-environment reuse. The gate must run without deploying resources, making network requests, or using AWS credentials.

Which testing approach should a DevOps engineer implement?

Show answerCORRECT ANSWER

Load the real authorizer module once per test scenario and replace only its external policy-store client with a controlled stub. Submit requests from different tenants sequentially without clearing the cache, assert each tenant's expected decisions, and test both tenant orders. Reset module state between scenarios and fail CodeBuild on assertion failures.

This approach preserves the state lifecycle responsible for the defect while isolating external dependencies. Sequential invocations exercise the real cache and authorization logic, tenant-specific expectations detect leakage, and testing both orders catches asymmetric behavior. Resetting state between scenarios keeps scenarios independent without erasing the state needed within each scenario.

Practice Unit Testing in a full exam

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

Start a free practice exam

What is Unit Testing

3 min read

Unit testing checks a small unit of behavior independently from the wider application. A unit might be a function that normalizes a timestamp, a method that calculates a feature, or a module that constructs a model request. Tests supply known inputs, execute the code, and assert expected outputs, exceptions, or observable effects. The useful boundary is a coherent behavior, not necessarily one function: tests should protect the public contract without depending unnecessarily on internal implementation details.

In cloud applications, isolate business logic from network access, storage, environment configuration, and runtime handlers. Inject dependencies so tests can substitute controlled implementations for database clients, object storage clients, clocks, and model endpoints. Stubs return predetermined responses; mocks can also verify interactions, such as whether a retry occurred. These substitutes keep tests fast and repeatable without provisioning infrastructure or requiring production credentials. However, a mocked service response cannot prove that real permissions, networking, or service contracts work.

For data engineering, test transformations with small, representative fixtures. A currency conversion function should handle valid amounts, missing values, unsupported currencies, and rounding boundaries. A record parser should reject malformed input and preserve required fields. Keep transformation logic separate from ingestion and persistence so it can be tested without launching an entire distributed job. Framework-specific transformations may need a local execution engine, while connectivity and distributed execution require additional tests.

Store tests alongside application code in version control and run them locally and automatically on proposed changes. A CI build should install pinned dependencies, execute the test framework, collect machine-readable results, and generate coverage reports. Configure test failures and required coverage thresholds to produce a failing build status so the delivery pipeline blocks promotion. Merely publishing a report does not enforce a quality gate. Keep test execution reproducible, avoid order-dependent shared state, and investigate flaky tests rather than repeatedly rerunning them until they pass.

Code coverage measures which statements or branches executed during tests; it does not establish that assertions are correct or requirements are complete. A test can execute every line while checking almost nothing. Use coverage to find untested behavior, especially error paths and boundary conditions, rather than treating a percentage as proof of quality. Excessive mocking can also make a suite brittle or allow incorrect assumptions about dependencies to go unnoticed.

Unit tests are one layer of an automated testing strategy. Integration tests check interactions between components or real services. Acceptance tests verify business requirements, and user interface tests exercise user-facing behavior. Security scans inspect code, dependencies, or running systems for vulnerabilities and are not substitutes for functional tests. Logging and monitoring reveal runtime behavior after deployment; they complement rather than replace pre-deployment testing.

For traditional machine learning, unit-test preprocessing, feature calculations, input validation, and inference response handling. Model quality, drift, training-serving consistency, and deployment performance require separate validation and operational checks. For generative AI applications, test prompt assembly, retrieval filtering, tool argument validation, and response parsing using fixed model responses. Evaluate real model outputs separately because they can vary and require task-specific quality and safety criteria. Amazon Q Developer can help generate tests and refactor code for testability, but engineers must review assertions, fixtures, and edge cases; generated tests can reproduce the same mistaken assumptions as generated implementation code.

Learn Unit Testing in lessons

7 short lessons on Unit Testing in 4 certification courses.

and 1 more lesson

Open the lessons