Merged in cpcloud/pytest/ignore-doctest-import-errors (pull request #243)
Add option to ignore import errors in doctests
This commit is contained in:
commit
660b84a052
|
@ -17,6 +17,10 @@ def pytest_addoption(parser):
|
||||||
action="store", default="test*.txt", metavar="pat",
|
action="store", default="test*.txt", metavar="pat",
|
||||||
help="doctests file matching pattern, default: test*.txt",
|
help="doctests file matching pattern, default: test*.txt",
|
||||||
dest="doctestglob")
|
dest="doctestglob")
|
||||||
|
group.addoption("--doctest-ignore-import-errors",
|
||||||
|
action="store_true", default=False,
|
||||||
|
help="ignore doctest ImportErrors",
|
||||||
|
dest="doctest_ignore_import_errors")
|
||||||
|
|
||||||
def pytest_collect_file(path, parent):
|
def pytest_collect_file(path, parent):
|
||||||
config = parent.config
|
config = parent.config
|
||||||
|
@ -130,7 +134,13 @@ class DoctestModule(pytest.File):
|
||||||
if self.fspath.basename == "conftest.py":
|
if self.fspath.basename == "conftest.py":
|
||||||
module = self.config._conftest.importconftest(self.fspath)
|
module = self.config._conftest.importconftest(self.fspath)
|
||||||
else:
|
else:
|
||||||
module = self.fspath.pyimport()
|
try:
|
||||||
|
module = self.fspath.pyimport()
|
||||||
|
except ImportError:
|
||||||
|
if self.config.getvalue('doctest_ignore_import_errors'):
|
||||||
|
pytest.skip('unable to import module %r' % self.fspath)
|
||||||
|
else:
|
||||||
|
raise
|
||||||
# satisfy `FixtureRequest` constructor...
|
# satisfy `FixtureRequest` constructor...
|
||||||
self.funcargs = {}
|
self.funcargs = {}
|
||||||
self._fixtureinfo = FuncFixtureInfo((), [], {})
|
self._fixtureinfo = FuncFixtureInfo((), [], {})
|
||||||
|
@ -138,9 +148,9 @@ class DoctestModule(pytest.File):
|
||||||
doctest_globals = dict(getfixture=fixture_request.getfuncargvalue)
|
doctest_globals = dict(getfixture=fixture_request.getfuncargvalue)
|
||||||
# uses internal doctest module parsing mechanism
|
# uses internal doctest module parsing mechanism
|
||||||
finder = doctest.DocTestFinder()
|
finder = doctest.DocTestFinder()
|
||||||
optionflags= get_optionflags(self)
|
optionflags = get_optionflags(self)
|
||||||
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags)
|
runner = doctest.DebugRunner(verbose=0, optionflags=optionflags)
|
||||||
for test in finder.find(module, module.__name__,
|
for test in finder.find(module, module.__name__,
|
||||||
extraglobs=doctest_globals):
|
extraglobs=doctest_globals):
|
||||||
if test.examples: # skip empty doctests
|
if test.examples: # skip empty doctests
|
||||||
yield DoctestItem(test.name, self, runner, test)
|
yield DoctestItem(test.name, self, runner, test)
|
||||||
|
|
|
@ -341,3 +341,19 @@ class TestDoctests:
|
||||||
""")
|
""")
|
||||||
reprec = testdir.inline_run(p, "--doctest-glob=x*.txt")
|
reprec = testdir.inline_run(p, "--doctest-glob=x*.txt")
|
||||||
reprec.assertoutcome(failed=1, passed=0)
|
reprec.assertoutcome(failed=1, passed=0)
|
||||||
|
|
||||||
|
def test_ignore_import_errors_on_doctest(self, testdir):
|
||||||
|
p = testdir.makepyfile("""
|
||||||
|
import asdf
|
||||||
|
|
||||||
|
def add_one(x):
|
||||||
|
'''
|
||||||
|
>>> add_one(1)
|
||||||
|
2
|
||||||
|
'''
|
||||||
|
return x + 1
|
||||||
|
""")
|
||||||
|
|
||||||
|
reprec = testdir.inline_run(p, "--doctest-modules",
|
||||||
|
"--doctest-ignore-import-errors")
|
||||||
|
reprec.assertoutcome(skipped=1, failed=1, passed=0)
|
||||||
|
|
Loading…
Reference in New Issue