Skip to content

Commit

Permalink
DoctTest line numbers not found due to method wrapping. (pytest-dev#8796
Browse files Browse the repository at this point in the history
)
  • Loading branch information
denivyruck committed Jul 6, 2021
1 parent 6740fb9 commit fc16751
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ def _find_lineno(self, obj, source_lines):
"""
if isinstance(obj, property):
obj = getattr(obj, "fget", obj)

if hasattr(obj, "__wrapped__"):
# Get the main obj in case of it being wrapped
obj = inspect.unwrap(obj)

# Type ignored because this is a private function.
return doctest.DocTestFinder._find_lineno( # type: ignore
self,
Expand Down
35 changes: 35 additions & 0 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,41 @@ def test_continue_on_failure(self, pytester: Pytester):
["*4: UnexpectedException*", "*5: DocTestFailure*", "*8: DocTestFailure*"]
)

def test_skipping_wrapped_test(self, pytester):
"""
Issue 8796: INTERNALERROR raised when skipping a decorated DocTest
through pytest_collection_modifyitems.
"""
pytester.makeconftest(
"""
import pytest
from _pytest.doctest import DoctestItem
def pytest_collection_modifyitems(config, items):
skip_marker = pytest.mark.skip()
for item in items:
if isinstance(item, DoctestItem):
item.add_marker(skip_marker)
"""
)

pytester.makepyfile(
"""
from contextlib import contextmanager
@contextmanager
def my_config_context():
'''
>>> import os
'''
"""
)

result = pytester.runpytest("--doctest-modules")
assert "INTERNALERROR" not in result.stdout.str()
result.assert_outcomes(skipped=1)


class TestDoctestAutoUseFixtures:

Expand Down

0 comments on commit fc16751

Please sign in to comment.