Sub topic 1
What to test in data and Spark code
Prerequisites
This chapter assumes you know basic Python and pytest, you can work with PySpark DataFrames, and you have used Databricks notebooks and jobs. You do not need any CI/CD experience. Later chapters build the tooling. This one decides what that tooling should check.
Why data code needs a strategy
Data code fails in ways that ordinary application code does not. The logic can be correct while the data is wrong, late, or shaped differently than you expected. A job can succeed and still write bad rows. A strategy separates two questions: "Is my code correct?" and "Is my data correct?" Each question needs different tests that run in different places.
The kinds of tests
There are four kinds of tests worth having.
- Unit tests check one transformation on a few rows of in-memory data. They have no reads or writes and need only a SparkSession. They are fast and run on every commit.
- Integration tests run code against a real workspace, using real tables, a real cluster or serverless compute, and a real job. They catch problems unit tests cannot, such as missing permissions, wrong table names, and schema drift.
- Data quality checks test the data itself while the pipeline runs. Examples are "order_id is never null" and "total is not negative". They guard against bad input that no code test could predict.
- Deployment checks confirm that the job definitions and configuration are valid before anything ships.
The diagram shows how the tests form a ladder. Each rung is slower and costs more than the one below, so you want many tests at the bottom and few at the top.
What to put in each layer
Choose the layer by asking what could break.
| What could break | Best test | Where it runs |
|---|---|---|
| A calculation, filter, or join condition | Unit test | Local machine or CI runner |
| Handling of nulls, duplicates, or empty input | Unit test | Local machine or CI runner |
| A table name, schema, or permission | Integration test | Workspace |
| A job's task order or cluster settings | Integration test | Workspace |
| Unexpected values in incoming data | Data quality check | Inside the pipeline |
| A malformed bundle or job definition | Deployment check | CI runner |
Notice that most business logic lands in unit tests. That is the goal. If a rule can be checked on five rows, check it there rather than on a full table.
What not to test
Do not test Spark itself. A test that confirms groupBy groups rows adds no value, because the Spark project already tests it. Test your own logic: the expressions, the conditions, and the choices you made.
Avoid asserting on row order unless your code sorts explicitly. Spark does not guarantee order, so such tests fail at random. Avoid tests that depend on today's date or on a shared production table. These fail without any code change.
Where each test runs
Unit tests run wherever Python runs. They use a local SparkSession, so a laptop or a CI runner is enough. Integration tests need a workspace. You can reach one from your machine with Databricks Connect, or you can deploy and run a job. Data quality checks run inside the pipeline itself, on every run, against live data.
This split sets the pace of your workflow. You run unit tests constantly while editing. You run integration tests before merging or after deploying to staging. Quality checks run on every production run.
A first look at a testable rule
Consider the rule "an order's total is quantity times unit price". It is a pure calculation, so it belongs in a unit test. The rule "the orders table exists and the job can read it" depends on the workspace, so it belongs in an integration test. The rule "no order has a negative quantity" depends on incoming data, so it belongs in a quality check. One feature can need all three.
Exercise
A job reads main.sales.orders, removes rows where status is "cancelled", and writes the result. For each of these three risks, name the test layer that fits best: (1) the filter keeps cancelled rows by mistake, (2) the service principal lacks permission to read main.sales.orders, (3) a new upstream release starts sending status values in uppercase.
Show answer
(1) A unit test, because the filter can be checked on a few in-memory rows with no workspace. (2) An integration test, because permissions only exist in a real workspace. (3) A data quality check, because it concerns incoming data that code tests cannot predict. For example, check that status is in an allowed set of values.