Skip to main content

Serverless-first testing framework for Databricks notebooks with Asset Bundle support

Project description

Databricks Notebook Test Framework

A Python-based automated testing framework for Databricks notebooks with native serverless support and Databricks Asset Bundle integration.

Features

  • Serverless-first - Automatic inline environment management for dependencies
  • Databricks Asset Bundles - Auto-detects bundle projects and resolves workspace paths
  • ✅ Simple, intuitive test pattern with setup/test/cleanup lifecycle
  • ✅ Execute tests remotely on Databricks (serverless or cluster)
  • Parallel test execution for faster test runs
  • ✅ Clean developer workflow for writing tests
  • ✅ JUnit XML results compatible with CI/CD pipelines
  • ✅ Parameterized testing support
  • ✅ Automatic test discovery (pytest-style patterns)
  • ✅ CLI-driven with rich output
  • ✅ Run multiple test classes in a single notebook
  • ✅ Zero external test framework dependencies

Installation

# Install from source
pip install -e .

Or from PyPI (once published):

pip install dbx_test

Quick Start

1. Create a Test Notebook

Create a test notebook (e.g., tests/my_notebook_test.py):

from dbx_test import NotebookTestFixture

class TestMyNotebook(NotebookTestFixture):
    def run_setup(self):
        """Setup code runs before tests"""
        self.data = spark.createDataFrame([(1, "a"), (2, "b")], ["id", "value"])
        self.data.createOrReplaceTempView("test_data")
    
    def test_row_count(self):
        """Test that we have expected row count"""
        result = spark.sql("SELECT * FROM test_data")
        assert result.count() == 2, "Expected 2 rows"
    
    def test_schema(self):
        """Test that schema is correct"""
        result = spark.sql("SELECT * FROM test_data")
        assert "id" in result.columns
        assert "value" in result.columns
    
    def run_cleanup(self):
        """Cleanup runs after all tests"""
        spark.sql("DROP VIEW IF EXISTS test_data")

2. Scaffold Your Project

# Creates test file + config automatically
dbx_test scaffold my_feature

# For bundle projects, this detects the bundle and provides bundle-specific tips

3. Configure Your Environment

Create config/test_config.yml:

workspace:
  # Use Databricks CLI profile
  profile: "default"
  
cluster:
  # Option 1: Use serverless (recommended, fastest)
  # Leave empty for serverless with inline dependencies
  
  # Install dependencies automatically
  libraries:
    - whl: "git+https://github.com/your-org/your-package.git"
    - pypi:
        package: "pandas==2.0.0"
  
  # Option 2: Use pre-created environment (serverless)
  # environment_key: "my_environment"
  
  # Option 3: Use existing cluster
  # cluster_id: "1234-567890-abcdef"
  
execution:
  timeout: 600
  parallel: false
  
reporting:
  output_dir: ".dbx-test-results"
  formats: ["junit", "console"]

4. Run Tests

For Databricks Asset Bundle Projects:

# Auto-detects bundle and resolves workspace path
dbx_test run --target dev --profile my-profile

# With custom subdirectory
dbx_test run --target dev --tests-dir src/tests --profile my-profile

For Non-Bundle Projects:

# Run tests from workspace path
dbx_test run --tests-dir /Workspace/Users/you@company.com/tests --profile my-profile

# Or from Repos
dbx_test run --tests-dir /Repos/production/my-project/tests --profile my-profile

Test Discovery: Automatically finds all notebooks matching test_* or *_test patterns (just like pytest!)

Databricks Asset Bundle Support

The framework automatically detects Databricks Asset Bundle projects and simplifies test execution:

Example Bundle Structure:

my_bundle/
├── databricks.yml
├── src/
│   └── my_code.py
└── tests/
    ├── test_feature_a.py
    └── test_feature_b.py

Example databricks.yml:

bundle:
  name: my_project

targets:
  dev:
    workspace:
      host: https://your-workspace.cloud.databricks.com/

Run Tests:

# Framework auto-detects the bundle and constructs the workspace path
dbx_test run --target dev --profile my-profile

# Resolves to: /Workspace/Users/you@company.com/.bundle/my_project/dev/files/tests

Benefits:

  • ✅ No manual workspace path configuration
  • ✅ Works seamlessly with databricks bundle deploy
  • ✅ Automatic path resolution based on target
  • ✅ Supports custom test directories

Serverless Compute with Inline Dependencies

The framework automatically creates inline environments for serverless compute:

cluster:
  # Dependencies are automatically installed in serverless environment
  libraries:
    - whl: "git+https://github.com/your-org/your-package.git"
    - pypi:
        package: "pandas==2.0.0"
    - whl: "/Workspace/Shared/wheels/custom-1.0.0-py3-none-any.whl"

How it works:

  1. Framework detects serverless compute (no cluster_id specified)
  2. Creates inline environment with your dependencies
  3. Executes tests with all libraries installed
  4. Cleans up automatically

For production, you can pre-create environments:

cluster:
  environment_key: "production_test_env"  # Reference pre-created environment

See Serverless Environments Guide for details.

Interactive Notebook Development

# Run tests directly in a Databricks notebook
from dbx_test import NotebookTestFixture, run_notebook_tests
import json

class TestMyData(NotebookTestFixture):
    def run_setup(self):
        self.df = spark.createDataFrame([(1, "Alice")], ["id", "name"])
    
    def test_count(self):
        assert self.df.count() == 1

# Run tests (automatically discovers all test classes)
results = run_notebook_tests()

# Return results to CLI (required for remote execution)
dbutils.notebook.exit(json.dumps(results))

📘 See Notebook Usage Guide for detailed examples and patterns.

CLI Commands

dbx_test run

Execute tests remotely on Databricks.

Options:

  • --target TARGET - Databricks Asset Bundle target (auto-detects workspace path)
  • --profile PROFILE - Databricks CLI profile to use
  • --tests-dir DIR - Directory containing tests (workspace path or relative for bundles)
  • --env ENV - Environment (dev/test/prod)
  • --parallel - Enable parallel execution
  • --output-format FORMAT - Output format (junit/console/json/html)
  • --config PATH - Path to config file (default: config/test_config.yml)
  • --verbose - Enable verbose output

Examples:

# Bundle project
dbx_test run --target dev --profile my-profile

# Workspace path
dbx_test run --tests-dir /Workspace/Users/you@company.com/tests --profile my-profile

# With multiple output formats
dbx_test run --target dev --profile prod \
  --output-format junit \
  --output-format html

dbx_test scaffold

Create a new test notebook from template.

# Create test and config files
dbx_test scaffold my_feature

# Detects bundle projects and provides bundle-specific instructions

dbx_test upload

Upload test notebooks to Databricks workspace.

# Upload local tests to workspace
dbx_test upload --tests-dir tests \
  --workspace-path /Workspace/Users/you@company.com/tests \
  --profile dev

Configuration

See Configuration Guide for detailed configuration options.

Documentation

Getting Started

Core Features

Advanced Topics

Integration

Examples

Architecture

src/dbx_test/
├── cli.py                 # CLI entry point
├── config.py              # Configuration management
├── runner_remote.py       # Remote Databricks execution (serverless/cluster)
├── notebook_runner.py     # Notebook test execution
├── testing.py             # Test fixture base class
├── reporting.py           # Report generation
├── artifacts.py           # Artifact management
├── bundle.py              # Databricks Asset Bundle integration
└── utils/                 # Utility functions
    ├── databricks.py      # Databricks API helpers (inline environments)
    ├── notebook.py        # Notebook parsing
    └── validation.py      # Validation utilities

Why This Framework?

✅ Serverless-First Design

  • Automatic inline environment creation
  • No cluster management overhead
  • Fast startup times
  • Cost-effective pay-per-use model

✅ Databricks Asset Bundle Native

  • Auto-detects bundle projects
  • Resolves workspace paths automatically
  • Seamless integration with databricks bundle deploy
  • No manual path configuration

✅ Developer-Friendly

  • Pytest-style test discovery
  • Simple test patterns (setup → test → cleanup)
  • Rich CLI output
  • Works in notebooks and CI/CD

✅ Production-Ready

  • JUnit XML for CI/CD integration
  • Parallel execution support
  • Comprehensive error reporting
  • Battle-tested on real projects

License

MIT License - see LICENSE file for details.

Contributing

Contributions welcome! Please see CONTRIBUTING.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dbx_test-0.1.0.tar.gz (86.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dbx_test-0.1.0-py3-none-any.whl (39.5 kB view details)

Uploaded Python 3

File details

Details for the file dbx_test-0.1.0.tar.gz.

File metadata

  • Download URL: dbx_test-0.1.0.tar.gz
  • Upload date:
  • Size: 86.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for dbx_test-0.1.0.tar.gz
Algorithm Hash digest
SHA256 710b2a905ab156c010347d3496136c0f82ce522e85c6b5944d6bcf55aaa14ee3
MD5 ffff741b8d71d84ecc34421345261c34
BLAKE2b-256 afc045abc8d1f0b3707da614a9499b7ccf70efabdc1aa6f855fa945e4ee90bca

See more details on using hashes here.

File details

Details for the file dbx_test-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dbx_test-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 39.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for dbx_test-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 41e7a72a240ee12789eabf605339a0d0e9e4d192872091a211d74d55420b4346
MD5 f8727d077b19771ed11b5e1639392f8d
BLAKE2b-256 14e46462712ac4a4346f16c6881187f268d86f5f900d9bf23a12d442f48e10f1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page