Sub topic 7
Integration tests against real tables and jobs
This chapter assumes you can write the pytest unit tests and the spark fixture from the earlier chapters. It also assumes you have a Databricks Connect environment that can reach a dev workspace.
What unit tests cannot tell you
Unit tests run a transformation on small in-memory DataFrames. They do not show whether the code works with real Delta tables, Unity Catalog permissions, or a job's configuration. An integration test closes that gap by running against a real workspace.
Integration tests are slower and cost compute. Keep them few, and aim them at the seams that unit tests skip:
- Reading from and writing to Unity Catalog tables.
- Schema and permission mismatches between your code and real data.
- A deployed job running end to end.
Choosing where the tests run
| Approach | What runs where | Best for |
|---|---|---|
| Databricks Connect | Your pytest process runs locally. Spark work runs on remote compute. | Testing functions against real tables |
| Jobs API through the SDK | The whole job runs in the workspace. | Testing a deployed job and its configuration |
Testing against real tables
Give each test run its own schema. Parallel CI runs then cannot overwrite each other's data, and cleanup is one DROP SCHEMA. The fixture below uses the spark fixture from the earlier chapter, with USE_DATABRICKS_CONNECT=1 set. It assumes a dev_catalog catalog where your identity can create schemas.
import uuid
import pytest
from my_project.transforms import add_order_total
@pytest.fixture
def test_schema(spark):
name = f"dev_catalog.it_{uuid.uuid4().hex[:8]}"
spark.sql(f"CREATE SCHEMA {name}")
yield name
spark.sql(f"DROP SCHEMA IF EXISTS {name} CASCADE")
def test_total_survives_a_delta_round_trip(spark, test_schema):
spark.createDataFrame(
[(1, 2, 5.0), (2, 3, 1.5)],
["order_id", "quantity", "unit_price"],
).write.saveAsTable(f"{test_schema}.orders")
result = add_order_total(spark.table(f"{test_schema}.orders"))
assert result.count() == 2
assert result.filter("total IS NULL").count() == 0The yield makes the cleanup run even when the test fails.
Testing a deployed job
A job test triggers a run and checks how it ended. It uses the Databricks SDK, which reads credentials from the same environment variables you set for CI. The job must already be deployed, for example with databricks bundle deploy -t staging. Pass its ID through an environment variable.
import os
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.jobs import RunResultState
def test_orders_job_succeeds():
w = WorkspaceClient()
job_id = int(os.environ["ORDERS_JOB_ID"])
run = w.jobs.run_now(job_id=job_id).result()
assert run.state.result_state == RunResultState.SUCCESSrun_now returns a waiter, and .result() blocks until the run reaches a terminal state. After this check passes, run assertions against the output tables with the spark fixture.
Common errors
- Leftover schemas. If a CI runner is killed mid-test, the fixture cleanup never runs. Prefix test schemas with
it_so a scheduled cleanup job can find and drop old ones. - Both
pysparkanddatabricks-connectinstalled. They conflict. Use a separate environment for each mode. - Slow feedback. Run unit tests on every push. Run integration tests on pull requests to main, or after a staging deploy.
- Stale deployed code. A job test checks whatever is deployed, not your working copy. Deploy first, then test.
Exercise
Your integration test writes to a table named dev_catalog.test.orders. Two pull requests run CI at the same time, and tests fail at random. What is the likely cause, and how do you fix it?
Show answer
Both runs share one table, so they overwrite or delete each other's data. Create a unique schema per run, for example with a uuid suffix, as the test_schema fixture does. Drop it with DROP SCHEMA ... CASCADE after the test.