Coder Social home page Coder Social logo

pytest-insta's People

Contributors

aberres avatar actions-user avatar dependabot-preview[bot] avatar dependabot[bot] avatar vberlier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pytest-insta's Issues

feature request: snapshot name suffix

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:

  • there's no way to ask for a snapshot to have a name suffix (and the "name" argument is overloaded to mean both "type" and "exact name").
  • the framework does not detect the same snapshot being overwritten multiple times in the same update run.

I'd be happy to send a pull request! Let me know how you feel about this.

Redactions

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?

Partial matching functionality?

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.

Storing snapshots inline

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.

"review" chokes on old snapshots in test directory

Steps to reproduce

  • Create a snapshot like snapshot('audit.txt')
  • Run test
  • Rename snapshot like like snapshot('audit.json')
  • Run test
  • Now run test with --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.

local and CI snapshot patch are not matching

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

Review snapshots on creation too

Firstly, thank you for creating this package. It seems really well made and I have been liking it so far! ❤️

Overview

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.

Why?

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.