From 61180eec938e41989a443ba899a55a8f6c87a8f9 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 28 Feb 2020 20:32:33 +0100 Subject: [PATCH] Test behavior of Source with regard to decorators Unlinke `inspect.getsource` it does not unwrap functions. --- testing/code/test_source.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/testing/code/test_source.py b/testing/code/test_source.py index 792b8d6b1..0d01e73d2 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -624,6 +624,28 @@ 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(): + pass + + src = inspect.getsource(deco_mark) + assert str(Source(deco_mark, deindent=False)) == src + assert src.startswith(" @pytest.mark.foo") + + @pytest.fixture + def deco_fixture(): + pass + + src = inspect.getsource(deco_fixture) + assert src == " @pytest.fixture\n def deco_fixture():\n pass\n" + 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"