Skip to content

Commit

Permalink
Fix usage of pytester with doctests (pytest-dev#6802)
Browse files Browse the repository at this point in the history
Use `request.node.name` instead of `request.function.__name__`:
`request.function` is `None` with `DoctestItem`s.
  • Loading branch information
blueyed authored Mar 4, 2020
1 parent 197b7c3 commit acec0b6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/6802.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The :fixture:`testdir fixture <testdir>` works within doctests now.
12 changes: 8 additions & 4 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,11 @@ def __init__(self, request: FixtureRequest, tmpdir_factory: TempdirFactory) -> N
self._mod_collections = (
WeakKeyDictionary()
) # type: WeakKeyDictionary[Module, List[Union[Item, Collector]]]
name = request.function.__name__
if request.function:
name = request.function.__name__ # type: str
else:
name = request.node.name
self._name = name
self.tmpdir = tmpdir_factory.mktemp(name, numbered=True)
self.test_tmproot = tmpdir_factory.mktemp("tmp-" + name, numbered=True)
self.plugins = [] # type: List[Union[str, _PluggyPlugin]]
Expand Down Expand Up @@ -617,7 +621,7 @@ def to_text(s):

if lines:
source = "\n".join(to_text(x) for x in lines)
basename = self.request.function.__name__
basename = self._name
items.insert(0, (basename, source))

ret = None
Expand Down Expand Up @@ -720,7 +724,7 @@ def copy_example(self, name=None):
example_dir = example_dir.join(*extra_element.args)

if name is None:
func_name = self.request.function.__name__
func_name = self._name
maybe_dir = example_dir / func_name
maybe_file = example_dir / (func_name + ".py")

Expand Down Expand Up @@ -1059,7 +1063,7 @@ def getmodulecol(self, source, configargs=(), withinit=False):
path = self.tmpdir.join(str(source))
assert not withinit, "not supported for paths"
else:
kw = {self.request.function.__name__: Source(source).strip()}
kw = {self._name: Source(source).strip()}
path = self.makepyfile(**kw)
if withinit:
self.makepyfile(__init__="#")
Expand Down
23 changes: 23 additions & 0 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ def test_hello(testdir):
result.assert_outcomes(passed=1)


def test_testdir_with_doctest(testdir):
"""Check that testdir can be used within doctests.
It used to use `request.function`, which is `None` with doctests."""
testdir.makepyfile(
**{
"sub/t-doctest.py": """
'''
>>> import os
>>> testdir = getfixture("testdir")
>>> str(testdir.makepyfile("content")).replace(os.sep, '/')
'.../basetemp/sub.t-doctest0/sub.py'
'''
""",
"sub/__init__.py": "",
}
)
result = testdir.runpytest(
"-p", "pytester", "--doctest-modules", "sub/t-doctest.py"
)
assert result.ret == 0


def test_runresult_assertion_on_xfail(testdir) -> None:
testdir.makepyfile(
"""
Expand Down

0 comments on commit acec0b6

Please sign in to comment.