Skip to content

Introduce pytest.not_raises #2339

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

Closed
wants to merge 1 commit into from

Conversation

nicoddemus
Copy link
Member

Fix #1830

@nicoddemus nicoddemus requested a review from The-Compiler March 30, 2017 01:49
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 92.607% when pulling 24b1895 on nicoddemus:not-raises into 2a130da on pytest-dev:features.

But this makes us repeat ourselves on the test function.

For these situations, where you need to ``parametrize`` a test function
so that sometimes it raises an exception and others do not, you can use
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"and others do not" -> "and sometimes doesn't"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that is better! 👍

@The-Compiler
Copy link
Member

LGTM apart from a sentence which sounds a bit weird 😉 Haven't tried it yet though.

@@ -131,6 +131,7 @@ def pytest_namespace():
raises.Exception = pytest.fail.Exception
return {
'raises': raises,
'not_raises': NotRaisesContext,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have a strong opinion on naming it wont_raise in order to give acroos the semantics better

Copy link
Member Author

@nicoddemus nicoddemus Mar 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like not_raises due to its similarity to raises, but I don't have a strong opinion on it. @The-Compiler do you mind changing to wont_raise?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both sound weird, so no strong opinion here 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, an additonal note, why do we make it a callable?

i believe making it a singleton context-manager named after exactly the context its going to encompass is the best we can do,

else we might see people wanting to creep in more specific behavior causing grief down the line

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, an additonal note, why do we make it a callable?

It felt more natural, because you can use pytest.raises(...). No problem changing it to a singleton if you feel it is more appropriate.

else we might see people wanting to creep in more specific behavior causing grief down the line

What do you mean? People asking to some weird parameter to it to give additional behavior? Can't we just say no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About wont_raise... I'm having some second thoughts that I would like you to consider @RonnyPfannschmidt:

  1. pytest.raises maps well to assert raises, and so does pytest.not_raises that also maps well to assert not raises.
  2. pytest.wont_raises is a contraction of will not, which is not so nice in general;

Hmm on second thought I think we should ask others for their opinion on this. It is important to nail this down right now than regret it later. I'll send an email to the list! 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoddemus wrt feature creep, sometimes it just happens, see the major mess non-strict xfail got us into because we did a too cheap job of handling flaky tests better

we should either clearly limit it, or clearly support passing exception types from the get go

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should either clearly limit it, or clearly support passing exception types from the get go

OK I don't have a strong objection here.

Also asked in the mailing list if anyone sees a problem in making this a singleton. 👍

('1foo bar_', pytest.raises(ValueError)),
])
def test_check_python_identifier(ident, expectation):
with pytest.raises(expectation):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't either the function signature be test_check_python_identifier(ident, valid): or the first argument to parameterize be ident, expectation?

Also, shouldn't the with statement be:

    with expectation:
      ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wcooley but I'm declining this PR for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicoddemus So I noticed after finding & reading over the mailing list threads...

For the sake of posterity for anyone finding this PR and wondering what to do, (I believe but have not actually tried) this can be achieved with a simple no-op context manager:

import contextlib

@contextlib.contextmanager
def noop():
    yield

Then:

@pytest.mark.parametrize('ident, expectation', [
        ('foobar', noop()),
        ('Foobar1', noop()),
        ('uh-oh', pytest.raises(Exception))])
def test_whatever(ident, expectation):
    with expectation:
       if 'oo' not in ident:
         raise Exception('No OO')

@nicoddemus
Copy link
Member Author

Closing this PR because unfortunately we couldn't reach a consensus on the mailing list about a proper API. 😞

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

Successfully merging this pull request may close these issues.

5 participants