From 9e5f293b4f4683cb44784ac02f8b9557b896b746 Mon Sep 17 00:00:00 2001 From: "W. Augusto Andreoli" Date: Sat, 22 Jan 2022 21:16:05 +0100 Subject: [PATCH 1/2] fix: don't break on doctests Fix AttributeError: 'DoctestItem' object has no attribute 'fixturenames'. --- pytest_socket.py | 5 +++++ tests/test_doctest.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/test_doctest.py diff --git a/pytest_socket.py b/pytest_socket.py index 86b5fdb..9499452 100644 --- a/pytest_socket.py +++ b/pytest_socket.py @@ -112,7 +112,12 @@ def pytest_runtest_setup(item) -> None: This is the bulk of the logic for the plugin. As the logic can be extensive, this method is allowed complexity. It may be refactored in the future to be more readable. + + If the given item is not a function test (i.e a DoctestItem) + or otherwise has no support for fixtures, skip it. """ + if not hasattr(item, "fixturenames"): + return # If test has the `enable_socket` marker, we accept this as most explicit. if "socket_enabled" in item.fixturenames or item.get_closest_marker( diff --git a/tests/test_doctest.py b/tests/test_doctest.py new file mode 100644 index 0000000..d1b2d38 --- /dev/null +++ b/tests/test_doctest.py @@ -0,0 +1,15 @@ + +def test_function_with_doctest(testdir): + testdir.makepyfile( + ''' + def my_sum(a, b): + """Sum two values. + + >>> my_sum(1, 1) + 2 + """ + return a + b + ''' + ) + result = testdir.runpytest("--doctest-modules") + result.assert_outcomes(passed=1) From 6f97dc0fa024060328b5d78b4b84c2bccd159a27 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 22 Jan 2022 20:18:32 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_doctest.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_doctest.py b/tests/test_doctest.py index d1b2d38..9344c70 100644 --- a/tests/test_doctest.py +++ b/tests/test_doctest.py @@ -1,4 +1,3 @@ - def test_function_with_doctest(testdir): testdir.makepyfile( '''