A pytest plugin for snapshot testing against R code outputs
Project description
pytest-r-snapshot 
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_snapshotwith 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.
- Commented fences (chunk lines prefixed with
- Configurable R execution context:
Rscriptpath, environment, working directory, timeout, encoding.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1be201452d92eae7a56abc3ff455db6c69638a7a74492dc4c7d6fc882ee561f2
|
|
| MD5 |
637bf59e2f13b8d33d517cfcc2f95e95
|
|
| BLAKE2b-256 |
3491536bf92c3512c6294e9d1def0f795828a6f5784abf7316a7b556bf22a439
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b1aa1b8c04d19f7c303ff1e7fb6ff9d4f2ecce1ac3452966edf08e91aa8b604
|
|
| MD5 |
7a5b44959b3556978138d02ca95e4062
|
|
| BLAKE2b-256 |
14d05a867051ed4b8f05da46967f886d80f06523e54e5042fd5b050bb2638483
|