Skip to main content

Snapshot testing pytest plugin with minimal ceremony and flexible persistence formats.

Project description

pytest-ditto

PyPI version Continuous Integration

Snapshot testing pytest plugin with minimal ceremony and flexible persistence formats.

Introduction

The pytest-ditto plugin is intended to be used snapshot/regression testing. There are two key components: the snapshot fixture and the snapshot persistence formats.

The snapshot Fixture

In the following basic example, the function to test is fn, the test is using the snapshot fixture and it is asserting that the result of calling the fn with the value of x does not change.

import ditto


def fn(x: int) -> int:
    return x + 1  # original implementation
    # return x + 2  # new implementation


def test_fn(snapshot) -> None:
    x = 1
    result = fn(x)
    assert result == snapshot(result, key="fn")

The first time the test is run, the snapshot fixture takes the data passed to it and persists it to a .ditto directory in the same location as the test module. Subsequent test runs will load the file and use that value in the test to test the output of the computed value.

By default, the snapshot data is converted and persisted using pickle; however, there are a range of persistence formats that can be used.

@ditto Marks

If the default persistence format, pickle, isn't appropriate different formats can be specified per test by using ditto marks - customised pytest mark decorators.

The default persistence types are: pickle, yaml and json; however additional plugins can be installed as per below:

Types Plugin Marks
pandas pytest-ditto-pandas
  • @ditto.pandas.parquet
  • @ditto.pandas.json
  • @ditto.pandas.csv
pyarrow pytest-ditto-pyarrow
  • @ditto.pyarrow.parquet
  • @ditto.pyarrow.feather
  • @ditto.pyarrow.csv

Usage

pd.DataFrame

Install pytest-ditto[pandas] to make pandas IO types available.

import pandas as pd

import ditto


def awesome_fn_to_test(df: pd.DataFrame):
    df.loc[:, "a"] *= 2
    return df


@ditto.pandas.parquet
def test_fn_with_parquet_dataframe_snapshot(snapshot):
    input_data = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 9]})
    result = awesome_fn_to_test(input_data)
    pd.testing.assert_frame_equal(result, snapshot(result, key="ab_dataframe"))


@ditto.pandas.json
def test_fn_with_json_dataframe_snapshot(snapshot):
    input_data = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 9]})
    result = awesome_fn_to_test(input_data)
    pd.testing.assert_frame_equal(result, snapshot(result, key="ab_dataframe"))

For the above example the snapshot files would be found in the following locations:

  • .ditto/test_fn_with_parquet_dataframe_snapshot@ab_dataframe.pandas.parquet
  • .ditto/test_fn_with_json_dataframe_snapshot@ab_dataframe.pandas.json

pyarrow.Table

Install pytest-ditto[pyarrow] to make pyarrow IO types available.

import pyarrow as pa
import pyarrow.compute as pc
import ditto
import pytest


@pytest.fixture
def table() -> pa.Table:
    return pa.table(
        [
            [1, 2, 3, 4],
            [4.5, 5.2, 6.8, 3.5],
            [7, 8.5, None, None],
            [True, False, True, True],
            ["a", "b", "c", "x"],
        ],
        names=list("abcde"),
    )


def fn(x: pa.Table):
    even_filter = (pc.bit_wise_and(pc.field("a"), pc.scalar(1)) == pc.scalar(0))
    return x.filter(even_filter)


@ditto.pyarrow.parquet
def test_fn_with_pyarrow_parquet_snapshot(snapshot, table):
    result = fn(table)
    assert result.equals(snapshot(result, key="filtered"))

For the above example the snapshot files would be found in the following location:

  • .ditto/test_fn_with_pyarrow_parquet_snapshot@filtered.pyarrow.parquet

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

pytest_ditto-0.2.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

pytest_ditto-0.2.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_ditto-0.2.0.tar.gz.

File metadata

  • Download URL: pytest_ditto-0.2.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_ditto-0.2.0.tar.gz
Algorithm Hash digest
SHA256 141e983d2c4e80a691cb12348f2f73a807e64d887150afcb6b7e74ae14d832bb
MD5 840ce0f5a20e85d61eea7c7bfa9b8432
BLAKE2b-256 24101e2ef85791ff3958f67a2c96c88fb9af913f1c70fcdf04c567699d355b0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_ditto-0.2.0.tar.gz:

Publisher: release.yml on owlowlyowl/pytest-ditto

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

File details

Details for the file pytest_ditto-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_ditto-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_ditto-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 936ad2a9876dd5a020303ebc97be1800c1f1c96b9402055571069f8ae8bf40b0
MD5 42737e33a476457dda5e3ec02e2dea51
BLAKE2b-256 005ef2a832a2fc4bd2a780af37c2c2f99a76f8969a649c2bf4960b3ee9ec5d7a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_ditto-0.2.0-py3-none-any.whl:

Publisher: release.yml on owlowlyowl/pytest-ditto

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