Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/15r10nk/inline-snapshot
create and update inline snapshots in your python tests
https://github.com/15r10nk/inline-snapshot
approval-testing golden-master pytest python snapshot-testing testing
Last synced: 25 days ago
JSON representation
create and update inline snapshots in your python tests
- Host: GitHub
- URL: https://github.com/15r10nk/inline-snapshot
- Owner: 15r10nk
- License: mit
- Created: 2022-07-25T16:57:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-21T08:26:41.000Z (6 months ago)
- Last Synced: 2024-05-21T16:14:12.815Z (6 months ago)
- Topics: approval-testing, golden-master, pytest, python, snapshot-testing, testing
- Language: Python
- Homepage: https://15r10nk.github.io/inline-snapshot/
- Size: 1.86 MB
- Stars: 326
- Watchers: 2
- Forks: 9
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
![ci](https://github.com/15r10nk/inline-snapshot/actions/workflows/ci.yml/badge.svg?branch=main)
[![Docs](https://img.shields.io/badge/docs-mkdocs-green)](https://15r10nk.github.io/inline-snapshot/)
[![pypi version](https://img.shields.io/pypi/v/inline-snapshot.svg)](https://pypi.org/project/inline-snapshot/)
![Python Versions](https://img.shields.io/pypi/pyversions/inline-snapshot)
![PyPI - Downloads](https://img.shields.io/pypi/dw/inline-snapshot)
[![coverage](https://img.shields.io/badge/coverage-100%25-blue)](https://15r10nk.github.io/inline-snapshot/contributing/#coverage)
[![GitHub Sponsors](https://img.shields.io/github/sponsors/15r10nk)](https://github.com/sponsors/15r10nk)## Installation
You can install "inline-snapshot" via [pip](https://pypi.org/project/pip/):
``` bash
pip install inline-snapshot
```## Key Features
- **Intuitive Semantics:** `snapshot(x)` mirrors `x` for easy understanding.
- **Versatile Comparison Support:** Equipped with `x == snapshot(...)`, `x <= snapshot(...)`, `x in snapshot(...)`, and `snapshot(...)[key]`.
- **Enhanced Control Flags:** Utilize various [flags](https://15r10nk.github.io/inline-snapshot/pytest/) for precise control of which snapshots you want to change.
- **Preserved Black Formatting:** Retains formatting consistency with Black formatting.
- **External File Storage:** Store snapshots externally using `outsource(data)`.
- **Seamless Pytest Integration:** Integrated seamlessly with pytest for effortless testing.
- **Customizable:** code generation can be customized with [@customize_repr](https://15r10nk.github.io/inline-snapshot/customize_repr)
- **Comprehensive Documentation:** Access detailed [documentation](https://15r10nk.github.io/inline-snapshot/) for complete guidance.## Usage
You can use `snapshot()` instead of the value which you want to compare with.
``` python
from inline_snapshot import snapshotdef test_something():
assert 1548 * 18489 == snapshot()
```You can now run the tests and record the correct values.
```
$ pytest --inline-snapshot=review
`````` python hl_lines="5"
from inline_snapshot import snapshotdef test_something():
assert 1548 * 18489 == snapshot(28620972)
```The following examples show how you can use inline-snapshot in your tests. Take a look at the
[documentation](https://15r10nk.github.io/inline-snapshot/) if you want to know more.``` python
from inline_snapshot import snapshot, outsource, externaldef test_something():
for number in range(5):
# testing for numeric limits
assert number <= snapshot(4)
assert number >= snapshot(0)for c in "hello world":
# test if something is part of a set
assert c in snapshot(["h", "e", "l", "o", " ", "w", "r", "d"])s = snapshot(
{
0: {"square": 0, "pow_of_two": False},
1: {"square": 1, "pow_of_two": True},
2: {"square": 4, "pow_of_two": True},
3: {"square": 9, "pow_of_two": False},
4: {"square": 16, "pow_of_two": True},
}
)for number in range(5):
# create sub-snapshots at runtime
assert s[number]["square"] == number**2
assert s[number]["pow_of_two"] == (
(number & (number - 1) == 0) and number != 0
)assert outsource("large string\n" * 1000) == snapshot(
external("8bf10bdf2c30*.txt")
)assert "generates\nmultiline\nstrings" == snapshot(
"""\
generates
multiline
strings\
"""
)
````snapshot()` can also be used as parameter for functions:
``` python
from inline_snapshot import snapshot
import subprocess as sp
import sysdef run_python(cmd, stdout=None, stderr=None):
result = sp.run([sys.executable, "-c", cmd], capture_output=True)
if stdout is not None:
assert result.stdout.decode() == stdout
if stderr is not None:
assert result.stderr.decode() == stderrdef test_cmd():
run_python(
"print('hello world')",
stdout=snapshot(
"""\
hello world
"""
),
stderr=snapshot(""),
)run_python(
"1/0",
stdout=snapshot(""),
stderr=snapshot(
"""\
Traceback (most recent call last):
File "", line 1, in
ZeroDivisionError: division by zero
"""
),
)
```## Feedback
inline-snapshot provides some advanced ways to work with snapshots.
I would like to know how these features are used to further improve this small library.
Let me know if you've found interesting use cases for this library via [twitter](https://twitter.com/15r10nk), [fosstodon](https://fosstodon.org/deck/@15r10nk) or in the github [discussions](https://github.com/15r10nk/inline-snapshot/discussions/new?category=show-and-tell).## Sponsors
I would like to thank my sponsors. Without them, I would not be able to invest so much time in my projects.
### Bronze sponsor 🥉
## Issues
If you encounter any problems, please [report an issue](https://github.com/15r10nk/inline-snapshot/issues) along with a detailed description.
## License
Distributed under the terms of the [MIT](http://opensource.org/licenses/MIT) license, "inline-snapshot" is free and open source software.