2
各doctestの最初にpytestの一時ディレクトリに変更したいと思います。すべてのdoctestを起動することなく自動的に行う方法があるかどうか疑問に思っていました:すべてのdoctestのpytest一時ディレクトリに自動的に変更する方法
>>> tmp = getfixture('tmpdir')
>>> old = tmp.chdir()
各doctestの最初にpytestの一時ディレクトリに変更したいと思います。すべてのdoctestを起動することなく自動的に行う方法があるかどうか疑問に思っていました:すべてのdoctestのpytest一時ディレクトリに自動的に変更する方法
>>> tmp = getfixture('tmpdir')
>>> old = tmp.chdir()
すべてがなんとかです:
conftest.py
:
import os
import pytest
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.
olddir = os.getcwd()
tmpdir.chdir()
yield
os.chdir(olddir)
else:
# For normal tests, we have to yield, since this is a yield-fixture.
yield
test_me.py
:
import os.path
# Regular tests are NOT chdir'ed.
def test_me():
moddir = os.path.dirname(__file__)
assert os.getcwd() == moddir
import_me.py
:
import os, os.path
# Doctests are chdir'ed.
def some_func():
"""
>>> 2+3
5
>>> os.getcwd().startswith('/private/')
True
"""
pass
、これはあなたのdoctestを検出するために、どのようにアイデアを提供し、どのようにテスト期間のために一時的にchdirします。
加えて、あなたもブレークポイントを入れて、固定具にrequest.node.dtest
の内容を調べることができます。こうすることで、ドキュメントストリングまたは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