pytest is a Python framework for automated tests. It uses plain test functions and assert statements, avoiding the TestCase class required by unittest. Install it with pip, name test functions with the test_ prefix, and run pytest from a terminal to discover tests in the current directory. section 10.3
Listing 10.11 In this code we use PyTest to construct a single test for our offline pipeline code. This code is similar to Listing 10.1, except now we are using PyTest’s syntax. In this code, the syntax is simpler because we no longer need to rely on unittest’s TestCase class.
import joblib #1
import pandas as pd
import sys
sys.path.append("../../.")
model_file = "../../ch9/model_outputs/churn_model.sav"
model = joblib.load(model_file) #2
def get_model_pred(
inputs: dict, model) -> float: #3
frame = pd.DataFrame.\
from_dict(inputs, orient = "index").\
transpose()
return round(model.\
clf.\
predict_proba(frame)[0][1], 5)
def test_1(): #4
inputs = {'total_day_minutes': 15, #5
'total_day_calls': 1,
'num_customer_service_calls': 0}
assert get_model_pred(inputs, model) == 0.01994 #6
A customer-churn prediction test loads a saved model, prepares one input record, and asserts that its probability matches an expected value. The test function is discovered because its name begins with test_. section 10.3
Use focused tests to check model outputs, input files, and failure conditions in a pipeline. pytest.raises can verify that invalid configuration data triggers an expected error. This supports small unit tests as well as checks across a fuller prediction workflow. section 10.3 section 10.6
When a project accumulates many tests, run only the checks needed:
- Run every discovered test in a directory with pytest, or target one test script with pytest filename.py. section 10.3
- Filter tests whose function names contain a word with -k, such as pytest test_full_pipeline_with_pytest.py -k file. section 10.3.1
- Mark a test with a decorator such as @pytest.mark.fetch_predictions, then select that group with -m fetch_predictions. section 10.3.1
Test a churn-model pipeline
A practical test can compare predictions for rows in a CSV file against stored expected values. Additional tests can pass a configuration with missing keys or a nonexistent input file, asserting that the pipeline raises the appropriate error rather than silently proceeding. section 10.3
pytest requires less boilerplate than unittest and can also run subsets, simulate different error conditions, and run tests in parallel. Test coverage can separately identify lines that tests have not executed. section 10.3 section 10.6












