A13 · MLOps, Deployment and AI Infrastructure

Testing, Logging and Configuration

assert checks that a condition is true and raises an AssertionError if it isn't. pytest is the most popular Python test framework: you write functions whose names start with test_, with assert inside, and it finds and runs them.

15 min Advanced

What you need: The earlier topics of this module, and its prerequisites.

Definition

assert checks that a condition is true and raises an AssertionError if it isn't. pytest is the most popular Python test framework: you write functions whose names start with test_, with assert inside, and it finds and runs them.

In simple words

A test is a small program that checks your program. Instead of re-running everything by hand after each change, you run one command, and see at once what broke.

Why it matters

Tests let you change code with confidence. In ML they catch data bugs, too: a test can check that no labels are missing or that outputs stay in range.

How it works

# test_maths.py
import pytest
from maths import average

def test_average():
    assert average([2, 4]) == 3

def test_empty_raises():
    with pytest.raises(ValueError):
        average([])

# Run from the terminal:
# pytest

In code

A very small test runner, to show what pytest does for you.

testing.py
def average(numbers):
    if not numbers:
        raise ValueError("empty")
    return sum(numbers) / len(numbers)

def test_average():
    assert average([2, 4]) == 3

def test_single():
    assert average([5]) == 5

def test_wrong_on_purpose():
    assert average([1, 2]) == 2

for test in (test_average, test_single, test_wrong_on_purpose):
    try:
        test()
        print("PASS", test.__name__)
    except AssertionError:
        print("FAIL", test.__name__)

Output

PASS test_average
PASS test_single
FAIL test_wrong_on_purpose

Common mistakes

  • Comparing floats with ==. Use pytest.approx(0.3) for 0.1 + 0.2.
  • Writing tests only for the happy path. Test empty and wrong inputs as well.

Check yourself

Question 1

1. How does pytest find tests?

Question 2

2. What happens when an assert fails?

Summary

  • assert stops the program if a condition is false.
  • pytest finds and runs test_ functions.
  • Test the edge cases too.

Finished reading?

Sources: pytest documentation