2018-10-25 15:01:29 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-12-14 07:28:07 +08:00
|
|
|
import sys
|
2018-08-05 02:14:00 +08:00
|
|
|
from functools import wraps
|
|
|
|
|
2016-12-14 07:28:07 +08:00
|
|
|
import pytest
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.compat import _PytestWrapper
|
|
|
|
from _pytest.compat import get_real_func
|
|
|
|
from _pytest.compat import is_generator
|
|
|
|
from _pytest.compat import safe_getattr
|
2018-11-01 07:54:48 +08:00
|
|
|
from _pytest.compat import safe_isclass
|
2017-08-21 22:28:29 +08:00
|
|
|
from _pytest.outcomes import OutcomeException
|
2016-12-11 23:59:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_is_generator():
|
|
|
|
def zap():
|
2019-03-23 00:16:08 +08:00
|
|
|
yield # pragma: no cover
|
2016-12-11 23:59:11 +08:00
|
|
|
|
|
|
|
def foo():
|
2019-03-23 00:16:08 +08:00
|
|
|
pass # pragma: no cover
|
2016-12-11 23:59:11 +08:00
|
|
|
|
|
|
|
assert is_generator(zap)
|
|
|
|
assert not is_generator(foo)
|
2016-12-14 07:28:07 +08:00
|
|
|
|
|
|
|
|
2017-01-19 18:38:15 +08:00
|
|
|
def test_real_func_loop_limit():
|
|
|
|
class Evil(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.left = 1000
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Evil left={left}>".format(left=self.left)
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
if not self.left:
|
2019-03-23 00:16:08 +08:00
|
|
|
raise RuntimeError("it's over") # pragma: no cover
|
2017-01-19 18:38:15 +08:00
|
|
|
self.left -= 1
|
|
|
|
return self
|
|
|
|
|
|
|
|
evil = Evil()
|
|
|
|
|
2019-03-23 00:16:08 +08:00
|
|
|
with pytest.raises(
|
|
|
|
ValueError,
|
|
|
|
match=(
|
|
|
|
"could not find real function of <Evil left=800>\n"
|
|
|
|
"stopped at <Evil left=800>"
|
|
|
|
),
|
|
|
|
):
|
|
|
|
get_real_func(evil)
|
2017-01-19 18:38:15 +08:00
|
|
|
|
|
|
|
|
2018-08-05 02:14:00 +08:00
|
|
|
def test_get_real_func():
|
|
|
|
"""Check that get_real_func correctly unwraps decorators until reaching the real function"""
|
|
|
|
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def inner():
|
2019-03-23 00:16:08 +08:00
|
|
|
pass # pragma: no cover
|
2018-08-05 02:14:00 +08:00
|
|
|
|
|
|
|
return inner
|
|
|
|
|
|
|
|
def func():
|
2019-03-23 00:16:08 +08:00
|
|
|
pass # pragma: no cover
|
2018-08-05 02:14:00 +08:00
|
|
|
|
|
|
|
wrapped_func = decorator(decorator(func))
|
|
|
|
assert get_real_func(wrapped_func) is func
|
|
|
|
|
|
|
|
wrapped_func2 = decorator(decorator(wrapped_func))
|
|
|
|
assert get_real_func(wrapped_func2) is func
|
|
|
|
|
|
|
|
# special case for __pytest_wrapped__ attribute: used to obtain the function up until the point
|
|
|
|
# a function was wrapped by pytest itself
|
2018-08-09 20:20:37 +08:00
|
|
|
wrapped_func2.__pytest_wrapped__ = _PytestWrapper(wrapped_func)
|
2018-08-05 02:14:00 +08:00
|
|
|
assert get_real_func(wrapped_func2) is wrapped_func
|
|
|
|
|
|
|
|
|
2016-12-14 07:28:07 +08:00
|
|
|
def test_is_generator_asyncio(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-12-14 07:28:07 +08:00
|
|
|
from _pytest.compat import is_generator
|
|
|
|
import asyncio
|
|
|
|
@asyncio.coroutine
|
|
|
|
def baz():
|
|
|
|
yield from [1,2,3]
|
|
|
|
|
|
|
|
def test_is_generator_asyncio():
|
|
|
|
assert not is_generator(baz)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-01-19 18:38:15 +08:00
|
|
|
# avoid importing asyncio into pytest's own process,
|
|
|
|
# which in turn imports logging (#8)
|
2016-12-14 07:54:20 +08:00
|
|
|
result = testdir.runpytest_subprocess()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2016-12-14 07:28:07 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.version_info < (3, 5), reason="async syntax available in Python 3.5+"
|
|
|
|
)
|
2016-12-14 07:28:07 +08:00
|
|
|
def test_is_generator_async_syntax(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-12-14 07:28:07 +08:00
|
|
|
from _pytest.compat import is_generator
|
|
|
|
def test_is_generator_py35():
|
|
|
|
async def foo():
|
|
|
|
await foo()
|
|
|
|
|
|
|
|
async def bar():
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert not is_generator(foo)
|
|
|
|
assert not is_generator(bar)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-12-14 07:28:07 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2017-08-21 22:28:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ErrorsHelper(object):
|
|
|
|
@property
|
|
|
|
def raise_exception(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
raise Exception("exception should be catched")
|
2017-08-21 22:28:29 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def raise_fail(self):
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest.fail("fail should be catched")
|
2017-08-21 22:28:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_helper_failures():
|
|
|
|
helper = ErrorsHelper()
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
helper.raise_exception
|
|
|
|
with pytest.raises(OutcomeException):
|
|
|
|
helper.raise_fail
|
|
|
|
|
|
|
|
|
|
|
|
def test_safe_getattr():
|
|
|
|
helper = ErrorsHelper()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert safe_getattr(helper, "raise_exception", "default") == "default"
|
|
|
|
assert safe_getattr(helper, "raise_fail", "default") == "default"
|
2018-11-01 07:54:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_safe_isclass():
|
|
|
|
assert safe_isclass(type) is True
|
|
|
|
|
|
|
|
class CrappyClass(Exception):
|
|
|
|
@property
|
|
|
|
def __class__(self):
|
|
|
|
assert False, "Should be ignored"
|
|
|
|
|
|
|
|
assert safe_isclass(CrappyClass()) is False
|