2020-06-13 22:29:01 +08:00
|
|
|
import enum
|
2022-02-11 23:20:42 +08:00
|
|
|
import sys
|
2023-07-01 05:55:42 +08:00
|
|
|
from functools import cached_property
|
2019-08-06 22:02:46 +08:00
|
|
|
from functools import partial
|
2018-08-05 02:14:00 +08:00
|
|
|
from functools import wraps
|
2020-10-03 03:09:56 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2020-06-13 22:29:01 +08:00
|
|
|
from typing import Union
|
2018-08-05 02:14:00 +08:00
|
|
|
|
2016-12-14 07:28:07 +08:00
|
|
|
import pytest
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.compat import _PytestWrapper
|
2020-06-13 22:29:01 +08:00
|
|
|
from _pytest.compat import assert_never
|
2018-10-25 15:01:29 +08:00
|
|
|
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
|
2020-10-29 15:56:34 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2016-12-11 23:59:11 +08:00
|
|
|
|
2020-06-13 22:29:01 +08:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from typing_extensions import Literal
|
|
|
|
|
2016-12-11 23:59:11 +08:00
|
|
|
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_is_generator() -> None:
|
2016-12-11 23:59:11 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_real_func_loop_limit() -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class Evil:
|
2017-01-19 18:38:15 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.left = 1000
|
|
|
|
|
|
|
|
def __repr__(self):
|
2020-10-03 04:16:22 +08:00
|
|
|
return f"<Evil left={self.left}>"
|
2017-01-19 18:38:15 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_get_real_func() -> None:
|
2018-08-05 02:14:00 +08:00
|
|
|
"""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
|
|
|
|
|
|
|
|
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_get_real_func_partial() -> None:
|
2019-08-06 22:02:46 +08:00
|
|
|
"""Test get_real_func handles partial instances correctly"""
|
|
|
|
|
|
|
|
def foo(x):
|
|
|
|
return x
|
|
|
|
|
|
|
|
assert get_real_func(foo) is foo
|
|
|
|
assert get_real_func(partial(foo)) is foo
|
|
|
|
|
|
|
|
|
2022-02-11 23:20:42 +08:00
|
|
|
@pytest.mark.skipif(sys.version_info >= (3, 11), reason="couroutine removed")
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_is_generator_asyncio(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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)
|
2020-10-29 15:56:34 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2016-12-14 07:28:07 +08:00
|
|
|
|
|
|
|
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_is_generator_async_syntax(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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)
|
2019-08-13 04:58:10 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:56:34 +08:00
|
|
|
result = pytester.runpytest()
|
2019-08-13 04:58:10 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
|
|
|
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_is_generator_async_gen_syntax(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-08-13 04:58:10 +08:00
|
|
|
"""
|
|
|
|
from _pytest.compat import is_generator
|
2021-12-30 18:37:18 +08:00
|
|
|
def test_is_generator():
|
2019-08-13 04:58:10 +08:00
|
|
|
async def foo():
|
|
|
|
yield
|
|
|
|
await foo()
|
|
|
|
|
|
|
|
async def bar():
|
|
|
|
yield
|
|
|
|
|
|
|
|
assert not is_generator(foo)
|
|
|
|
assert not is_generator(bar)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-10-29 15:56:34 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2017-08-21 22:28:29 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class ErrorsHelper:
|
2020-01-15 17:08:30 +08:00
|
|
|
@property
|
|
|
|
def raise_baseexception(self):
|
|
|
|
raise BaseException("base exception should be raised")
|
|
|
|
|
2017-08-21 22:28:29 +08:00
|
|
|
@property
|
|
|
|
def raise_exception(self):
|
2021-12-27 20:23:15 +08:00
|
|
|
raise Exception("exception should be caught")
|
2017-08-21 22:28:29 +08:00
|
|
|
|
|
|
|
@property
|
2020-01-15 17:08:30 +08:00
|
|
|
def raise_fail_outcome(self):
|
2021-12-27 20:23:15 +08:00
|
|
|
pytest.fail("fail should be caught")
|
2017-08-21 22:28:29 +08:00
|
|
|
|
|
|
|
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_helper_failures() -> None:
|
2017-08-21 22:28:29 +08:00
|
|
|
helper = ErrorsHelper()
|
|
|
|
with pytest.raises(Exception):
|
|
|
|
helper.raise_exception
|
|
|
|
with pytest.raises(OutcomeException):
|
2020-01-15 17:08:30 +08:00
|
|
|
helper.raise_fail_outcome
|
2017-08-21 22:28:29 +08:00
|
|
|
|
|
|
|
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_safe_getattr() -> None:
|
2017-08-21 22:28:29 +08:00
|
|
|
helper = ErrorsHelper()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert safe_getattr(helper, "raise_exception", "default") == "default"
|
2020-01-15 17:08:30 +08:00
|
|
|
assert safe_getattr(helper, "raise_fail_outcome", "default") == "default"
|
|
|
|
with pytest.raises(BaseException):
|
|
|
|
assert safe_getattr(helper, "raise_baseexception", "default")
|
2018-11-01 07:54:48 +08:00
|
|
|
|
|
|
|
|
2020-10-29 15:56:34 +08:00
|
|
|
def test_safe_isclass() -> None:
|
2018-11-01 07:54:48 +08:00
|
|
|
assert safe_isclass(type) is True
|
|
|
|
|
|
|
|
class CrappyClass(Exception):
|
2019-07-08 15:04:19 +08:00
|
|
|
# Type ignored because it's bypassed intentionally.
|
|
|
|
@property # type: ignore
|
2018-11-01 07:54:48 +08:00
|
|
|
def __class__(self):
|
|
|
|
assert False, "Should be ignored"
|
|
|
|
|
|
|
|
assert safe_isclass(CrappyClass()) is False
|
2019-11-06 16:24:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_cached_property() -> None:
|
|
|
|
ncalls = 0
|
|
|
|
|
|
|
|
class Class:
|
|
|
|
@cached_property
|
|
|
|
def prop(self) -> int:
|
|
|
|
nonlocal ncalls
|
|
|
|
ncalls += 1
|
|
|
|
return ncalls
|
|
|
|
|
|
|
|
c1 = Class()
|
|
|
|
assert ncalls == 0
|
|
|
|
assert c1.prop == 1
|
|
|
|
assert c1.prop == 1
|
|
|
|
c2 = Class()
|
|
|
|
assert ncalls == 1
|
|
|
|
assert c2.prop == 2
|
|
|
|
assert c1.prop == 1
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_assert_never_union() -> None:
|
2020-10-06 09:13:05 +08:00
|
|
|
x: Union[int, str] = 10
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
if isinstance(x, int):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
assert_never(x) # type: ignore[arg-type]
|
|
|
|
|
|
|
|
if isinstance(x, int):
|
|
|
|
pass
|
|
|
|
elif isinstance(x, str):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert_never(x)
|
|
|
|
|
|
|
|
|
|
|
|
def test_assert_never_enum() -> None:
|
|
|
|
E = enum.Enum("E", "a b")
|
2020-10-06 09:13:05 +08:00
|
|
|
x: E = E.a
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
if x is E.a:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
assert_never(x) # type: ignore[arg-type]
|
|
|
|
|
|
|
|
if x is E.a:
|
|
|
|
pass
|
|
|
|
elif x is E.b:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert_never(x)
|
|
|
|
|
|
|
|
|
|
|
|
def test_assert_never_literal() -> None:
|
2020-10-06 09:13:05 +08:00
|
|
|
x: Literal["a", "b"] = "a"
|
2020-06-13 22:29:01 +08:00
|
|
|
|
|
|
|
if x == "a":
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
with pytest.raises(AssertionError):
|
|
|
|
assert_never(x) # type: ignore[arg-type]
|
|
|
|
|
|
|
|
if x == "a":
|
|
|
|
pass
|
|
|
|
elif x == "b":
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
assert_never(x)
|