-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
raises regexp #372
Comments
Original comment by György Kiss (BitBucket: kissgyorgy, GitHub: kissgyorgy): I made a plugin for this if anyone interested: https://github.com/Walkman/pytest_raisesregexp |
Original comment by Ivan Smirnov (BitBucket: aldanor, GitHub: aldanor): @walkman_ I'd modify your example code slightly for it to behave more like class raises_regexp(object):
def __init__(self, exception, message, *args, **kwargs):
self.exception = exception
self.message = message
self.excinfo = None
if args:
with self:
args[0](*args[1], **kwargs) |
Original comment by Ivan Smirnov (BitBucket: aldanor, GitHub: aldanor): @hpk42 What do you think about all this? This is one feature that's available in unittest but not available in pytest and it's quite useful. I think adding a separate function would be cleaner since it would allow you use it same way as pytest.raises_regexp(ValueError, 'expected message', func, 1, 2, a=3, b=4) Adding a |
Original comment by Floris Bruynooghe (BitBucket: flub, GitHub: flub): Would it be very wrong to only support a new |
Original comment by Bruno Oliveira (BitBucket: nicoddemus, GitHub: nicoddemus): I wouldn't mind very much about Anyway, I miss this functionality too and would love to have it built-in, having turned to |
proposal; with pytest.raises.matching(ValueError, '.*Regex'):
... |
the builtin solution should modify the context manager and use the regex condition based on the value of the regex attribute |
I was almost tempted to use |
Probably a mixture between "nobody has looked at it yet" and "there is a plugin for it" and "it's easy enough to do without pytest support": with pytest.raises(ValueError) as excinfo:
func()
assert re.match(pattern, str(excinfo.value)) But since many people have said they'd appreciate such a feature, and nobody seems to have had time yet, I'm sure a contribution would we very welcome! |
I could tackle this, but would like to know what is the preferred interface:
What do you guys think? |
(Just posted a tweet about this). |
Another proposal from @hpk42 was to add a keyword argument to with pytest.raises(ValueError, regex=r'...'):
... That has the downside of being only usable in context manager form. I'd still prefer this form, though, as I don't think the non-contextmanager form is used widely anyways.
|
I would also prefer @hpk42 ’s proposal. But in this case you'd have to check if the second argument is a non-keyword argument, which would mean that it is the function to be called. In that case, the This seems a little dirty to me, but on the other hand, it would also make it possible to add additional keyword arguments like # "regex" as argument for "myfunc":
pytest.raises(ValueError, myfunc, regex=r'spam')
# "regex" as argument for "raises":
pytest.raises(
ValueError,
regex=r'spam',
func=myfunc,
args=(23, 'foo'),
kwargs={'spam': 42}) |
Thanks for the reminder @The-Compiler! We can call this option 3, if people want to reference the options by number: with pytest.raises(ValueError, regex=r'...'):
function_to_test()
# no functional form supported Initially I liked that proposal, but later I realized that having a special case where suddenly adding a keyword argument breaks the functional form would be surprising to people. Having this inconsistency doesn't seem too appealing to me, and we have other proposals that fit the requirements just as well. Proposal 1 ( Personally I think 2 ( So in the end, my vote would go to 1, with 2 coming as a very close second. |
Since the oldest supported python version is now 2.6 I don't think supporting the functional-style is very important anymore. Using I'm not sure why I think out of the existing proposals I probably like the keyword option most, closely followed by And just to add to the confusion, I'll add another proposal:
(the |
What changes? Wouldn't the plugin just be blacklisted and the functionality works just as before?
The problem is that the signature is
If we now add a fourth one (but only in the context manager form!), that's going to be... a bad idea.
I like that! 👍 to both. |
Oh my! I love this. 😍
I see only benefits! To me this proposal wins hands down. 😄 |
I like this one, although it's a bit crufty:
This looks a bit better:
|
lets add a match method to execinfo after pulling py.code into pytest, then we can pretty much pick the external facade afterwards |
I prefer the |
with the match method inside of the excinfo implementing all other convenience variants is trivial |
👍 on implementing |
Adding |
fixed in c578226 |
Originally reported by: György Kiss (BitBucket: kissgyorgy, GitHub: kissgyorgy)
I used assertRaisesRegexp in
unittest
and I miss it frompytest
. As I don't want to useunittest
anymore :D I wrote a context manager:Usage:
I think others could benefit from something like this and would be nice to have it in pytest!
The text was updated successfully, but these errors were encountered: