Skip to main content

Golden master testing for pytest with automatic regeneration.

Project description

PyPI version PyPI - Python Version PyPI - License

pytest-remaster

Golden master testing for pytest. Compare output against expected files, auto-regenerate on mismatch.

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("api_result.json", target="myapp.api.call")
patcher.register("user.json", target="myapp.get_user", default=None)

@pytest.mark.parametrize("case", discover_test_cases(CASES_DIR))
@patcher.use
def test_command(case, golden_master):
    golden_master.check_all(lambda: my_app(case), case.input)

Options: target=None (load only), side_effect=True, loader=json.loads, case_param="case".

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.1a1.tar.gz (17.1 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.1a1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytest_remaster-0.0.1a1.tar.gz
  • Upload date:
  • Size: 17.1 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.1a1.tar.gz
Algorithm Hash digest
SHA256 d3b33276401b566667ff66d31ae249d6a449841e85d97817d71cd9c746847884
MD5 b5c7dae1b278e7b3dba96eec6cc2fd61
BLAKE2b-256 cd858625c6a360b79bdf1422e6e2014d32606f916c5020f59b54109d2403036f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_remaster-0.0.1a1.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.1a1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_remaster-0.0.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 d7289b81c4e50986ce86b75b63ea569cf06d89d61cc8ffbbeb5f97fc7987ba59
MD5 90a39ca8331870e478b5a60412bce315
BLAKE2b-256 b51d70d4c22ac94432bcdef7caf8ada9164c7d9ec99cb45f0520dbcb26ef6555

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_remaster-0.0.1a1-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