Sub topic 2
Laying out a repository for testing and deployment
This chapter assumes you know basic Git, pytest, and YAML, and that you have run Databricks jobs and notebooks. It covers where files go. Later chapters fill in the contents.
Why layout matters
Databricks projects often start as a folder of notebooks. A notebook can't be imported by a test, and it mixes logic with reads and writes. A deployment tool also needs to find your job definitions in a predictable place. A good layout solves both problems. Logic goes in a Python package, tests sit beside it, and deployment files sit at the root.
A working layout
orders_pipeline/
├── databricks.yml
├── pyproject.toml
├── requirements-dev.txt
├── .github/
│ └── workflows/
│ └── ci.yml
├── resources/
│ └── orders_job.yml
├── src/
│ └── my_project/
│ ├── __init__.py
│ ├── transforms.py
│ ├── io.py
│ └── main.py
├── notebooks/
│ └── explore_orders.py
└── tests/
├── unit/
│ └── test_transforms.py
└── integration/
└── test_orders_job.pyEach top-level item has one purpose:
| Path | Purpose |
|---|---|
| `src/my_project/` | Importable package with all logic. |
| `tests/unit/` | Fast tests that need no workspace. |
| `tests/integration/` | Slower tests that run against a workspace. |
| `resources/` | Job and pipeline definitions, one file per resource. |
| `databricks.yml` | Bundle root: name, targets, and the `include` list. |
| `.github/workflows/` | CI and CD definitions. |
| `notebooks/` | Exploration and thin entry points only. |
How the pieces connect
The package is the center. Tests import it, jobs run it, and notebooks call it.
Use the src layout
Place the package under src/ instead of at the repository root. With a root-level package, pytest can import your working copy by accident, even when the installed package is broken. The src/ layout forces tests to import the package the same way a job does. Tell pytest where to find it in pyproject.toml:
[tool.pytest.ini_options]
pythonpath = ["src"]
testpaths = ["tests"]
markers = [
"integration: tests that need a Databricks workspace",
]Registering the integration marker lets you run pytest tests/unit in CI on every pull request. You can select the slower tests separately with pytest -m integration.
Keep notebooks thin
Notebooks can stay in the repository, but treat them as callers. A notebook or a main.py entry point should parse its parameters, call a function from the package, and stop. If a notebook holds a join or a filter, move that code into transforms.py. The next chapter shows how.
One resource per file
The include entry in databricks.yml (resources/*.yml) pulls in every file in resources/. Keep one job or pipeline per file. Reviews of changes to a single job then stay small, and a merge conflict stays local to that job. Keep environment differences in the targets section of databricks.yml, not in copies of the job files.
Common mistakes
- Putting
tests/insidesrc/. The test code then ships with the deployed package. - Letting
io.pyimport fromtransforms.pyand the reverse. Transforms should never import I/O code. - Committing credentials or workspace tokens. CI reads them from secrets, as a later chapter shows.
- Keeping one
requirements.txtfor everything. Separate runtime dependencies from development tools such as pytest, so the deployed job does not install the test tools.
Exercise
A teammate keeps a join in notebooks/explore_orders.py and wants to unit test it. Which path should the join move to, and why does the layout make it testable?
Show answer
Move it to src/my_project/transforms.py as a function. Tests under tests/unit/ import the my_project package through the pythonpath = ["src"] setting, and a notebook cannot be imported that way. The notebook then calls the same function.