Pytester
Python Testing for Databricks
Install / Use
/learn @databrickslabs/PytesterREADME
Python Testing for Databricks
<!-- TOC -->- Python Testing for Databricks
- Installation
- Ecosystem
- PyTest Fixtures
- Logging
debug_env_namefixturedebug_envfixtureenv_or_skipfixturewsfixturemake_run_asfixtureaccfixturesparkfixturesql_backendfixturesql_execfixturesql_fetch_allfixturemake_randomfixturemake_instance_poolfixturemake_instance_pool_permissionsfixturemake_jobfixturemake_job_permissionsfixturemake_clusterfixturemake_cluster_permissionsfixturemake_cluster_policyfixturemake_cluster_policy_permissionsfixturemake_pipelinefixturemake_warehousefixturemake_groupfixturemake_acc_groupfixturemake_userfixturemake_pipeline_permissionsfixturemake_notebookfixturemake_notebook_permissionsfixturemake_workspace_filefixturemake_directoryfixturemake_directory_permissionsfixturemake_repofixturemake_repo_permissionsfixturemake_workspace_file_permissionsfixturemake_workspace_file_path_permissionsfixturemake_secret_scopefixturemake_secret_scope_aclfixturemake_authorization_permissionsfixturemake_udffixturemake_catalogfixturemake_schemafixturemake_tablefixturemake_storage_credentialfixturemake_volumefixtureproduct_infofixturemake_modelfixturemake_experimentfixturemake_experiment_permissionsfixturemake_warehouse_permissionsfixturemake_lakeview_dashboard_permissionsfixturelog_workspace_linkfixturelog_account_linkfixturemake_dashboard_permissionsfixturemake_alert_permissionsfixturemake_queryfixturemake_query_permissionsfixturemake_registered_model_permissionsfixturemake_serving_endpointfixturemake_serving_endpoint_permissionsfixturemake_feature_tablefixturemake_feature_table_permissionsfixturewatchdog_remove_afterfixturewatchdog_purge_suffixfixtureis_in_debugfixture
- Project Support
Installation
Add a databricks-labs-pytester dependency to your pyproject.toml file (or legacy requirements.txt file). You can
also install it directly from the command line:
pip install databricks-labs-pytester
If you use hatch as a build system, make sure to add databricks-labs-pytester as
a test-time dependency and not as a compile-time dependency, otherwise your wheels will
transitively depend on pytest, which is not usually something you need.
[project]
name = "name-of-your-project"
# ...
dependencies = [
"databricks-sdk~=0.30",
# ... dependencies required for your code to execute
]
[tool.hatch.envs.default]
dependencies = [
# ... dependencies required to test/validate/format your code:
"black~=24.3.0",
"coverage[toml]~=7.4.4",
"mypy~=1.9.0",
"pylint~=3.2.2",
"pylint-pytest==2.0.0a0",
"databricks-labs-pylint~=0.4.0",
"databricks-labs-pytester~=0.2", # <= this library
"pytest~=8.3.3",
"pytest-cov~=4.1.0",
"pytest-mock~=3.14.0",
"pytest-timeout~=2.3.1",
"pytest-xdist~=3.5.0",
"python-lsp-server>=1.9.0",
"ruff~=0.3.4",
"types-PyYAML~=6.0.12",
"types-requests~=2.31.0",
]
Ecosystem
Built on top of Databricks SDK for Python, this library is part of the Databricks Labs Python ecosystem, which includes the following projects:
- PyLint Plugin for Databricks for static code analysis and early bug detection.
- Blueprint for Python-native pathlib.Path-like interfaces, Managing Python App installations within Databricks Workspaces, Application Migrations, and Building Wheels.
- LSQL for lightweight SQL handling and dashboards-as-code.
- UCX for automated migrations into Unity Catalog and LSP plugin for static code analysis for UC compatibility.
See this video for a quick overview of the Databricks Labs Python ecosystem.
PyTest Fixtures
PyTest Fixtures are a powerful way to manage test setup and teardown in Python. This library provides a set of fixtures to help you write integration tests for Databricks. These fixtures were incubated within the Unity Catalog Automated Migrations project for more than a year and are now available for other projects to simplify integration testing with Databricks.
Logging
This library is built on years of debugging integration tests for Databricks and its ecosystem.
That's why it comes with a built-in logger that traces creation and deletion of dummy entities through links in the Databricks Workspace UI. If you run the following code:
def test_new_user(make_user, ws):
new_user = make_user()
home_dir = ws.workspace.get_status(f"/Users/{new_user.user_name}")
assert home_dir.object_type == ObjectType.DIRECTORY
You will see the following output, where the first line is clickable and will take you to the user's profile in the Databricks Workspace UI:
12:30:53 INFO [d.l.p.fixtures.baseline] Created dummy-xwuq-...@example.com: https://.....azuredatabricks.net/#settings/workspace/identity-and-access/users/735...
12:30:53 DEBUG [d.l.p.fixtures.baseline] added workspace user fixture: User(active=True, display_name='dummy-xwuq-...@example.com', ...)
12:30:58 DEBUG [d.l.p.fixtures.baseline] clearing 1 workspace user fixtures
12:30:58 DEBUG [d.l.p.fixtures.baseline] removing workspace user fixture: User(active=True, display_name='dummy-xwuq-...@example.com', ...)
You may need to add the following to your conftest.py file to enable this:
import logging
from databricks.labs.blueprint.logger import install_logger
install_logger()
logging.getLogger('databricks.labs.pytester').setLevel(logging.DEBUG)
<!-- FIXTURES -->
debug_env_name fixture
Specify the name of the debug environment. By default, it is set to .env,
which will try to find a file named .env
in any of the parent directories of the current working directory and load
the environment variables from it via the debug_env fixture.
Alternatively, if you are concerned of the
risk of .env files getting checked into version control,
we recommend using the ~/.databricks/debug-env.json file to store different sets o
Related Skills
node-connect
352.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
352.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
352.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
