From 61180eec938e41989a443ba899a55a8f6c87a8f9 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 28 Feb 2020 20:32:33 +0100 Subject: [PATCH 1/3] 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" From b98a182aa1614a43c7a884148500d85d58150c69 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Fri, 28 Feb 2020 20:45:02 +0100 Subject: [PATCH 2/3] (no) coverage --- testing/code/test_source.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testing/code/test_source.py b/testing/code/test_source.py index 0d01e73d2..7b828abc9 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -630,7 +630,7 @@ def test_source_with_decorator() -> None: @pytest.mark.foo def deco_mark(): - pass + assert False src = inspect.getsource(deco_mark) assert str(Source(deco_mark, deindent=False)) == src @@ -638,10 +638,10 @@ def test_source_with_decorator() -> None: @pytest.fixture def deco_fixture(): - pass + assert False src = inspect.getsource(deco_fixture) - assert src == " @pytest.fixture\n def deco_fixture():\n pass\n" + assert src == " @pytest.fixture\n def deco_fixture():\n assert False\n" assert str(Source(deco_fixture)).startswith("@functools.wraps(function)") assert str(Source(get_real_func(deco_fixture), deindent=False)) == src From 55099e57c37778fcb585ecdb2f1469ac7131a1f9 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 19 May 2020 19:19:53 -0300 Subject: [PATCH 3/3] Add requested comment as per review --- testing/code/test_source.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/testing/code/test_source.py b/testing/code/test_source.py index 7b828abc9..35728c334 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -642,6 +642,9 @@ def test_source_with_decorator() -> None: 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