liveBook · Manning

Unittest

As explained in Software Engineering for Data Scientists

unittest is Python's standard-library unit testing framework, including the TestCase class for organizing test methods and assertions.

unittest is Python’s built-in framework for automating unit tests: checks that a small component, usually a function, produces the expected result. It is useful when moving a data science workflow from a local prototype to a remote or production setting, where a known model prediction can be checked again before relying on it. section 10.1.1 section 10.6

Tests are organized in a class derived from unittest.TestCase. Put test methods in that class, prepare inputs and expected outputs, and let the test report whether each check succeeds or fails. This structure turns repeatable manual checks into a collection that can be run whenever code changes. section 10.2 section 10.6

Use unittest to make small, focused checks that are easier to diagnose when they fail:

  • Test functions with normal inputs as well as edge cases that could otherwise break unexpectedly. section 10.2
  • Split long logic into smaller functions or test separate points in it, so a failed test better pinpoints the problem. section 10.2
  • Anticipate operational failures such as incorrect model inputs, database network errors, missing configuration, or API timeouts. section 10.2
  • Keep unit tests distinct from integration tests, which check cooperating pipeline components, and regression tests, which watch for performance degradation after rollout. section 10.1.1

Checking a churn-model prediction

A TestCase can load a saved customer-churn model, convert one input dictionary into a DataFrame, calculate a probability, round it, and compare it with an expected prediction. The same approach can read several test rows from a file and compare each stored expected prediction. A passing run reports OK; a feature-name mismatch produces a failed test with the missing and unexpected names, making the pipeline defect visible. section 10.2 section 10.2

Listing 10.1 This script (available in the ch10/test_model_outputs.py file) will test the model prediction of a single set of inputs against an expected value. If the prediction is correct, Python will print an "OK" message. If not, it will return a message stating that the test failed.
import unittest #1
import joblib
import pandas as pd
import sys
sys.path.append(".")

class TestModelOutputs(unittest.TestCase): #2

    model_file = "ch9/model_outputs/churn_model.sav"
    model = joblib.load(model_file) #3

    def get_model_pred(self,  #4
        inputs: dict,
        model) -> float:

        frame = pd.DataFrame.from_dict( #4
            inputs,
            orient = "index").transpose()

        return round(model.\ #4
               clf.\
               predict_proba(frame)[0][1], 5)

    def test_1(self): #5

        inputs = {'total_day_minutes': 15, #6
            'total_day_calls': 1,
            'num_customer_service_calls': 0}

        self.assertEqual(self.\ #7
            get_model_pred(inputs,
            TestModelOutputs.model),
            0.01994)


if __name__ == '__main__': #8
    unittest.main()

Inspect a complete TestCase example that loads a churn model and tests one prediction against an expected value. section 10.2

unittest also supports tests around pipeline safeguards. For example, tests can verify that a command-line configuration argument was supplied, that its file exists, and that required configuration keys are present before the pipeline reads data, loads a model, generates predictions, and uploads results. Running the tester script from a terminal executes the collected checks together. section 10.2 section 10.2

Writing these checks alongside code supports maintainability: failures identify where behavior changed rather than leaving a broken model workflow to be discovered later. After covering a model API workflow, the same pattern can be extended by writing several unittest tests for its requests and expected behavior. section 10.2 section 10.5

How Software Engineering for Data Scientists covers it

How deep it goes
Explains TestCase-based test organization and practical normal, edge-case, model-output, and pipeline-validation checks.
Examples
Uses customer-churn predictions, CSV test inputs, missing configuration keys, nonexistent files, and a feature-name mismatch failure.
What it assumes
Assumes Python code and functions, while introducing unittest through unit-test examples rather than advanced testing theory.
What you can do after
Apply it via unittest to create TestCase classes that validate model outputs and pipeline error conditions.
How it connects
Connects unit testing with assertions, integration testing, regression testing, exception handling, PyTest, and code coverage.

Also explained in

Compare how 5 books explain unittest →

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.