2018-04-06 20:16:12 +08:00
|
|
|
from _pytest.outcomes import Failed
|
2012-11-02 23:04:57 +08:00
|
|
|
import pytest
|
2016-11-09 07:34:45 +08:00
|
|
|
import sys
|
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestRaises(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def test_raises(self):
|
|
|
|
source = "int('qwe')"
|
|
|
|
excinfo = pytest.raises(ValueError, source)
|
|
|
|
code = excinfo.traceback[-1].frame.code
|
|
|
|
s = str(code.fullsource)
|
|
|
|
assert s == source
|
|
|
|
|
|
|
|
def test_raises_exec(self):
|
|
|
|
pytest.raises(ValueError, "a,x = []")
|
|
|
|
|
|
|
|
def test_raises_syntax_error(self):
|
|
|
|
pytest.raises(SyntaxError, "qwe qwe qwe")
|
|
|
|
|
|
|
|
def test_raises_function(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest.raises(ValueError, int, "hello")
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_raises_callable_no_exception(self):
|
2017-02-17 02:41:51 +08:00
|
|
|
class A(object):
|
2012-11-02 23:04:57 +08:00
|
|
|
def __call__(self):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2012-11-02 23:04:57 +08:00
|
|
|
try:
|
|
|
|
pytest.raises(ValueError, A())
|
|
|
|
except pytest.raises.Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_raises_as_contextmanager(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2018-07-08 23:35:53 +08:00
|
|
|
import pytest
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_simple():
|
|
|
|
with pytest.raises(ZeroDivisionError) as excinfo:
|
2015-11-27 22:43:01 +08:00
|
|
|
assert isinstance(excinfo, _pytest._code.ExceptionInfo)
|
2012-11-02 23:04:57 +08:00
|
|
|
1/0
|
|
|
|
print (excinfo)
|
|
|
|
assert excinfo.type == ZeroDivisionError
|
2015-06-19 08:04:47 +08:00
|
|
|
assert isinstance(excinfo.value, ZeroDivisionError)
|
2012-11-02 23:04:57 +08:00
|
|
|
|
|
|
|
def test_noraise():
|
|
|
|
with pytest.raises(pytest.raises.Exception):
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
int()
|
|
|
|
|
|
|
|
def test_raise_wrong_exception_passes_by():
|
|
|
|
with pytest.raises(ZeroDivisionError):
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
1/0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-11-02 23:04:57 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*3 passed*"])
|
2012-11-02 23:04:57 +08:00
|
|
|
|
2014-04-15 06:09:10 +08:00
|
|
|
def test_noclass(self):
|
|
|
|
with pytest.raises(TypeError):
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest.raises("wrong", lambda: None)
|
2014-04-15 06:09:10 +08:00
|
|
|
|
2018-03-28 10:57:15 +08:00
|
|
|
def test_invalid_arguments_to_raises(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
with pytest.raises(TypeError, match="unknown"):
|
|
|
|
with pytest.raises(TypeError, unknown="bogus"):
|
2018-03-28 10:57:15 +08:00
|
|
|
raise ValueError()
|
|
|
|
|
2014-04-15 06:09:10 +08:00
|
|
|
def test_tuple(self):
|
|
|
|
with pytest.raises((KeyError, ValueError)):
|
2018-05-23 22:48:46 +08:00
|
|
|
raise KeyError("oops")
|
2016-02-03 17:01:03 +08:00
|
|
|
|
|
|
|
def test_no_raise_message(self):
|
|
|
|
try:
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest.raises(ValueError, int, "0")
|
2016-02-03 17:01:03 +08:00
|
|
|
except pytest.raises.Exception as e:
|
2018-05-18 05:31:16 +08:00
|
|
|
assert e.msg == "DID NOT RAISE {}".format(repr(ValueError))
|
2016-06-20 02:24:47 +08:00
|
|
|
else:
|
|
|
|
assert False, "Expected pytest.raises.Exception"
|
|
|
|
|
2016-06-17 01:15:32 +08:00
|
|
|
try:
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
pass
|
|
|
|
except pytest.raises.Exception as e:
|
2018-05-18 05:31:16 +08:00
|
|
|
assert e.msg == "DID NOT RAISE {}".format(repr(ValueError))
|
2016-06-20 02:24:47 +08:00
|
|
|
else:
|
|
|
|
assert False, "Expected pytest.raises.Exception"
|
2016-06-17 01:15:32 +08:00
|
|
|
|
2016-06-21 00:44:34 +08:00
|
|
|
def test_custom_raise_message(self):
|
2016-06-17 01:15:32 +08:00
|
|
|
message = "TEST_MESSAGE"
|
|
|
|
try:
|
|
|
|
with pytest.raises(ValueError, message=message):
|
|
|
|
pass
|
|
|
|
except pytest.raises.Exception as e:
|
2016-06-17 02:09:15 +08:00
|
|
|
assert e.msg == message
|
2016-06-20 02:24:47 +08:00
|
|
|
else:
|
|
|
|
assert False, "Expected pytest.raises.Exception"
|
2016-10-18 07:22:53 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("method", ["function", "with"])
|
2016-11-09 07:34:45 +08:00
|
|
|
def test_raises_cyclic_reference(self, method):
|
|
|
|
"""
|
|
|
|
Ensure pytest.raises does not leave a reference cycle (#1965).
|
|
|
|
"""
|
|
|
|
import gc
|
2016-10-18 07:22:53 +08:00
|
|
|
|
2016-11-09 07:34:45 +08:00
|
|
|
class T(object):
|
2016-10-18 07:22:53 +08:00
|
|
|
def __call__(self):
|
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
t = T()
|
2018-05-23 22:48:46 +08:00
|
|
|
if method == "function":
|
2016-10-18 07:22:53 +08:00
|
|
|
pytest.raises(ValueError, t)
|
|
|
|
else:
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
t()
|
|
|
|
|
2016-11-09 07:34:45 +08:00
|
|
|
# ensure both forms of pytest.raises don't leave exceptions in sys.exc_info()
|
|
|
|
assert sys.exc_info() == (None, None, None)
|
|
|
|
|
|
|
|
del t
|
|
|
|
|
|
|
|
# ensure the t instance is not stuck in a cyclic reference
|
|
|
|
for o in gc.get_objects():
|
|
|
|
assert type(o) is not T
|
|
|
|
|
2017-02-01 20:37:13 +08:00
|
|
|
def test_raises_match(self):
|
|
|
|
msg = r"with base \d+"
|
|
|
|
with pytest.raises(ValueError, match=msg):
|
2018-05-23 22:48:46 +08:00
|
|
|
int("asdf")
|
2017-02-01 20:37:13 +08:00
|
|
|
|
|
|
|
msg = "with base 10"
|
|
|
|
with pytest.raises(ValueError, match=msg):
|
2018-05-23 22:48:46 +08:00
|
|
|
int("asdf")
|
2017-02-01 20:37:13 +08:00
|
|
|
|
|
|
|
msg = "with base 16"
|
2018-05-23 22:48:46 +08:00
|
|
|
expr = r"Pattern '{}' not found in 'invalid literal for int\(\) with base 10: 'asdf''".format(
|
|
|
|
msg
|
|
|
|
)
|
2017-02-01 20:37:13 +08:00
|
|
|
with pytest.raises(AssertionError, match=expr):
|
|
|
|
with pytest.raises(ValueError, match=msg):
|
2018-05-23 22:48:46 +08:00
|
|
|
int("asdf", base=10)
|
2018-02-15 19:11:56 +08:00
|
|
|
|
|
|
|
def test_raises_match_wrong_type(self):
|
|
|
|
"""Raising an exception with the wrong type and match= given.
|
|
|
|
|
|
|
|
pytest should throw the unexpected exception - the pattern match is not
|
|
|
|
really relevant if we got a different exception.
|
|
|
|
"""
|
|
|
|
with pytest.raises(ValueError):
|
2018-05-23 22:48:46 +08:00
|
|
|
with pytest.raises(IndexError, match="nomatch"):
|
|
|
|
int("asdf")
|
2018-04-06 20:16:12 +08:00
|
|
|
|
|
|
|
def test_raises_exception_looks_iterable(self):
|
|
|
|
from six import add_metaclass
|
|
|
|
|
|
|
|
class Meta(type(object)):
|
|
|
|
def __getitem__(self, item):
|
2018-05-23 22:48:46 +08:00
|
|
|
return 1 / 0
|
2018-04-06 20:16:12 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
@add_metaclass(Meta)
|
|
|
|
class ClassLooksIterableException(Exception):
|
|
|
|
pass
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
with pytest.raises(
|
|
|
|
Failed, match="DID NOT RAISE <class 'raises.ClassLooksIterableException'>"
|
|
|
|
):
|
2018-04-06 20:16:12 +08:00
|
|
|
pytest.raises(ClassLooksIterableException, lambda: None)
|