diff --git a/_pytest/doctest.py b/_pytest/doctest.py index aaebab78e..6753e5040 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -1,6 +1,7 @@ """ discover and run doctests in modules and test files.""" import pytest, py +from _pytest.python import FixtureRequest, FuncFixtureInfo from py._code.code import TerminalRepr, ReprFileLocation def pytest_addoption(parser): @@ -70,9 +71,14 @@ class DoctestItem(pytest.Item): class DoctestTextfile(DoctestItem, pytest.File): def runtest(self): doctest = py.std.doctest + # satisfy `FixtureRequest` constructor... + self.funcargs = {} + self._fixtureinfo = FuncFixtureInfo((), [], {}) + fixture_request = FixtureRequest(self) failed, tot = doctest.testfile( str(self.fspath), module_relative=False, optionflags=doctest.ELLIPSIS, + extraglobs=dict(getfixture=fixture_request.getfuncargvalue), raise_on_error=True, verbose=0) class DoctestModule(DoctestItem, pytest.File): @@ -82,6 +88,11 @@ class DoctestModule(DoctestItem, pytest.File): module = self.config._conftest.importconftest(self.fspath) else: module = self.fspath.pyimport() + # satisfy `FixtureRequest` constructor... + self.funcargs = {} + self._fixtureinfo = FuncFixtureInfo((), [], {}) + fixture_request = FixtureRequest(self) failed, tot = doctest.testmod( module, raise_on_error=True, verbose=0, + extraglobs=dict(getfixture=fixture_request.getfuncargvalue), optionflags=doctest.ELLIPSIS) diff --git a/doc/en/doctest.txt b/doc/en/doctest.txt index 63fb32dc8..9f8b8a9db 100644 --- a/doc/en/doctest.txt +++ b/doc/en/doctest.txt @@ -50,3 +50,9 @@ then you can just invoke ``py.test`` without command line options:: mymodule.py . ========================= 1 passed in 0.02 seconds ========================= + +It is possible to use fixtures using the ``getfixture`` helper:: + + # content of example.rst + >>> tmp = getfixture('tmpdir') + >>> ... diff --git a/doc/ja/doctest.txt b/doc/ja/doctest.txt index 2765d7e7a..00c9001d2 100644 --- a/doc/ja/doctest.txt +++ b/doc/ja/doctest.txt @@ -72,3 +72,12 @@ Python モジュール (通常 python テストモジュールを含む) の doc mymodule.py . ========================= 1 passed in 0.02 seconds ========================= + +.. + It is possible to use fixtures using the ``getfixture`` helper:: + +それは ``getfixture`` ヘルパーを使ってフィクスチャを使用することが可能である:: + + # content of example.rst + >>> tmp = getfixture('tmpdir') + >>> ... diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 771cb52f3..7b22e906c 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -124,3 +124,23 @@ class TestDoctests: " 1", "*test_txtfile_failing.txt:2: DocTestFailure" ]) + + def test_txtfile_with_fixtures(self, testdir): + p = testdir.maketxtfile(""" + >>> dir = getfixture('tmpdir') + >>> type(dir).__name__ + 'LocalPath' + """) + reprec = testdir.inline_run(p, ) + reprec.assertoutcome(passed=1) + + def test_doctestmodule_with_fixtures(self, testdir): + p = testdir.makepyfile(""" + ''' + >>> dir = getfixture('tmpdir') + >>> type(dir).__name__ + 'LocalPath' + ''' + """) + reprec = testdir.inline_run(p, "--doctest-modules") + reprec.assertoutcome(passed=1)