Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for with snapshot() #81

Open
zmievsa opened this issue May 2, 2024 · 3 comments
Open

Add support for with snapshot() #81

zmievsa opened this issue May 2, 2024 · 3 comments

Comments

@zmievsa
Copy link

zmievsa commented May 2, 2024

When you develop libraries, you often need to check that your library raises some exception at runtime.

I created better-devtools (Samuel Colvin's devtools fork) to automate this process and I would like to propose a similar tool for snapshot.

It would allow for something like the following syntax:

with snapshot():
    raise ValueError

Which would translate into something like the following:

with snapshot(pytest.raises(ValueError)):
    raise ValueError
@15r10nk
Copy link
Owner

15r10nk commented May 2, 2024

I use something like this in my own tests.

def test_with():
    with raises(snapshot("ZeroDivisionError: division by zero")):
        print(1/0)

the implementation looks like this:

@contextlib.contextmanager
def raises(snapshot):
    with pytest.raises(Exception) as excinfo:
        yield

    assert f"{type(excinfo.value).__name__}: {excinfo.value}" == snapshot

would this fit your needs? I could put such generic utils in a inline_snapshot.extras module.

@zmievsa
Copy link
Author

zmievsa commented May 3, 2024

Yup, it would! Looks great! You really did think of all use cases.

This is an amazing project.

@alexmojaki
Copy link
Sponsor

That is very cool. I think I've had times where I would have used this, and it didn't occur to me that vanilla snapshot could do the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants