如何自动更改所有文档测试的 pytest 临时目录

d_j*_*d_j 4 python testing doctest pytest

我想在每个 doctest 的开头更改为 pytest 临时目录,我想知道是否有一种方法可以自动执行此操作,而无需启动每个 doctest:

>>> tmp = getfixture('tmpdir')                                                                                            
>>> old = tmp.chdir()                                                                                                   
Run Code Online (Sandbox Code Playgroud)

Ser*_*yev 5

一切皆有可能:

conftest.py:

import pytest

@pytest.fixture(autouse=True)
def _docdir(request):

    # Trigger ONLY for the doctests.
    doctest_plugin = request.config.pluginmanager.getplugin("doctest")
    if isinstance(request.node, doctest_plugin.DoctestItem):

        # Get the fixture dynamically by its name.
        tmpdir = request.getfuncargvalue('tmpdir')

        # Chdir only for the duration of the test.
        with tmpdir.as_cwd():
            yield

    else:
        # For normal tests, we have to yield, since this is a yield-fixture.
        yield
Run Code Online (Sandbox Code Playgroud)

test_me.py:

import os.path

# Regular tests are NOT chdir'ed.
def test_me():
    moddir = os.path.dirname(__file__)
    assert os.getcwd() == moddir
Run Code Online (Sandbox Code Playgroud)

import_me.py:

import os, os.path

# Doctests are chdir'ed.
def some_func():
    """
    >>> 2+3
    5
    >>> os.getcwd().startswith('/private/')
    True
    """
    pass
Run Code Online (Sandbox Code Playgroud)

希望这能让您了解如何检测 doctest,以及如何在测试期间临时 chdir。


此外,您还可以放置断点并调查request.node.dtest夹具中的内容。这样,您可以将可选的注释/标记添加到文档字符串或文档测试行,并进行相应的操作:

(Pdb++) pp request.node.dtest.docstring
"\n    >>> 2+3\n    5\n    >>> os.getcwd().startswith('/private/')\n    True\n    "

(Pdb++) pp request.node.dtest.examples[0].source
'2+3\n'
(Pdb++) pp request.node.dtest.examples[0].want
'5\n'

(Pdb++) pp request.node.dtest.examples[1].source
"os.getcwd().startswith('/private/')\n"
(Pdb++) pp request.node.dtest.examples[1].want
'True\n'
(Pdb++) pp request.node.dtest.examples[1].exc_msg
None
Run Code Online (Sandbox Code Playgroud)