Turn on the continue on failure only when the flag is given

This commit is contained in:
William Lee 2018-02-23 22:31:11 -06:00
parent 7f2dd74ae9
commit f4cc45bb41
2 changed files with 26 additions and 17 deletions

View File

@ -50,6 +50,10 @@ def pytest_addoption(parser):
action="store_true", default=False, action="store_true", default=False,
help="ignore doctest ImportErrors", help="ignore doctest ImportErrors",
dest="doctest_ignore_import_errors") dest="doctest_ignore_import_errors")
group.addoption("--doctest-continue-on-failure",
action="store_true", default=False,
help="for a given doctest, continue to run after the first failure",
dest="doctest_continue_on_failure")
def pytest_collect_file(path, parent): def pytest_collect_file(path, parent):
@ -100,23 +104,17 @@ class MultipleDoctestFailures(Exception):
def _init_runner_class(): def _init_runner_class():
import doctest import doctest
class PytestDoctestRunner(doctest.DocTestRunner): class PytestDoctestRunner(doctest.DebugRunner):
""" """
Runner to collect failures. Note that the out variable in this case is Runner to collect failures. Note that the out variable in this case is
a list instead of a stdout-like object a list instead of a stdout-like object
""" """
def __init__(self, checker=None, verbose=None, optionflags=0, def __init__(self, checker=None, verbose=None, optionflags=0,
continue_on_failure=True): continue_on_failure=True):
doctest.DocTestRunner.__init__( doctest.DebugRunner.__init__(
self, checker=checker, verbose=verbose, optionflags=optionflags) self, checker=checker, verbose=verbose, optionflags=optionflags)
self.continue_on_failure = continue_on_failure self.continue_on_failure = continue_on_failure
def report_start(self, out, test, example):
pass
def report_success(self, out, test, example, got):
pass
def report_failure(self, out, test, example, got): def report_failure(self, out, test, example, got):
failure = doctest.DocTestFailure(test, example, got) failure = doctest.DocTestFailure(test, example, got)
if self.continue_on_failure: if self.continue_on_failure:
@ -257,6 +255,16 @@ def get_optionflags(parent):
return flag_acc return flag_acc
def _get_continue_on_failure(config):
continue_on_failure = config.getvalue('doctest_continue_on_failure')
if continue_on_failure:
# We need to turn off this if we use pdb since we should stop at
# the first failure
if config.getvalue("usepdb"):
continue_on_failure = False
return continue_on_failure
class DoctestTextfile(pytest.Module): class DoctestTextfile(pytest.Module):
obj = None obj = None
@ -272,10 +280,11 @@ class DoctestTextfile(pytest.Module):
globs = {'__name__': '__main__'} globs = {'__name__': '__main__'}
optionflags = get_optionflags(self) optionflags = get_optionflags(self)
continue_on_failure = not self.config.getvalue("usepdb")
runner = _get_runner(verbose=0, optionflags=optionflags, runner = _get_runner(
checker=_get_checker(), verbose=0, optionflags=optionflags,
continue_on_failure=continue_on_failure) checker=_get_checker(),
continue_on_failure=_get_continue_on_failure(self.config))
_fix_spoof_python2(runner, encoding) _fix_spoof_python2(runner, encoding)
parser = doctest.DocTestParser() parser = doctest.DocTestParser()
@ -310,10 +319,10 @@ class DoctestModule(pytest.Module):
# 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)
continue_on_failure = not self.config.getvalue("usepdb") runner = _get_runner(
runner = _get_runner(verbose=0, optionflags=optionflags, verbose=0, optionflags=optionflags,
checker=_get_checker(), checker=_get_checker(),
continue_on_failure=continue_on_failure) continue_on_failure=_get_continue_on_failure(self.config))
for test in finder.find(module, module.__name__): for test in finder.find(module, module.__name__):
if test.examples: # skip empty doctests if test.examples: # skip empty doctests

View File

@ -767,7 +767,7 @@ class TestDoctestSkips(object):
7 7
>>> i + 1 >>> i + 1
""") """)
result = testdir.runpytest("--doctest-modules") result = testdir.runpytest("--doctest-modules", "--doctest-continue-on-failure")
result.assert_outcomes(passed=0, failed=1) result.assert_outcomes(passed=0, failed=1)
# The lines that contains the failure are 4, 5, and 8. The first one # The lines that contains the failure are 4, 5, and 8. The first one
# is a stack trace and the other two are mismatches. # is a stack trace and the other two are mismatches.