TestCase is the base class used to organize unittest checks in Python. Create a class that derives from unittest.TestCase, then place related test methods and shared test data inside it. This gives a customer-churn pipeline one home for checking model predictions, configuration validation, and expected failures. section 10.2 section 10.6
Use a TestCase class to keep focused checks together:
- Subclass unittest.TestCase and define methods such as test_1 or test_input_file for individual checks. section 10.2
- Store reusable fixtures as class variables, such as a loaded model, a fake configuration filename, or a configuration with missing keys. section 10.2
- Use assertion methods to compare an actual prediction with an expected value and assertRaises to verify that invalid configuration produces the expected exception. section 10.2
- Keep tests small and cover both normal inputs and edge cases so a failure helps identify the faulty component quickly. section 10.2
Testing a churn-model pipeline
A TestModelOutputs class extends unittest.TestCase. It loads a saved churn model, reads test customer rows, and checks each predicted probability against an expected value. Other methods verify that missing configuration keys raise AssertionError and nonexistent files raise FileNotFoundError. Running the test script reports either OK or the failing test and error. section 10.2
Listing 10.7 This code creates a class we can use to run a collection of tests for the code script in Listing 10.6 The tests we write are in Listing 10.8. This listing shows the creates a class that has several variables we will use for the tests, such as examples of configs with missing keys or fake input files. Additionally, we create a method (get_model_pred) that handles fetching predictions from the customer churn model.
import pandas as pd #1
import joblib #1
class TestModelOutputs(unittest.TestCase): #2
model_file = "ch9/model_outputs/churn_model.sav"
model = joblib.load() #3
fake_config_file = "this_file_doesnt_exist.json" #4
missing_key_config = { #5
"model_filename": "ch9/model_outputs/churn_model.sav",
"id_field": "customer_id",
"input_database": "data/customers.db",
"output_database": "data/customers.db",
"output_table": "customer_churn_predictions",
"input_features": [
"total_day_minutes",
"total_day_calls",
"number_customer_service_calls"]
}
config_with_fake_input_file = { #6
"model_filename": "ch9/model_outputs/churn_model.sav",
"input_filename": "this_input_file_doesnt_exist.csv",
"id_field": "customer_id",
"input_database": "data/customers.db",
"output_database": "data/customers.db",
"output_table": "customer_churn_predictions",
"input_features": [
"total_day_minutes",
"total_day_calls",
"number_customer_service_calls"]
}
config_with_fake_model_file = { #7
"model_filename": "this_model_file_doesnt_exist.sav",
"input_filename": "data/customer_sample.csv",
"id_field": "customer_id",
"input_database": "data/customers.db",
"output_database": "data/customers.db",
"output_table": "customer_churn_predictions",
"input_features": [
"total_day_minutes",
"total_day_calls",
"number_customer_service_calls"]
}
def get_model_pred(
self, inputs: dict, model) -> float: #8
frame = pd.DataFrame.\
from_dict(inputs,
orient = "index").\
transpose()
return round(model.\
clf.\
predict_proba(frame)[0][1], 5)
Inspect a TestCase class that collects shared model and configuration fixtures before its individual pipeline tests are defined. section 10.2

