Merged in witsch/pytest/doctest-fixtures (pull request #25)

fixture support in doctests
This commit is contained in:
holger krekel 2013-03-21 12:33:43 +01:00
commit 8f8466ee40
4 changed files with 46 additions and 0 deletions

View File

@ -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)

View File

@ -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')
>>> ...

View File

@ -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')
>>> ...

View File

@ -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)