Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Machine Learning Professional

Unit tests and how to organize them

Databricks Machine Learning Professional · Lesson 14 of 22 · 1:41:45

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Machine Learning Professional exam. In this lesson: what a unit test checks, writing tests with pytest, running tests from a notebook, and organizing functions and tests in files. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

Zero days

Last month, a change to Pebblecove's feature code turned a missing check-in date into zero days before check-in, and the cancellation model quietly got worse for two weeks. A small test, run before the merge, would have caught it in seconds. This lesson covers unit tests for the functions in your notebooks, and the three ways to organize functions and their tests.

What a unit test checks

A unit test checks one function in isolation: given specific inputs, it compares the output with the expected output. It is small and fast, so it runs on every change, and a failure points straight at the broken function. Good candidates are the functions of feature engineering and preprocessing, like the one that computes the days before check-in, tested on normal values, edge cases, and missing values. And unit tests should not run against production data: test on small inputs that you build yourself, or on views, especially for functions that change data.

Writing tests with pytest

pytest is the most common framework for Python unit tests. You write test functions whose names start with test, in files named with test, and each one calls the function with known inputs. An assert statement compares the result with the expected value, and pytest reports every assert that fails, with the actual and the expected values. To test a function that takes a Spark DataFrame, build a tiny DataFrame in the test, call the function, and compare the collected rows with the expected rows. The Python unittest module works too, with test classes and assert methods.

Running tests from a notebook

With the functions and the tests in workspace files, a notebook can run them all: it calls pytest dot main, with the folder, the verbose flag, and an option that turns off its cache. pytest dot main returns a code, and an assert that this code equals zero makes the notebook, and the job that runs it, fail when any test fails. Notebooks can also import functions from Python files in the workspace, like any module, which replaces the older percent run command. And a widget can switch one notebook between a test mode and a normal mode.

Organizing functions and tests

There are three common ways to organize functions and their unit tests. Storing them outside of notebooks, in Python files, lets you call the functions from notebooks and from anywhere else, and test frameworks work best there, but it adds files to track. Storing the functions in one notebook and the tests in a separate notebook makes the functions easy to reuse across notebooks, but they cannot be used outside of notebooks, and the number of notebooks grows. Storing functions and tests in the same notebook is the easiest to track, but the functions are harder to reuse, and cannot be used outside of notebooks. For Python, Databricks recommends the first approach: functions and tests outside of notebooks.

Recap

Let's recap. A unit test checks one function, given specific inputs, against the expected output. pytest finds the test functions, and assert compares the result with the expected value. A notebook can run pytest dot main, and must assert that its return code is zero. Functions and tests outside of notebooks can be reused anywhere, which Databricks recommends for Python. And unit tests should not run against production data.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. Which is the best candidate for a unit test?

    • The full nightly training job
    • The response time of the serving endpoint
    • A function that computes days before check-in
    • The accuracy of the model on next month's data
    Show the answer

    A function that computes days before check-in

    A function with clear inputs and an expected output is exactly what a unit test checks. “The full nightly training job” would run many pipelines together, which is an integration test. “The response time of the serving endpoint” would measure a deployed endpoint, which is a load test. And “The accuracy of the model on next month's data” is only measurable after deployment, by monitoring.

  2. A notebook task runs pytest.main on the tests folder. Two tests fail, but the job still shows success. What is missing?

    • An assert that the return code is zero
    • The verbose flag
    • A widget set to Test mode
    • A %run of the tests notebook
    Show the answer

    An assert that the return code is zero

    Pytest dot main returns a code instead of raising an error, so the notebook must assert that the code is zero, to fail the job. “The verbose flag” only makes the output more detailed. “A widget set to Test mode” only selects a mode in one notebook. And “A %run of the tests notebook” would include another notebook, and checks nothing.

  3. Feature functions must be imported by notebooks, jobs, and a command line tool, and tested in continuous integration. Which organization fits?

    • Functions and tests in the same notebook
    • Functions in one notebook, tests in another
    • Functions in a notebook, included with %run
    • Functions and tests in Python files, outside notebooks
    Show the answer

    Functions and tests in Python files, outside notebooks

    Functions in Python files can be imported anywhere, notebooks included, and test frameworks run best outside of notebooks. “Functions and tests in the same notebook” would keep the functions inside one notebook, which other tools cannot import. “Functions in one notebook, tests in another” would make the functions reusable between notebooks only. And “Functions in a notebook, included with %run” would still keep the functions inside a notebook, out of reach of the command line tool.