-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add B017 - detection for an evil form of assertRaises (#163)
* Add B017 - detection for a bad form of assertRaises ```with assertRaises(Exception):``` is basically a "catch 'em all" assert that casts far too broad of a net when it comes to detecting failures in code being tested. Assertions should be testing specific failure cases, not "did Python throw /any/ type of error?", and so the context manager form, or the assertRaisesRegex form are far better to use. * Amend documentation, revert version change
- Loading branch information
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
""" | ||
Should emit: | ||
B017 - on lines 10 | ||
""" | ||
import unittest | ||
|
||
|
||
class Foobar(unittest.TestCase): | ||
def evil_raises(self) -> None: | ||
with self.assertRaises(Exception): | ||
raise Exception("Evil I say!") | ||
|
||
def context_manager_raises(self) -> None: | ||
with self.assertRaises(Exception) as ex: | ||
raise Exception("Context manager is good") | ||
self.assertEqual("Context manager is good", str(ex.exception)) | ||
|
||
def regex_raises(self) -> None: | ||
with self.assertRaisesRegex(Exception, "Regex is good"): | ||
raise Exception("Regex is good") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters