Skip to main content

Notebook-friendly approval testing for Python and Jupyter

Project description

nbapproval

Notebook-friendly approval testing for Python and Jupyter.

nbapproval lets you compare actual notebook outputs to approved values, store approvals in a separate approvals notebook, and fail CI runs when approvals are missing or mismatched.

Install

pip install nbapproval

Quick Start

from nbapproval import approval_test

approval_test(
    "Simple approval check",
    {"value": 42},
)

approval_test.assert_all_approved()

API (Terse-First)

Primary call supports concise notebook usage:

approval_test(description, actual, sort_by=None)

Also supported:

  • id alias for test_id
  • desc alias for description
  • keyword form: actual=...
  • automatic test_id derivation from description when omitted
  • pandas DataFrame values as actual (auto-converted to stable records)

Examples:

# Explicit id + keyword style
approval_test(
    id="known_holiday_checkpoints_match_expected_names_for_specific_dates",
    desc="Known holiday checkpoints match expected names for specific dates.",
    actual=approval_test.to_iso_records(actual_df),
    sort_by=["Date", "Expected"],
)

# Terse positional style (id auto-derived from description)
approval_test(
    "Known holiday checkpoints match expected names for specific dates.",
    actual_df,
    sort_by=["Date", "Expected"],
)

approval_test.from_dataframe(...) remains available, but is optional now because the main call handles DataFrames directly.

Runtime Status

You can inspect the current approval run state directly:

approval_test.status_report()
approval_test.approvals_notebook_path
  • status_report() returns totals and per-test statuses for the current session.
  • approvals_notebook_path returns the resolved approvals notebook path.

approval_test.assert_all_approved() prints a summary and approvals notebook path, then raises if any test is not Approved.

Notebook Magics

The package registers two IPython magics for concise notebook tests:

  • %approve for line-style checks
  • %%approve for cell-style checks

Examples:

# line magic with expression only
%approve bool(df["Date"].is_monotonic_increasing)

# line magic with options + expression (use :: separator)
%approve --id dates_are_sorted --desc "Dates are sorted" :: bool(df["Date"].is_monotonic_increasing)

# cell magic with a code block; last expression becomes approved value
%%approve --desc "Federal holidays for 2026" --sort-by "['Date', 'Holiday']"
df.loc[df["Year"] == 2026, ["Date", "Holiday"]]

Notes:

  • In %approve, when options are present, put :: before the expression.
  • In %%approve, full code blocks are supported; setup statements are allowed, and the last expression is used as actual.

Testing

Run tests with:

pip install -e .[dev]
pytest -q

Notes

  • Stable and unique test_id values are required.
  • For deterministic CI runs, configure an explicit approvals notebook path.
  • Works well with Papermill-driven notebook execution.

License

Apache License 2.0. See LICENSE.

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

nbapproval-0.4.0.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

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

nbapproval-0.4.0-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file nbapproval-0.4.0.tar.gz.

File metadata

  • Download URL: nbapproval-0.4.0.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nbapproval-0.4.0.tar.gz
Algorithm Hash digest
SHA256 41926a0d21ba6d3cb88a398759391f263931d62002d0299dd84d00c44f818ed8
MD5 eb05188199bf978f083f1f0206eb8bba
BLAKE2b-256 8bcd1821039448d496a9c75d16e5b3eabeca1009dd1ffe7f8c3381e054ce42e7

See more details on using hashes here.

Provenance

The following attestation bundles were made for nbapproval-0.4.0.tar.gz:

Publisher: publish.yml on wiki3-ai/nbapproval

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nbapproval-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: nbapproval-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nbapproval-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30c0da95606de462391c1d855f5c8fec53e59d72cf4cc8002d74e5f98836097a
MD5 48f605f8d92c5c2fa0206cd45a1498a9
BLAKE2b-256 f13ebb3c1c420de7dbc77ef828bd0a3a9670a0c0449c1dcdbbf7d7caf37a4861

See more details on using hashes here.

Provenance

The following attestation bundles were made for nbapproval-0.4.0-py3-none-any.whl:

Publisher: publish.yml on wiki3-ai/nbapproval

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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