Skip to main content

Pytest plugin for golden master (characterisation) testing with automatic expected file regeneration.

Project description

PyPI version PyPI - Python Version PyPI - License

pytest-remaster

Pytest plugin for golden master (characterisation) testing with automatic expected file regeneration.

Example 1: directory per test case

Each test case is a directory with input files and numbered expected outputs:

tests/cases/
  greet/hello/
    command             # input
    expected_0.txt      # first expected output
  help/unknown/
    command
    expected_0.txt
    expected_1.txt      # multiple outputs supported
import pytest
from pytest_remaster import CaseData, GoldenMaster, discover_test_cases

CASES_DIR = Path(__file__).parent / "cases"

@pytest.mark.parametrize("case", discover_test_cases(CASES_DIR))
def test_command(case: CaseData, golden_master: GoldenMaster) -> None:
    cmd = (case.input / "command").read_text().strip()
    golden_master.check_all(lambda: my_app(cmd), case.input, suffix=".txt")

Example 2: one file per test case

Each test case is a source file, with expected output derived from the filename:

tests/functional/
  arguments.py          # input (source to lint)
  arguments.txt         # expected output
  anomalous.py
  anomalous.txt
import pytest
from pytest_remaster import CaseData, GoldenMaster, discover_test_files

FUNC_DIR = Path(__file__).parent / "functional"

@pytest.mark.parametrize("case", discover_test_files(FUNC_DIR, "*.py"))
def test_lint(case: CaseData, golden_master: GoldenMaster) -> None:
    golden_master.check(lambda: lint(case.input), case.expected(suffix=".txt"))

Both examples auto-update expected files on mismatch. Review the diff in git, rerun. Pass --no-remaster for strict comparison.

API

golden_master.check(actual, expected_path)

Compare one value against one expected file:

golden_master.check(output, case.expected(suffix=".txt"))
golden_master.check(data, path / "db.json", normalizer=json_normalizer)

Options: serializer=str, normalizer=None.

golden_master.check_all(actuals, directory)

Compare a list against expected_0, expected_1, ... files in a directory:

golden_master.check_all(lambda: my_app(cmd), case.input, suffix=".txt")

Options: serializer=str, normalizer=None, suffix="".

Discovery

discover_test_cases(base_dir)               # leaf directories → CaseData
discover_test_files(base_dir, "*.py")       # files by pattern → CaseData

CaseData.input is the source path. CaseData.expected(index, suffix) derives expected file paths.

FilePatchRegistry

Auto-load fixture files from case directories and patch mock targets:

from pytest_remaster import FilePatchRegistry

patcher = FilePatchRegistry()
patcher.register("command", loader=str.strip)
patcher.register("salt.json", target="pepper.Pepper", attr="return_value.low.side_effect")
patcher.register("tiger.json", target="requests.get", attr="return_value.json.side_effect", default=[])
patcher.register("user.json", default={"name": "default"})

@pytest.mark.parametrize("case", discover_test_cases(CASES_DIR))
def test_command(case, golden_master):
    with patcher.mock(case) as loaded:
        events = run_command(loaded["command"], loaded["user.json"])
        golden_master.check_all(events, case.input)

Options: target=None (load only), attr="return_value" (nested mock attribute path), loader=json.loads, default=None.

Configuration

[tool.pytest.ini_options]
remaster-by-default = false  # default: true

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_remaster-0.0.1a2.tar.gz (17.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_remaster-0.0.1a2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest_remaster-0.0.1a2.tar.gz.

File metadata

  • Download URL: pytest_remaster-0.0.1a2.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytest_remaster-0.0.1a2.tar.gz
Algorithm Hash digest
SHA256 8f1c31ba6ee52fbe0329e9c70bc3558a767ba024e0b55ebe71f2ac99e5b8d350
MD5 8901d3f6c5ae73783952dcd20a4f62af
BLAKE2b-256 0794758840e4ec8bf24734b183e79779bea264a186440ed88d5de54782ce2019

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_remaster-0.0.1a2.tar.gz:

Publisher: release.yaml on Pierre-Sassoulas/pytest-remaster

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_remaster-0.0.1a2-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_remaster-0.0.1a2-py3-none-any.whl
Algorithm Hash digest
SHA256 32606a912d5e8f8f7653342f7fd217e8a4577f26e9cb2552abfbdf2b52a0df5f
MD5 19933eb8a58501ca011b05c800c2afa9
BLAKE2b-256 16e200fd133b2b29c6724ff94856e1dd9f2ca3360f852daf5982f6ec8f3393cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_remaster-0.0.1a2-py3-none-any.whl:

Publisher: release.yaml on Pierre-Sassoulas/pytest-remaster

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