summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoofar <toofar@spalge.com>2023-01-23 19:53:35 +1300
committertoofar <toofar@spalge.com>2023-01-23 19:53:35 +1300
commitb3caf7eeeb7bdc0364af4b471e2bdfb8834c33a1 (patch)
tree778c3f3f9a2b4090fb82ac181375792767884496
parent62245cbd98bbeae8a033336897537e8514a600f8 (diff)
downloadqutebrowser-b3caf7eeeb7bdc0364af4b471e2bdfb8834c33a1.tar.gz
qutebrowser-b3caf7eeeb7bdc0364af4b471e2bdfb8834c33a1.zip
Remove some broad exception assertions
Flake8-bugbear points out some places where we use pytest.raises(Exception). That can swallow errors with the tests that aren't the intended ones. I most cases we are also passing match="some-regex" to limit the exceptions that it matches against. There is one case where that didn't look easy to do so I subclassed exception. ref: #7553
-rw-r--r--tests/unit/utils/test_utils.py20
1 files changed, 12 insertions, 8 deletions
diff --git a/tests/unit/utils/test_utils.py b/tests/unit/utils/test_utils.py
index 47b03061e..86b5efb8c 100644
--- a/tests/unit/utils/test_utils.py
+++ b/tests/unit/utils/test_utils.py
@@ -448,7 +448,7 @@ class TestPreventExceptions:
@utils.prevent_exceptions(42, True)
def func_predicate_true(self):
- raise Exception
+ raise Exception("its-true")
def test_predicate_true(self, caplog):
"""Test with a True predicate."""
@@ -459,12 +459,12 @@ class TestPreventExceptions:
@utils.prevent_exceptions(42, False)
def func_predicate_false(self):
- raise Exception
+ raise Exception("its-false")
def test_predicate_false(self, caplog):
"""Test with a False predicate."""
with caplog.at_level(logging.ERROR, 'misc'):
- with pytest.raises(Exception):
+ with pytest.raises(Exception, match="its-false"):
self.func_predicate_false()
assert not caplog.records
@@ -546,13 +546,17 @@ class TestIsEnum:
assert not utils.is_enum(23)
+class SentinalException(Exception):
+ pass
+
+
class TestRaises:
"""Test raises."""
def do_raise(self):
"""Helper function which raises an exception."""
- raise Exception
+ raise SentinalException
def do_nothing(self):
"""Helper function which does nothing."""
@@ -571,15 +575,15 @@ class TestRaises:
def test_no_args_true(self):
"""Test with no args and an exception which gets raised."""
- assert utils.raises(Exception, self.do_raise)
+ assert utils.raises(SentinalException, self.do_raise)
def test_no_args_false(self):
"""Test with no args and an exception which does not get raised."""
- assert not utils.raises(Exception, self.do_nothing)
+ assert not utils.raises(SentinalException, self.do_nothing)
def test_unrelated_exception(self):
"""Test with an unrelated exception."""
- with pytest.raises(Exception):
+ with pytest.raises(SentinalException):
utils.raises(ValueError, self.do_raise)
@@ -891,7 +895,7 @@ def test_ceil_log_hypothesis(number, base):
@pytest.mark.parametrize('number, base', [(64, 0), (0, 64), (64, -1),
(-1, 64), (1, 1)])
def test_ceil_log_invalid(number, base):
- with pytest.raises(Exception): # ValueError/ZeroDivisionError
+ with pytest.raises((ValueError, ZeroDivisionError)):
math.log(number, base)
with pytest.raises(ValueError):
utils.ceil_log(number, base)