Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix usage of pytester with doctests #6802

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -544,7 +544,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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RonnyPfannschmidt
Would like to get your view on this: is a fallback to request.node.name sane, or is there something better?

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 @@ -618,7 +622,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 @@ -721,7 +725,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 @@ -1060,7 +1064,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