Skip to main content

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.

Metadata

Release files for pytest-r-snapshot 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-r-snapshot 0.1.3
File Size Uploaded
pytest_r_snapshot-0.1.3.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-r-snapshot 0.1.3
File Interpreter ABI Platform
pytest_r_snapshot-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 24.8 kB

Release files / pytest_r_snapshot-0.1.3.tar.gz

Download URL pytest_r_snapshot-0.1.3.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
dbc33f28046d938b59f6f0f96951be51f63956384164d3ea5b426db29c7b719a
BLAKE2b-256 checksum
How to use checksums
9a3615e25bf65e8ae99697db6b734b8985c6ea9b35bd7dbbc64594170f40ad46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.11 {"installer":{"name":"uv","version":"0.12.11","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}

Release files / pytest_r_snapshot-0.1.3-py3-none-any.whl

Download URL pytest_r_snapshot-0.1.3-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b52dd608865e3d9cb6daaa8b01f030c16b9cafac15a47f8dbaa2fe6de1fc37f
BLAKE2b-256 checksum
How to use checksums
a22d9562db6313aec9e9cffee0cc19432746be84c9f6544e756c3c312ad05ad5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.11 {"installer":{"name":"uv","version":"0.12.11","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}

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page