def __init__(self, checker=None, verbose=False, optionflags=0): """ Create a new test runner. Optional keyword arg `checker` is the `OutputChecker` that should be used to compare the expected outputs and actual outputs of doctest examples. Optional keyword arg 'verbose' prints lots of stuff if true, only failures if false; by default, it's true iff '-v' is in sys.argv. Optional argument `optionflags` can be used to control how the test runner compares expected output to actual output, and how it displays failures. See the documentation for `testmod` for more information. """ self._checker = checker or OutputChecker() self._verbose = verbose self.optionflags = optionflags self.original_optionflags = optionflags # Keep track of the examples we've run. self.tries = 0 self.failures = 0 self._name2ft = {} # Create a fake output target for capturing doctest output. self._fakeout = _SpoofOut()
def __init__(self, text_formatter=formatters.NoopFormatter(), code_parser=parsers.DocTestParser(), use_ansi=True, use_readline=True, env_driver=None, *args, **kwargs): code.InteractiveConsole.__init__(self, *args, **kwargs) self._output_checker = doctest.OutputChecker() self._fakeout = doctest._SpoofOut() self.chunks = None self.exc_msg = None self.name = 'literate program' self.text_formatter = text_formatter self.code_parser = code_parser self.use_ansi = use_ansi self.pause = True self.interactive = True if use_readline: self._readline = __import__('readline') self._add_readline() else: self._readline = None self._env_driver = env_driver self._correct_path()
def doctest_run_examples_inner(out, fakeout, examples, globs, verbose, name, compileflags, optionflags): import sys, traceback OK, BOOM, FAIL = range(3) NADA = "nothing" stderr = doctest._SpoofOut() _tag_out = doctest._tag_out failures = 0 for source, want, lineno in examples: if verbose: _tag_out(out, ("Trying", source), ("Expecting", want or NADA)) fakeout.clear() try: exec compile(source, "<string>", "single", compileflags, 1) in globs got = fakeout.get() state = OK except KeyboardInterrupt: raise except: # See whether the exception was expected. if ( want.find("Traceback (innermost last):\n") == 0 or want.find("Traceback (most recent call last):\n") == 0 ): # Only compare exception type and value - the rest of # the traceback isn't necessary. want = want.split("\n")[-2] + "\n" exc_type, exc_val = sys.exc_info()[:2] got = traceback.format_exception_only(exc_type, exc_val)[-1] state = OK else: # unexpected exception stderr.clear() traceback.print_exc(file=stderr) state = BOOM if state == OK: if _check_output(want, got, optionflags): if verbose: out("ok\n") continue state = FAIL assert state in (FAIL, BOOM) failures = failures + 1 out("*" * 65 + "\n") _tag_out(out, ("Failure in example", source)) out("from line #" + ` lineno ` + " of " + name + "\n") if state == FAIL: _tag_out(out, ("Expected", want or NADA), ("Got", got)) else: assert state == BOOM _tag_out(out, ("Exception raised", stderr.get())) return failures, len(examples)
def doctest_run_examples_inner(out, fakeout, examples, globs, verbose, name, compileflags, optionflags): import sys, traceback OK, BOOM, FAIL = range(3) NADA = "nothing" stderr = doctest._SpoofOut() _tag_out = doctest._tag_out failures = 0 for source, want, lineno in examples: if verbose: _tag_out(out, ("Trying", source), ("Expecting", want or NADA)) fakeout.clear() try: exec compile(source, "<string>", "single", compileflags, 1) in globs got = fakeout.get() state = OK except KeyboardInterrupt: raise except: # See whether the exception was expected. if want.find("Traceback (innermost last):\n") == 0 or \ want.find("Traceback (most recent call last):\n") == 0: # Only compare exception type and value - the rest of # the traceback isn't necessary. want = want.split('\n')[-2] + '\n' exc_type, exc_val = sys.exc_info()[:2] got = traceback.format_exception_only(exc_type, exc_val)[-1] state = OK else: # unexpected exception stderr.clear() traceback.print_exc(file=stderr) state = BOOM if state == OK: if _check_output(want, got, optionflags): if verbose: out("ok\n") continue state = FAIL assert state in (FAIL, BOOM) failures = failures + 1 out("*" * 65 + "\n") _tag_out(out, ("Failure in example", source)) out("from line #" + ` lineno ` + " of " + name + "\n") if state == FAIL: _tag_out(out, ("Expected", want or NADA), ("Got", got)) else: assert state == BOOM _tag_out(out, ("Exception raised", stderr.get())) return failures, len(examples)