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:

  • pandas via pytest-ditto-pandas

Usage

pd.DataFrame

import pandas as pd

import ditto


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


# The following test uses pandas.DataFrame.to_parquet to write the data snapshot to the
# `.ditto` directory with filename:
# `test_fn_with_parquet_dataframe_snapshot@ab_dataframe.pandas.parquet`.

@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"))


# The following test uses pandas.DataFrame.to_json(orient="table") to write the data
# snapshot to the `.ditto` directory with filename:
# `test_fn_with_json_dataframe_snapshot@ab_dataframe.pandas.json`.

@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"))

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.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

pytest_ditto-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_ditto-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pytest_ditto-0.1.0.tar.gz
Algorithm Hash digest
SHA256 351c9438b1fa320c7a6d7896b8fe9a08e9934c593b8e02e2851ba2576c513c80
MD5 e5101ea5b3319050c42ac4270e93dca9
BLAKE2b-256 8edc4446e1c5e758e459813fa78b044b1f01e0071dea7f3a4558af52cb6a4722

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_ditto-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pytest_ditto-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64f566aabe9c7821987d4c577307a21169be1845ef4e4258c8ab058f60ae70e4
MD5 36e0fb9733c7d3ff1cfaa9a2d95b31bf
BLAKE2b-256 7eaf31c2c66d569f4c82a1f07772cd7dfbc0f322ef5c4e7b08a715d2a2a3219b

See more details on using hashes here.

Supported by

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