diff --git a/CHANGELOG b/CHANGELOG index b2ad9ea0a..5643224d2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -29,6 +29,9 @@ Unreleased - don't hide an ImportError when importing a plugin produces one. fixes issue375. +- fix issue275 - allow usefixtures and autouse fixtures + for running doctest text files. + - fix issue380 by making --resultlog only rely on longrepr instead of the "reprcrash" attribute which only exists sometimes. diff --git a/_pytest/doctest.py b/_pytest/doctest.py index 29ddf33ad..82bbc4b49 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -91,8 +91,13 @@ class DoctestTextfile(DoctestItem, pytest.File): doctest = py.std.doctest # satisfy `FixtureRequest` constructor... self.funcargs = {} - self._fixtureinfo = FuncFixtureInfo((), [], {}) + fm = self.session._fixturemanager + def func(): + pass + self._fixtureinfo = fm.getfixtureinfo(node=self, func=func, + cls=None, funcargs=False) fixture_request = FixtureRequest(self) + fixture_request._fillfixtures() failed, tot = doctest.testfile( str(self.fspath), module_relative=False, optionflags=doctest.ELLIPSIS, diff --git a/doc/en/doctest.txt b/doc/en/doctest.txt index 0caf73747..0cf0be5f7 100644 --- a/doc/en/doctest.txt +++ b/doc/en/doctest.txt @@ -56,3 +56,7 @@ It is possible to use fixtures using the ``getfixture`` helper:: # content of example.rst >>> tmp = getfixture('tmpdir') >>> ... + >>> + +Also, :ref:`usefixtures` and :ref:`autouse` fixtures are supported +when executing text doctest files. diff --git a/testing/test_doctest.py b/testing/test_doctest.py index fb8586697..1d4e41333 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -209,6 +209,26 @@ class TestDoctests: reprec = testdir.inline_run(p, ) reprec.assertoutcome(passed=1) + @xfail_if_pdbpp_installed + def test_txtfile_with_usefixtures_in_ini(self, testdir): + testdir.makeini(""" + [pytest] + usefixtures = myfixture + """) + testdir.makeconftest(""" + import pytest + @pytest.fixture + def myfixture(monkeypatch): + monkeypatch.setenv("HELLO", "WORLD") + """) + + p = testdir.maketxtfile(""" + >>> import os + >>> os.environ["HELLO"] + 'WORLD' + """) + reprec = testdir.inline_run(p, ) + reprec.assertoutcome(passed=1) @xfail_if_pdbpp_installed def test_doctestmodule_with_fixtures(self, testdir):