Merged in witsch/pytest/doctest-fixtures (pull request #25)
fixture support in doctests
This commit is contained in:
commit
8f8466ee40
|
@ -1,6 +1,7 @@
|
||||||
""" discover and run doctests in modules and test files."""
|
""" discover and run doctests in modules and test files."""
|
||||||
|
|
||||||
import pytest, py
|
import pytest, py
|
||||||
|
from _pytest.python import FixtureRequest, FuncFixtureInfo
|
||||||
from py._code.code import TerminalRepr, ReprFileLocation
|
from py._code.code import TerminalRepr, ReprFileLocation
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
|
@ -70,9 +71,14 @@ class DoctestItem(pytest.Item):
|
||||||
class DoctestTextfile(DoctestItem, pytest.File):
|
class DoctestTextfile(DoctestItem, pytest.File):
|
||||||
def runtest(self):
|
def runtest(self):
|
||||||
doctest = py.std.doctest
|
doctest = py.std.doctest
|
||||||
|
# satisfy `FixtureRequest` constructor...
|
||||||
|
self.funcargs = {}
|
||||||
|
self._fixtureinfo = FuncFixtureInfo((), [], {})
|
||||||
|
fixture_request = FixtureRequest(self)
|
||||||
failed, tot = doctest.testfile(
|
failed, tot = doctest.testfile(
|
||||||
str(self.fspath), module_relative=False,
|
str(self.fspath), module_relative=False,
|
||||||
optionflags=doctest.ELLIPSIS,
|
optionflags=doctest.ELLIPSIS,
|
||||||
|
extraglobs=dict(getfixture=fixture_request.getfuncargvalue),
|
||||||
raise_on_error=True, verbose=0)
|
raise_on_error=True, verbose=0)
|
||||||
|
|
||||||
class DoctestModule(DoctestItem, pytest.File):
|
class DoctestModule(DoctestItem, pytest.File):
|
||||||
|
@ -82,6 +88,11 @@ class DoctestModule(DoctestItem, pytest.File):
|
||||||
module = self.config._conftest.importconftest(self.fspath)
|
module = self.config._conftest.importconftest(self.fspath)
|
||||||
else:
|
else:
|
||||||
module = self.fspath.pyimport()
|
module = self.fspath.pyimport()
|
||||||
|
# satisfy `FixtureRequest` constructor...
|
||||||
|
self.funcargs = {}
|
||||||
|
self._fixtureinfo = FuncFixtureInfo((), [], {})
|
||||||
|
fixture_request = FixtureRequest(self)
|
||||||
failed, tot = doctest.testmod(
|
failed, tot = doctest.testmod(
|
||||||
module, raise_on_error=True, verbose=0,
|
module, raise_on_error=True, verbose=0,
|
||||||
|
extraglobs=dict(getfixture=fixture_request.getfuncargvalue),
|
||||||
optionflags=doctest.ELLIPSIS)
|
optionflags=doctest.ELLIPSIS)
|
||||||
|
|
|
@ -50,3 +50,9 @@ then you can just invoke ``py.test`` without command line options::
|
||||||
mymodule.py .
|
mymodule.py .
|
||||||
|
|
||||||
========================= 1 passed in 0.02 seconds =========================
|
========================= 1 passed in 0.02 seconds =========================
|
||||||
|
|
||||||
|
It is possible to use fixtures using the ``getfixture`` helper::
|
||||||
|
|
||||||
|
# content of example.rst
|
||||||
|
>>> tmp = getfixture('tmpdir')
|
||||||
|
>>> ...
|
||||||
|
|
|
@ -72,3 +72,12 @@ Python モジュール (通常 python テストモジュールを含む) の doc
|
||||||
mymodule.py .
|
mymodule.py .
|
||||||
|
|
||||||
========================= 1 passed in 0.02 seconds =========================
|
========================= 1 passed in 0.02 seconds =========================
|
||||||
|
|
||||||
|
..
|
||||||
|
It is possible to use fixtures using the ``getfixture`` helper::
|
||||||
|
|
||||||
|
それは ``getfixture`` ヘルパーを使ってフィクスチャを使用することが可能である::
|
||||||
|
|
||||||
|
# content of example.rst
|
||||||
|
>>> tmp = getfixture('tmpdir')
|
||||||
|
>>> ...
|
||||||
|
|
|
@ -124,3 +124,23 @@ class TestDoctests:
|
||||||
" 1",
|
" 1",
|
||||||
"*test_txtfile_failing.txt:2: DocTestFailure"
|
"*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)
|
||||||
|
|
Loading…
Reference in New Issue