Merge pull request #6840 from blueyed/test_source_with_decorator

This commit is contained in:
Bruno Oliveira 2020-05-19 19:30:23 -03:00 committed by GitHub
commit d05ef61e95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 0 deletions

View File

@ -624,6 +624,31 @@ def test_comment_in_statement() -> None:
)
def test_source_with_decorator() -> None:
"""Test behavior with Source / Code().source with regard to decorators."""
from _pytest.compat import get_real_func
@pytest.mark.foo
def deco_mark():
assert False
src = inspect.getsource(deco_mark)
assert str(Source(deco_mark, deindent=False)) == src
assert src.startswith(" @pytest.mark.foo")
@pytest.fixture
def deco_fixture():
assert False
src = inspect.getsource(deco_fixture)
assert src == " @pytest.fixture\n def deco_fixture():\n assert False\n"
# currenly Source does not unwrap decorators, testing the
# existing behavior here for explicitness, but perhaps we should revisit/change this
# in the future
assert str(Source(deco_fixture)).startswith("@functools.wraps(function)")
assert str(Source(get_real_func(deco_fixture), deindent=False)) == src
def test_single_line_else() -> None:
source = getstatement(1, "if False: 2\nelse: 3")
assert str(source) == "else: 3"