You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On the other hand, in our own test code this is a very popular pattern.
I would vote for direct string compare, instead of escaped regex match (if this feature is accepted).
I think that adding performance numbers here can help: how fast it is to run a test case with self.assertRaisesRegex(re.escape(...)) and how fast it is to run the same with direct string compare. It might be helpful when there are a lot of such tests.
I'm also of the same mind. What I could however suggest is that we extend the unittest.TestCase class by adding test.support.TestCase with some methods like that. It would help us keep the same syntax and we won't need to have to pass self to the function.
To me it probably would be most natural to have only single method assertRaises(exc, expected_string=None, regex=False, callback=None). I quite like APIs where by default it is not regex, but there is a flag for it, such as ripgrep.
However, there is an issue of callback with full *args and **kwds mixed up into the signature.
I used to use similar patterns in the past, but don't do this anymore, instead just have callback argument and if I need args or kwds, I just use partial, otherwise it makes extensions difficult, such as this case.
regex-convenient: 0.4 s
regex-manual: 0.24 s
string: 0.17 s
change-convenient: -57%
change-manual: -29%
Relative difference is non-trivial, however aggregate impact is small. Could have a slight impact far far in the future, but now it is tiny. CPython - 2041 hits of assertRaisesRegex.
I am +1 for convenience. I think it is useful to have this for CPython tests.
And +0.1 on implementation. Alternatively, could just add 3 lines of Python, but I am still slightly biased towards not computing more than needed given it is a simple addition.
Usually, it is enough to only test that some words or sequence of words are occurred in the error message, just to differentiate it from other errors with the same type. Such tests are less fragile, you can slightly rewrite the error message without changing tests.
If you need to check the exact error message, it may be better to write it as:
Activity
sobolevn commentedon Aug 15, 2024
I am a bit sceptical about adding features that only take 3 lines of code to implement in your own code (from your example):
On the other hand, in our own test code this is a very popular pattern.
I would vote for direct string compare, instead of escaped regex match (if this feature is accepted).
I think that adding performance numbers here can help: how fast it is to run a test case with
self.assertRaisesRegex(re.escape(...))and how fast it is to run the same with direct string compare. It might be helpful when there are a lot of such tests.picnixz commentedon Aug 15, 2024
I'm also of the same mind. What I could however suggest is that we extend the
unittest.TestCaseclass by addingtest.support.TestCasewith some methods like that. It would help us keep the same syntax and we won't need to have to passselfto the function.dg-pb commentedon Aug 15, 2024
To me it probably would be most natural to have only single method
assertRaises(exc, expected_string=None, regex=False, callback=None). I quite like APIs where by default it is notregex, but there is a flag for it, such asripgrep.However, there is an issue of callback with full
*argsand**kwdsmixed up into the signature.I used to use similar patterns in the past, but don't do this anymore, instead just have
callbackargument and if I needargsorkwds, I just usepartial, otherwise it makes extensions difficult, such as this case.serhiy-storchaka commentedon Aug 15, 2024
There are many more required unittest features on the list. I am skeptical about this one.
dg-pb commentedon Aug 15, 2024
Benchmark results (100_000 runs):
regex-convenient: 0.4 s
regex-manual: 0.24 s
string: 0.17 s
change-convenient: -57%
change-manual: -29%
Relative difference is non-trivial, however aggregate impact is small. Could have a slight impact far far in the future, but now it is tiny. CPython - 2041 hits of
assertRaisesRegex.I am +1 for convenience. I think it is useful to have this for CPython tests.
And +0.1 on implementation. Alternatively, could just add 3 lines of Python, but I am still slightly biased towards not computing more than needed given it is a simple addition.
serhiy-storchaka commentedon Aug 16, 2024
Usually, it is enough to only test that some words or sequence of words are occurred in the error message, just to differentiate it from other errors with the same type. Such tests are less fragile, you can slightly rewrite the error message without changing tests.
If you need to check the exact error message, it may be better to write it as:
This works also for warnings, and you can test also other attributes, for example the stack level for warnings.
dg-pb commentedon Aug 16, 2024
This is sensible. As long as it is clear that this is the recommended pattern for it, I think this is sufficient.
If everyone is happy with this, I am closing this issue and PR.
dg-pb commentedon Aug 16, 2024
.