Skip to main content

A pytest plugin for snapshot testing against R code outputs

Project description

pytest-r-snapshot

PyPI version Python versions CI tests Mypy check Ruff check Documentation License

A pytest plugin for snapshot testing against reference outputs produced by R code. Particularly useful for test-driven development (TDD) when porting R packages to Python.

It is designed for a portable workflow:

  • Record locally (requires R): run labelled R chunks embedded in your Python tests and write snapshot files.
  • Replay everywhere (default; no R required): read committed snapshot files and compare them to Python outputs.

The R chunks live close to the test assertion (copy/paste-able from R scripts or R Markdown), but snapshots are stored as deterministic UTF-8 text files.

Installation

You can install pytest-r-snapshot from PyPI:

pip install pytest-r-snapshot

If your project is managed with uv, add it as a dev dependency:

uv add --dev pytest-r-snapshot

Or install the development version from GitHub:

git clone https://github.com/nanxstats/pytest-r-snapshot.git
cd pytest-r-snapshot
python3 -m pip install -e .

Quick start

Embed a labelled R fenced chunk (commented or in a docstring), then compare your Python output to the recorded snapshot:

def test_summary_matches_r(r_snapshot):
    # ```{r, summary}
    # x <- c(1, 2, 3)
    # summary(x)
    # ```

    actual = my_python_summary(...)
    r_snapshot.assert_match_text(actual, name="summary")

Generate (or update) snapshots locally:

pytest --r-snapshot=record

Commit the generated snapshot files, and run CI with the default replay mode (no R required).

What you get

  • A pytest fixture r_snapshot with methods:
    • assert_match_text(actual, name=..., ext=".txt", normalize=...)
    • read_text(name=..., ext=".txt")
    • record_text(name=..., ext=".txt")
    • path_for(name=..., ext=".txt")
  • Snapshot modes: replay (default), record, auto.
  • Chunk extraction for R Markdown-style fenced chunks inside Python source:
    • Commented fences (chunk lines prefixed with #).
    • Raw chunks in docstrings / multi-line strings.
  • Configurable R execution context: Rscript path, environment, working directory, timeout, encoding.

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_r_snapshot-0.1.2.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

pytest_r_snapshot-0.1.2-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest_r_snapshot-0.1.2.tar.gz.

File metadata

  • Download URL: pytest_r_snapshot-0.1.2.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pytest_r_snapshot-0.1.2.tar.gz
Algorithm Hash digest
SHA256 1be201452d92eae7a56abc3ff455db6c69638a7a74492dc4c7d6fc882ee561f2
MD5 637bf59e2f13b8d33d517cfcc2f95e95
BLAKE2b-256 3491536bf92c3512c6294e9d1def0f795828a6f5784abf7316a7b556bf22a439

See more details on using hashes here.

File details

Details for the file pytest_r_snapshot-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pytest_r_snapshot-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pytest_r_snapshot-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4b1aa1b8c04d19f7c303ff1e7fb6ff9d4f2ecce1ac3452966edf08e91aa8b604
MD5 7a5b44959b3556978138d02ca95e4062
BLAKE2b-256 14d05a867051ed4b8f05da46967f886d80f06523e54e5042fd5b050bb2638483

See more details on using hashes here.

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