liveBook · Manning

TestCase

As explained in Software Engineering for Data Scientists

TestCase is the unittest base class that provides structure and assertion methods for defining a group of related tests.

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

How Software Engineering for Data Scientists covers it

How deep it goes
Demonstrates subclassing, shared test data, assertions, expected exceptions, and terminal results without detailing TestCase internals.
Examples
Uses a customer churn model, test CSV rows, missing configuration keys, and a nonexistent config file.
What it assumes
Assumes Python code and functions while introducing unittest test classes from examples.
What you can do after
Apply it via unittest to group checks for predictions and expected pipeline errors.
How it connects
Connects unittest with unit tests, exception handling, model predictions, configuration validation, and debugging.

Also explained in

Compare how 2 books explain TestCase →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Engineering for Data Scientists. The section links point to the parts of the book it draws on.