pytest-ditto


Namepytest-ditto JSON
Version 0.1.2 PyPI version JSON
download
home_pageNone
SummarySnapshot testing pytest plugin with minimal ceremony and flexible persistence formats.
upload_time2024-06-09 06:46:45
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseNone
keywords pytest testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pytest-ditto
[![PyPI version](https://badge.fury.io/py/pytest-ditto.svg)](https://badge.fury.io/py/pytest-ditto)
[![Continuous Integration](https://github.com/owlowlyowl/pytest-ditto/actions/workflows/ci.yml/badge.svg)](https://github.com/owlowlyowl/pytest-ditto/actions/workflows/ci.yml)

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. 


```python
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`

```python
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"))
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pytest-ditto",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "Lachlan Taylor <lachlanbtaylor@proton.me>",
    "keywords": "pytest, testing",
    "author": null,
    "author_email": "Lachlan Taylor <lachlanbtaylor@proton.me>",
    "download_url": "https://files.pythonhosted.org/packages/61/0a/658c761d1d910eb7eca85a59176daa34b72c3a1227d56efd3f358c2ff824/pytest_ditto-0.1.2.tar.gz",
    "platform": null,
    "description": "# pytest-ditto\n[![PyPI version](https://badge.fury.io/py/pytest-ditto.svg)](https://badge.fury.io/py/pytest-ditto)\n[![Continuous Integration](https://github.com/owlowlyowl/pytest-ditto/actions/workflows/ci.yml/badge.svg)](https://github.com/owlowlyowl/pytest-ditto/actions/workflows/ci.yml)\n\nSnapshot testing pytest plugin with minimal ceremony and flexible persistence formats.\n\n## Introduction\nThe `pytest-ditto` plugin is intended to be used snapshot/regression testing. There are\ntwo key components: the `snapshot` fixture and the snapshot persistence formats.\n\n### The `snapshot` Fixture\nIn the following basic example, the function to test is `fn`, the test is using the\n`snapshot` fixture and it is asserting that the result of calling the `fn` with the\nvalue of `x` does not change. \n\n\n```python\nimport ditto\n\n\ndef fn(x: int) -> int:\n    return x + 1  # original implementation\n    # return x + 2  # new implementation\n\n\ndef test_fn(snapshot) -> None:\n    x = 1\n    result = fn(x)\n    assert result == snapshot(result, key=\"fn\")\n```\n\nThe first time the test is run, the `snapshot` fixture takes the data passed to it and\npersists it to a `.ditto` directory in the same location as the test module. Subsequent\ntest runs will load the file and use that value in the test to test the output of the\ncomputed value.\n\nBy default, the snapshot data is converted and persisted using `pickle`; however, there\nare a range of persistence formats that can be used.\n\n### @ditto Marks\nIf the default persistence format, `pickle`, isn't appropriate different formats can be\nspecified per test by using `ditto` marks - customised `pytest` mark decorators.\n\nThe default persistence types are: `pickle`, `yaml` and `json`; however additional\nplugins can be installed as per below:\n- pandas via `pytest-ditto-pandas`\n\n\n## Usage\n\n### `pd.DataFrame`\n\n```python\nimport pandas as pd\n\nimport ditto\n\n\ndef awesome_fn_to_test(df: pd.DataFrame):\n    df.loc[:, \"a\"] *= 2\n    return df\n\n\n# The following test uses pandas.DataFrame.to_parquet to write the data snapshot to the\n# `.ditto` directory with filename:\n# `test_fn_with_parquet_dataframe_snapshot@ab_dataframe.pandas.parquet`.\n\n@ditto.pandas.parquet\ndef test_fn_with_parquet_dataframe_snapshot(snapshot):\n    input_data = pd.DataFrame({\"a\": [1, 2, 3], \"b\": [4, 5, 9]})\n    result = awesome_fn_to_test(input_data)\n    pd.testing.assert_frame_equal(result, snapshot(result, key=\"ab_dataframe\"))\n\n\n# The following test uses pandas.DataFrame.to_json(orient=\"table\") to write the data\n# snapshot to the `.ditto` directory with filename:\n# `test_fn_with_json_dataframe_snapshot@ab_dataframe.pandas.json`.\n\n@ditto.pandas.json\ndef test_fn_with_json_dataframe_snapshot(snapshot):\n    input_data = pd.DataFrame({\"a\": [1, 2, 3], \"b\": [4, 5, 9]})\n    result = awesome_fn_to_test(input_data)\n    pd.testing.assert_frame_equal(result, snapshot(result, key=\"ab_dataframe\"))\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Snapshot testing pytest plugin with minimal ceremony and flexible persistence formats.",
    "version": "0.1.2",
    "project_urls": null,
    "split_keywords": [
        "pytest",
        " testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "871d1e0ea13671f419874c373c8f59443dbf92ece7aa76da912304b099f011ed",
                "md5": "0d2e9aab819f301a20e8a67e7c159827",
                "sha256": "c656b9afd4c778711e03b1dbcf983e7906add93a0317cbf5dc64c7fccf66d89a"
            },
            "downloads": -1,
            "filename": "pytest_ditto-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0d2e9aab819f301a20e8a67e7c159827",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 8730,
            "upload_time": "2024-06-09T06:46:44",
            "upload_time_iso_8601": "2024-06-09T06:46:44.899545Z",
            "url": "https://files.pythonhosted.org/packages/87/1d/1e0ea13671f419874c373c8f59443dbf92ece7aa76da912304b099f011ed/pytest_ditto-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "610a658c761d1d910eb7eca85a59176daa34b72c3a1227d56efd3f358c2ff824",
                "md5": "cab5f2e705001b7a643ea3a9bacb7a52",
                "sha256": "355a0fba28ad1370a46750d87ed6cc2d40d810795a6691b1a94d467802fc4578"
            },
            "downloads": -1,
            "filename": "pytest_ditto-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "cab5f2e705001b7a643ea3a9bacb7a52",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 6310,
            "upload_time": "2024-06-09T06:46:45",
            "upload_time_iso_8601": "2024-06-09T06:46:45.981150Z",
            "url": "https://files.pythonhosted.org/packages/61/0a/658c761d1d910eb7eca85a59176daa34b72c3a1227d56efd3f358c2ff824/pytest_ditto-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-09 06:46:45",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "pytest-ditto"
}
        
Elapsed time: 0.52570s