MLOps & Production

Testing ML Code & Data

Writing unit tests, integration tests, behavioral tests, and data validation suites for reliable machine learning code.

🟡 intermediate5 min readmlops
Testing ML Code and Data requires testing software logic, data pipelines, and model behavioral predictions. Standard unit tests check feature transformation functions and tensor shape outputs. Data tests validate schema types and value ranges, while model behavioral tests verify directionality, edge cases, and invariance under noise.

The Silent Failure Problem

In standard software development, bugs cause explicit exceptions: NullPointerException or SyntaxError.

In machine learning development, code can run cleanly without any error while producing garbage predictions:

To catch silent failures, machine learning testing requires a Three Tier Testing Strategy.

┌─────────────────────────────────────────────────────────────┐
│ 1. CODE UNIT TESTS:      Feature functions, tensor shapes.  │
│ 2. DATA QUALITY TESTS:   Schema types, nulls, range bounds. │
│ 3. MODEL BEHAVIOR TESTS: Directionality, edge cases, noise. │
└─────────────────────────────────────────────────────────────┘

1. Code Unit Tests (pytest)

Test deterministic pipeline functions in isolation using small synthetic mock datasets:

2. Data Quality Tests (Great Expectations)

Validate incoming training and serving datasets before executing expensive model operations:

3. Model Behavioral Testing (CheckList Framework)

Evaluate trained model behaviors using three functional test patterns:

┌──────────────────────────┬──────────────────────────┬──────────────────────────┐
│ A. INVARIANCE TESTS      │ B. DIRECTIONAL TESTS     │ C. MINIMUM FUNCTIONALITY │
├──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ Changing non semantic    │ Increasing positive      │ Model correctly handles  │
│ input details (changing  │ input signals (adding    │ obvious benchmark edge   │
│ user name from John to   │ positive reviews) MUST   │ cases (negations like    │
│ Mary) MUST NOT change    │ INCREASE predicted sentiment| "not good").             │
│ model prediction score.  │ score.                   │                          │
└──────────────────────────┴──────────────────────────┴──────────────────────────┘

Say this out loud

Testing machine learning code requires testing software functions, data quality, and model behavioral predictions. Unit tests check feature engineering logic and tensor shapes. Data tests validate schemas and value bounds using tools like Great Expectations. Behavioral tests evaluate invariance, directional expectations, and minimum functionality to prevent silent production failures.

Followups to expect

  1. What is Integration Testing in ML? Testing the full end to end pipeline on a small synthetic dataset to ensure data loading, feature extraction, model prediction, and post processing execute cleanly together.
  2. What is Shadow Testing? Deploying a candidate model alongside the live production model to evaluate real world predictions on production traffic without displaying results to end users.

Check yourself

Question 1 of 3

Why do standard software unit tests fail to catch many critical errors in machine learning applications?

More in MLOps & Production

See all →
Data Drift vs Concept Drift4 minWhat to Monitor in Production5 minPoint-in-Time Correct Feature Joins5 min