-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
assertRaises(Regex)?(Exception)
is problematic
#106300
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
Comments
And I found a real bug hidden under This test cpython/Lib/test/test_unittest/test_runner.py Lines 389 to 404 in 04dfc6f
with self.assertRaises(Exception) as e:
TestableTest.doClassCleanups()
self.assertEqual(e, 'cleanup1') Reader might expect that I will fix |
Nice catch. Could you please fix also cases with |
Yes, sure. Is it better to include |
Up to you. |
…ythonGH-106302) (cherry picked from commit 6e6a4cd)
…ests (pythonGH-106302). (cherry picked from commit 6e6a4cd) Co-authored-by: Nikita Sobolev <[email protected]>
…ests (pythonGH-106302). (cherry picked from commit 6e6a4cd) Co-authored-by: Nikita Sobolev <[email protected]>
…H-106302). (GH-106545) (cherry picked from commit 6e6a4cd) Co-authored-by: Nikita Sobolev <[email protected]>
…6737) Use a custom exception to prevent unintentional silence of actual errors.
…ythonGH-106737) Use a custom exception to prevent unintentional silence of actual errors. (cherry picked from commit fd9d70a) Co-authored-by: Nikita Sobolev <[email protected]>
…ythonGH-106737) Use a custom exception to prevent unintentional silence of actual errors. (cherry picked from commit fd9d70a) Co-authored-by: Nikita Sobolev <[email protected]>
…GH-106737) (GH-108007) Use a custom exception to prevent unintentional silence of actual errors. (cherry picked from commit fd9d70a) Co-authored-by: Nikita Sobolev <[email protected]>
…GH-106737) (#108006) gh-106300: Improve errors testing in test_unittest.test_runner (GH-106737) Use a custom exception to prevent unintentional silence of actual errors. (cherry picked from commit fd9d70a) Co-authored-by: Nikita Sobolev <[email protected]>
Thanks for the fixes! |
assertRaises(Exception)
is problematic, because it does not really test anything.For example, here's the test code from
test_mailbox.py
:self.assertRaises(Exception)
can happen for any other reason, we have zero confidence in the fact that we have the right exception. For example, a simple typo inemail.message_from_stringX
will also satisfy this check.Sometimes, it is a bit better when
assertRaisesRegex
is used, then we have at least have a message to compare and be at least partially sure that the exception is correct. But, it is still not quite good, because we don't know the type of error that happen and it might accidentally change.There are multiple places where this pattern is used:
I suggest to:
class MyExc(Exception): ...
, this way we will have 100% guarantee that the test is accurateassertRaisesRegex
more, where possibleassertRaises(Exception)
where it is legit, for exampletest_ordereddict
says:Note, the exact exception raised is not guaranteed. The only guarantee that the next() will not succeed
as exc
is used and there areisinstance
checks - then we can keep it as isException
class is tested excplicitly, like intest_yield_from
withg.throw(Exception)
I will send a PR with the fix.
Linked PRs
assertRaises(Exception)
usages in tests #106302assertRaises(Exception)
usages in tests (GH-106302) #106534assertRaises(Exception)
usages in tests (GH-106302). #106545assertRaises(Exception)
usage intest_runner
#106737The text was updated successfully, but these errors were encountered: