Run Tensorflow unit tests
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
TensorFlow unit tests should verify tensor behavior quickly and deterministically, without running expensive training loops. Flaky tests usually come from uncontrolled randomness, unstable device assumptions, or loose numeric assertions. A strong test setup focuses on repeatability, small fixtures, and clear failure messages.
Build Deterministic Tensor Tests
Set seeds and keep test inputs small.
What this validates:
- dtype and shape compatibility,
- numerical output correctness,
- deterministic behavior for fixed inputs.
Prefer unit tests that run in milliseconds and isolate one behavior each.
Test tf.function and Eager Consistency
Many bugs appear only when code is traced by tf.function.
This catches differences between eager and traced execution paths early.
Choose Assertion Style for Numeric Stability
Use tolerance-aware assertions for floating-point outputs.
Use strict equality only for integer or exact symbolic operations.
Run with Pytest and Keep Scope Clear
Run unit tests separately from heavier integration tests.
A clean test layout helps:
tests/unitfor pure tensor logic,tests/integrationfor data pipelines and training steps,tests/e2efor full model workflows.
This keeps feedback loop fast while preserving deeper coverage in slower stages.
CPU and GPU Test Strategy
Do not require GPU for baseline unit tests unless feature is GPU-specific. Keep most unit tests CPU-compatible so CI remains portable.
If GPU-specific kernels must be tested, mark those tests explicitly and run them in dedicated pipelines.
CI Reliability Checklist
For CI stability:
- log TensorFlow and Python versions,
- pin key dependencies,
- isolate random seeds where stochastic ops are tested,
- fail fast on first critical module failure.
Useful version log command:
When tests suddenly fail after dependency upgrades, version logs make root-cause analysis much faster.
Organize Test Data and Fixtures
Keep synthetic fixtures tiny and local to test modules unless shared across many cases. For shared fixtures, use explicit factory helpers so shape and dtype assumptions stay visible.
Example helper:
Using helper factories reduces repeated setup code and keeps fixture changes centralized when model interfaces evolve. It also makes new tests easier to review because setup assumptions are standardized. Consistent fixtures reduce flaky CI behavior over time.
Common Pitfalls
- Mixing long training scenarios into unit test stage.
- Using no seed control in stochastic operations.
- Overly broad numeric tolerances that hide regressions.
- Assuming GPU availability in generic CI runners.
- Testing multiple behaviors in one test and getting ambiguous failures.
Summary
- Keep TensorFlow unit tests small, deterministic, and behavior-specific.
- Validate eager and
tf.functionpaths when relevant. - Use tolerance-aware assertions for floating-point results.
- Separate fast unit tests from heavy integration or training tests.
- Make CI reproducible with version logging and controlled environments.
Related reading
- Run Tensorflow with NVIDIA TensorRT Inference Engine
- Running a Tensorflow model on Android
- Running a tensorflow program multiple times each time afresh
- Running a Tensorflow program on an IPU Model throws an Illegal instruction core dumped error
- Running Adam Optimizer
- Running Keras model for prediction in multiple threads
- Running a single test from unittest.TestCase via the command line
- Running a specific test case in Django when your app has a tests directory
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.