-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add B908: Rule to check that assertRaises-like contexts has no more t…
…han 1 top-level statements (#382) * Add B908: Rule to check that assertRaises-like contexts has no more than 1 top-level statement * Update README with B908 * Catch "with raises" and "with warns" * Update README * Fix typing * Add "assertWarnsRegexp" to B908_unittest_methods * Update README.rst Co-authored-by: Cooper Lees <me@cooperlees.com> * Remove assertWarnsRegexp * Remove old comment --------- Co-authored-by: Alexey Nikitin <alexeynikitin@swatmobility.com> Co-authored-by: Cooper Lees <me@cooperlees.com>
- Loading branch information
1 parent
8c0e7eb
commit d752c44
Showing
4 changed files
with
124 additions
and
2 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,57 @@ | ||
import unittest | ||
import warnings | ||
|
||
import pytest | ||
from pytest import raises, warns | ||
|
||
with pytest.raises(TypeError): | ||
a = 1 + "x" | ||
b = "x" + 1 | ||
print(a, b) | ||
|
||
|
||
class SomeTestCase(unittest.TestCase): | ||
def test_func_raises(self): | ||
with self.assertRaises(TypeError): | ||
a = 1 + "x" | ||
b = "x" + 1 | ||
print(a, b) | ||
|
||
def test_func_raises_regex(self): | ||
with self.assertRaisesRegex(TypeError): | ||
a = 1 + "x" | ||
b = "x" + 1 | ||
print(a, b) | ||
|
||
def test_func_raises_regexp(self): | ||
with self.assertRaisesRegexp(TypeError): | ||
a = 1 + "x" | ||
b = "x" + 1 | ||
print(a, b) | ||
|
||
def test_raises_correct(self): | ||
with self.assertRaises(TypeError): | ||
print("1" + 1) | ||
|
||
|
||
with raises(Exception): | ||
"1" + 1 | ||
"2" + 2 | ||
|
||
with pytest.warns(Warning): | ||
print("print before warning") | ||
warnings.warn("some warning", stacklevel=1) | ||
|
||
with warns(Warning): | ||
print("print before warning") | ||
warnings.warn("some warning", stacklevel=1) | ||
|
||
# should not raise an error | ||
with pytest.raises(TypeError): | ||
print("1" + 1) | ||
|
||
with pytest.warns(Warning): | ||
warnings.warn("some warning", stacklevel=1) | ||
|
||
with raises(Exception): | ||
raise Exception("some exception") |
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