vberlier / pytest-insta Goto Github PK
View Code? Open in Web Editor NEWA practical snapshot testing plugin for pytest.
License: MIT License
A practical snapshot testing plugin for pytest.
License: MIT License
Thanks for making this! Insta is great.
Suppose I am testing the old and new versions of an API - I may want two flavors of every snapshot. Naively I'd write:
assert snapshot("v2.json") == (await client.get_new_api()).json()
assert snapshot("v1.json") == (await client.get_old_api()).json()
That would work fine as long as I called this only once. But what happens when my test wants to call each API multiple times? If I run with --insta update
, I end up with the last response saved. If I run without --insta update
, the test obviously fails.
There are two problems here:
update
run.I'd be happy to send a pull request! Let me know how you feel about this.
Insta has a neat feature called redactions which let you omit certain values from the snapshot (e.g. a timestamp field which will be different on every run).
Is it possible to do something similar with pytest-insta
?
Hi @vberlier
First off, what a wonderful library! Thanks for the time and effort you've put into this, it really scratches an itch I've experienced when it comes to testing in the Python world.
I wanted to discuss if there is interest in extending the functionality (which I'd be more than happy to contribute the work towards) to allow for setting "partial" expectations in a snapshot.
Here's a use case to hopefully illustrate that sort of functionality.
I often find myself writing tests like this....
def test_posts_detail(api_client):
post = PostFactory(title="Amazing deal!", body="Don't miss out", published_at=date(2021, 1, 1))
response = api_client.get(f"/posts/{post.id}/")
assert response.json() == {
"id": str(post.id),
"title": "Amazing deal!",
"body": "Don't miss out",
"published_at": "2021-1-1"
}
It's super useful to be able this against a snapshot. Especially as project go on, and API change over time, the review process really help keep things manageable.
However, there is one snag, most of all the fields are "frozen" but in this case the id
is dynamic. I can't control what it will be (i.e depends on how many tests, what operation happened before, etc).
I would love the ability to provide the snapshot with a few selected expectations, i.e something like this....
def test_posts_detail(api_client, snapshot):
post = PostFactory(title="Amazing deal!", body="Don't miss out", published_at=date(2021, 1, 1))
response = api_client.get(f"/posts/{post.id}/")
assert snapshot("json", {"id": str(post.id)}) == response.json()
In this case, the id
will always be updated and set at runtime in the test, but the rest of are snapshotted.
This looks like a really nice project! I was looking for a snapshot testing library to automate tests that compare some JSON responses. One feature I would really like is to store the snapshots in code. For example, something like this:
def test_foo():
expected_value = snapshot(
{...} # some large dict
)
actual_value = foo()
assert expected_value == actual_value
The reason for this is to keep the expected/snapshotted value close to where it is being used, which makes it a lot easier to inspect the expected value, e.g. in code review.
Although I've never used it, it seems that insta (Rust version) supports this, albeit only for strings. I think native Python objects would be ideal, but storing inline as JSON might be easier.
Steps to reproduce
snapshot('audit.txt')
snapshot('audit.json')
--insta review
Expected behavior
Things work just fie.
Review works as expected or alternatively I am instructed to do some kind of cleanup.
Actual behavior
As audit.txt
is still existing in the cache folder the last pytest call fails with an error like this:
FileNotFoundError: [Errno 2] No such file or directory: 'flaskapp/snapshots/views__cm_views__audit.txt'
Running --insta clear
fixes the issue.
For new users, this behavior can be quite unexpected. Especially as one might not even remember anymore that a snapshot with the unexpected name once existed.
I'm having trouble getting the snapshot tests to pass in my CI as the snapshot location is different in CI than locally. For example the github runner fails with:
assert <not found:'ast_parsing__vyper_cfgir_home_runner_work_slither_slither_tests_e2e_vyper_parsing_test_data_precedence_vy_foo__0.txt'>
Locally, it is named ast_parsing__vyper_cfgir_builtins_test_builtins__0.txt
and the test passes
Firstly, thank you for creating this package. It seems really well made and I have been liking it so far! ❤️
When I first create a snapshot test with snapshot()
and run pytest --insta review
, I see this in the output:
========================================================================================== SNAPSHOTS ===========================================================================================
CREATE tests/snapshots/<snapshot name>.json
But I don't get to accept/reject this first snapshot. I then have to manually cat
the file and review it.
IIRC Armin's insta
(the rust one), asks you to review the first snapshot too. It would be nice if pytest-insta
did the same too.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.