Python 如何为所有doctest自动更改为pytest临时目录

Python 如何为所有doctest自动更改为pytest临时目录,python,testing,pytest,doctest,Python,Testing,Pytest,Doctest,我想在每个doctest开始时切换到pytest临时目录,我想知道是否有一种方法可以在不启动每个doctest的情况下自动执行此操作: >>> tmp = getfixture('tmpdir') >>> old = tmp.chdir()

我想在每个doctest开始时切换到pytest临时目录,我想知道是否有一种方法可以在不启动每个doctest的情况下自动执行此操作:

>>> tmp = getfixture('tmpdir')                                                                                            
>>> old = tmp.chdir()                                                                                                   
一切都是可行的:

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
import os.path

# Regular tests are NOT chdir'ed.
def test_me():
    moddir = os.path.dirname(__file__)
    assert os.getcwd() == moddir
import os, os.path

# Doctests are chdir'ed.
def some_func():
    """
    >>> 2+3
    5
    >>> os.getcwd().startswith('/private/')
    True
    """
    pass
test\u me.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
import os.path

# Regular tests are NOT chdir'ed.
def test_me():
    moddir = os.path.dirname(__file__)
    assert os.getcwd() == moddir
import os, os.path

# Doctests are chdir'ed.
def some_func():
    """
    >>> 2+3
    5
    >>> os.getcwd().startswith('/private/')
    True
    """
    pass
import\u me.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
import os.path

# Regular tests are NOT chdir'ed.
def test_me():
    moddir = os.path.dirname(__file__)
    assert os.getcwd() == moddir
import os, os.path

# Doctests are chdir'ed.
def some_func():
    """
    >>> 2+3
    5
    >>> os.getcwd().startswith('/private/')
    True
    """
    pass
希望这能让您了解如何检测doctest,以及如何在测试期间临时执行chdir


此外,您还可以在fixture中放置断点并研究
request.node.dtest
的内容。这样,您就可以向docstring或doctest行添加可选注释/标记,并相应地执行以下操作:

(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