How do you unit test a feature-engineering function used in Databricks notebooks?
- Exam domain
- MLOps44% of the exam
- Difficulty
- Medium
- In this mock
- 22 questions on this domainPractise this domain
Why the exam asks this
Objective in the exam guide: Implement unit tests for individual functions in Databricks notebooks to ensure they produce expected outputs when given specific inputs.
Unit tests for notebook functions are named.
Why D is the answer
Move it into a Python module and write pytest tests with small, known inputs and expected outputs
Unit tests check one function with controlled inputs. Functions in importable modules are easy to test with pytest locally or in CI.
Remember: Unit test functions with known inputs.
How it works
Unit tests isolate functions with controlled data.
In detail
- Small known inputs.
- Expected outputs asserted.
Every option, checked
Knowing why each wrong option fails is how you rule it out on exam day.
A. Run the whole training pipeline on production data and check that the final metric looks reasonableWrongMost tempting
That is an end-to-end check, not a unit test.
Why it tempts: It tests a lot at once, but not units.
B. Visually inspect a sample of rows after each changeWrong
Eyeballing is not repeatable.
C. Test it only in productionWrong
Tests belong before prod.
D. Move it into a Python module and write pytest tests with small, known inputs and expected outputsRight
Unit test functions with known inputs.
Use case
An illustrative scenario, not a real incident.
Illustrative: a test asserts that add_tenure_bucket maps 13 months to "1-2 years".
See it in different ways
flowchart LR S0["Extract function to module"] --> S1["Write pytest with fixtures"] --> S2["Assert outputs"] --> S3["Run in CI"]
How to read this chart
- Read it in order: Extract function to module, then Write pytest with fixtures, then Assert outputs, then Run in CI.
sequenceDiagram participant P0 as Pull request participant P1 as CI participant P2 as pytest P0->>P1: trigger P1->>P2: run tests P2-->>P1: pass or fail
How to read this chart
- 1. Pull request sends "trigger" to CI.
- 2. CI sends "run tests" to pytest.
- 3. CI gets a reply from pytest: "pass or fail".