Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Data Engineer Professional

Python projects and tests

Databricks Data Engineer Professional · Lesson 3 of 21 · 17:21

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 Data Engineer Professional exam. In this lesson: untested notebooks versus a real Python project for Declarative Automation Bundles: the project structure, building a wheel, unit tests with pytest, and integration tests that run on Databricks. Includes three original practice questions.

Study notes

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

Untested notebooks

Quayside Eats keeps its transformation code in notebooks that call each other, with no tests, and a change to the fee logic last month silently doubled some delivery fees. The team wants a Python project that it can test on a laptop, build into a wheel, and deploy with a bundle. This lesson covers the project structure of a bundle, building a wheel, unit tests with the PySpark testing functions, and integration tests.

Project structure

The command databricks bundle init, with the default Python template, creates a project that scales. At the root, databricks.yml names the bundle, includes the resource files, declares the artifacts to build, defines catalog and schema variables, and lists the targets. The resources folder holds one YAML file per job or pipeline, the src folder holds the Python package with the transformation code, and the tests folder holds the unit tests. A pyproject.toml file declares the package and its dependencies, and the template uses uv to install them and to run the tests. Because the code sits in a package instead of notebooks, every job, pipeline, and test imports the same functions.

Building a wheel

To ship the package, the bundle builds a wheel. The artifacts mapping in databricks.yml, with the type whl, a build command, and a path, tells bundle deploy to build the wheel and to upload it to the workspace. A job then runs it with a Python wheel task, which names the package and its entry point, and lists the wheel in its libraries or in its environment. Without the artifacts mapping, the bundle deploys a wheel that was already built, for example by the CI pipeline. Either way, the wheel comes from the same commit that is deployed, so the code that runs is the code that was tested.

Unit tests

Unit tests check one function on small, known data. Write each transformation as a function that takes a DataFrame and returns a DataFrame, in the package, so a test can import it, and chain the functions with the transform method of a DataFrame. A test with pytest builds a tiny input DataFrame, calls the function, and compares the result with an expected DataFrame. assertDataFrameEqual compares the rows and the schemas of two DataFrames, ignores the row order by default, and accepts a tolerance for floating point numbers. assertSchemaEqual compares only the schemas, which catches a renamed column or a changed type. You can run the tests from an IDE with uv, or in the workspace, from a notebook that calls pytest.

Integration tests

Unit tests prove each function, while integration tests prove that the deployed job or pipeline works end to end. The usual pattern deploys the bundle to a test target, with bundle deploy and dash t test, runs the job with bundle run, and checks the tables that it writes from known test data. In CI/CD, the pipeline authenticates as a service principal, runs the unit tests first, then the integration tests, and only then deploys to production. Test targets should use their own catalog or schema, set through bundle variables, so a test run never touches production tables. And a failed check should fail the run, so a broken change stops before it reaches production.

Recap

Let's recap. A bundle project has databricks.yml at the root, resources for jobs and pipelines, a Python package in src, and unit tests in tests. The artifacts mapping builds the wheel at deployment, and a Python wheel task runs its entry point. Transformations take and return DataFrames, so tests can import them, and the transform method chains them. assertDataFrameEqual compares rows and schemas in any row order, and assertSchemaEqual compares schemas only. And integration tests deploy the bundle to a test target, run the job on test data, and check the results before production.

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. The fee logic is copied into five notebooks, and every fix must be made five times. The team wants one tested implementation that its jobs and pipelines share. What should the data engineer do?

    • Run one notebook from the others with %run
    • Store the logic in a volume as a text file
    • Copy the functions into each job’s YAML file
    • Move the logic into a package in src, with tests
    Show the answer

    Move the logic into a package in src, with tests

    Functions in a package under src are written once, imported by every job and pipeline, and tested in the tests folder. “Run one notebook from the others with %run” would still keep the logic in a notebook, which is harder to test and to version as a package. “Store the logic in a volume as a text file” would store code in a file that nothing imports or tests. And “Copy the functions into each job’s YAML file” is not possible, since job files describe resources, not Python code.

  2. A unit test compares a transformation’s output with an expected DataFrame, and it fails at random, although the rows are always the same. Only their order changes from run to run. What should the test use?

    • A larger tolerance for floating point values
    • assertDataFrameEqual with its defaults
    • assertSchemaEqual on the two DataFrames
    • collect() on both, compared as lists
    Show the answer

    assertDataFrameEqual with its defaults

    AssertDataFrameEqual ignores the row order by default, so the same rows in another order still pass. “A larger tolerance for floating point values” would only relax the comparison of numbers, and the order would still differ. “assertSchemaEqual on the two DataFrames” would compare only the schemas, and miss wrong values. And “collect() on both, compared as lists” would compare lists in their collected order, which fails for the same reason.

  3. Integration tests run the deployed job against test data, but last week they overwrote a production table. How should the team prevent this?

    • Run integration tests only on a laptop
    • Skip integration tests, keep the unit tests
    • Give the test target its own catalog
    • Deploy to production first, then test there
    Show the answer

    Give the test target its own catalog

    A test target with its own catalog, set through a bundle variable, keeps every test write away from production tables. “Run integration tests only on a laptop” cannot run a deployed job, so it would not be an integration test at all. “Skip integration tests, keep the unit tests” would leave the deployed job untested from end to end. And “Deploy to production first, then test there” would test changes on production data, which is the risk itself.