0
0
MLOpsdevops~10 mins

Automated testing for ML code in MLOps - Commands & Configuration

Choose your learning style9 modes available
Introduction
Automated testing for ML code helps catch errors early by running checks on your machine learning scripts automatically. It ensures your ML models and data processing work as expected without manual testing every time you change code.
When you want to verify that your data preprocessing functions handle different input formats correctly
When you need to check that your model training code produces consistent results after changes
When you want to ensure your evaluation metrics calculations are accurate
When you want to catch bugs in feature engineering before deploying models
When you want to automate testing to save time and avoid manual errors
Commands
Run automated tests defined in the test_ml_code.py file to check ML code correctness.
Terminal
pytest test_ml_code.py
Expected OutputExpected
============================= test session starts ============================== collected 3 items test_ml_code.py ... [100%] ============================== 3 passed in 0.05s ===============================
-v - Show detailed test results
-k - Run tests matching a keyword expression
Discover and run all unit tests in the 'tests' directory to validate ML code components.
Terminal
python -m unittest discover tests
Expected OutputExpected
... ---------------------------------------------------------------------- Ran 3 tests in 0.012s OK
-v - Run tests with verbose output
Key Concept

If you remember nothing else from this pattern, remember: automated tests catch ML code errors early and save time by running checks automatically.

Code Example
MLOps
import unittest
import numpy as np

def preprocess(data):
    return (data - np.mean(data)) / np.std(data)

class TestMLCode(unittest.TestCase):
    def test_preprocess(self):
        data = np.array([1, 2, 3, 4, 5])
        processed = preprocess(data)
        self.assertAlmostEqual(np.mean(processed), 0, places=6)
        self.assertAlmostEqual(np.std(processed), 1, places=6)

    def test_model_training(self):
        np.random.seed(0)
        data = np.random.rand(10, 2)
        labels = np.random.randint(0, 2, 10)
        # Dummy training: sum features > 1 predicts 1 else 0
        predictions = (np.sum(data, axis=1) > 1).astype(int)
        accuracy = np.mean(predictions == labels)
        self.assertGreaterEqual(accuracy, 0)

    def test_metric_calculation(self):
        true = np.array([1, 0, 1, 1])
        pred = np.array([1, 0, 0, 1])
        accuracy = np.mean(true == pred)
        self.assertEqual(accuracy, 0.75)

if __name__ == '__main__':
    unittest.main()
OutputSuccess
Common Mistakes
Not writing tests for data preprocessing functions
Data issues can silently cause model failures or poor performance if preprocessing is not tested.
Write tests that check data inputs and outputs for preprocessing steps.
Ignoring test failures and continuing development
Ignoring failures can let bugs accumulate and cause bigger problems later.
Fix test failures immediately to keep code reliable.
Writing tests that depend on random outputs without fixing seeds
Tests may fail unpredictably due to randomness in ML code.
Set random seeds in tests to ensure consistent results.
Summary
Use pytest or unittest commands to run automated tests on ML code.
Write tests for data preprocessing, model training, and metric calculations.
Fix test failures immediately to keep ML code reliable and consistent.