Lesson 3 of 5 CI/CD and Testing for ML and AI

Automating Tests Across ML Pipelines

This lesson explains automated tests at several levels that catch code bugs, bad data, weak models and infrastructure problems before production.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish the testing layers.
  • Place each kind of test in the MLOps process.
  • Design effective quality gates.

Testing layers

Unit tests check feature engineering functions, data parsers and inference handlers with small fixed inputs. Data validation tests check schema, ranges and completeness with Glue Data Quality or custom checks. Model quality gates compare evaluation metrics with thresholds and with the current production model, using a ConditionStep. Bias and explainability checks use Clarify. Integration tests deploy to a staging endpoint and send real requests to check request and response formats. Load tests confirm latency and throughput under expected traffic, for example with Inference Recommender.

Sort into groups

Which test layer?

Exam skills 3.3.4

Sort each test into the layer of an ML pipeline it belongs to.

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

Where tests run

Different tests run at different points in an MLOps process. Unit tests of code, such as feature transformation functions, run in CodeBuild when code is committed. Data validation and model quality gates run inside the SageMaker pipeline, where the data and model are available.

Integration and load tests run in the deploy pipeline after deployment to staging and before the manual approval for production, because they need a running endpoint. A failing test at any stage stops the pipeline and reports why, so problems are fixed before they reach production.

Testing generative AI components

Generative AI adds prompt regression tests, structured output validation, safety tests with adversarial prompts and guardrail tests, covered in the next lessons. The following table summarises the layers.

TestChecksWhere
UnitFeature and inference codeCodeBuild on commit
Data validationSchema, ranges, completenessPipeline start
Model quality gateMetrics vs. threshold and championPipeline ConditionStep
Bias checkClarify metrics vs. limitsPipeline ClarifyCheckStep
IntegrationEndpoint request/response contractStaging, deploy pipeline
LoadLatency and throughputStaging, before approval

Designing effective quality gates

A quality gate compares a new model with expectations, and the way the comparison is defined decides whether it catches real regressions. An absolute threshold, such as AUC of at least 0.85, guarantees a minimum standard. A relative threshold, such as no more than 0.01 below the current production model, catches a model that passes the minimum but is worse than what users already have. Most pipelines use both, with a small tolerance so that random variation between training runs does not block every release.

Aggregate metrics can hide regressions on important groups. Computing metrics per segment, such as per region, product line or customer type, and gating on the most important segments prevents a model that improves overall while failing for one market. The evaluation data itself must be fixed and versioned; a test set that changes between runs makes the gate's decisions incomparable.

The order of tests matters for cost and speed. Cheap, fast checks, such as unit tests and data validation, should run first, so that a broken feature function or a malformed input file stops the pipeline before hours of training. Expensive checks, such as load tests on a staging endpoint, run last, only for candidates that passed everything else. Tests must also be deterministic enough to trust: a test that fails at random is soon ignored, so fixed random seeds, fixed test data and tolerances appropriate to the metric's natural variation are part of good test design.

The following diagram shows a mismatch between a new model version and its production client, and the test that catches it.

Why the new model version failed only in production. It expects 42 features, but the production client still sends 41; unit tests and the quality gate test the model, not the interface. An integration test that replays real production requests against the staging endpoint fails with a 4xx error before production approval.
The scenario is illustrative.
Select all that apply

A good quality gate

Exam skills 3.3.4

Identify the properties of an effective model quality gate.

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

A broken contract

A new model version expects 42 features, but the production client sends 41 because a feature was added in training. Unit tests and the quality gate passed.

An integration test that replays a sample of real production requests against the staging endpoint would fail with a 4xx error before production approval. Adding that test to the deploy pipeline closes the gap; the model itself was fine, but the interface between client and model had changed.

Check yourself

Which tests catch changes to an endpoint's request format?

Show answer

Integration tests against a staging endpoint.

Exam Tips

Remember for the exam

  • Unit tests in CodeBuild; data and quality gates in the SageMaker pipeline.
  • Integration tests against a staging endpoint catch request format changes.
  • Load tests confirm latency before production approval.
  • Gate on both absolute thresholds and comparison with the champion.
  • A failing test must stop the pipeline.