Lines Matching +full:runner +full:- +full:before +full:- +full:script
2 # Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org).
7 # Provided as-is; use at your own risk; no warranty; no promises; enjoy!
9 r"""Module doctest -- a framework for running examples in docstrings.
20 Then running the module as a script will cause the examples in the
30 Run it with the -v switch instead:
32 python M.py -v
42 with the unittest framework, and support for running non-Python text
74 # 5. Doctest Runner
111 # - Example: a <source, want> pair, plus an intra-docstring line number.
112 # - DocTest: a collection of examples, parsed from a docstring, plus
114 # - DocTestFinder: extracts DocTests from a given object's docstring and
116 # - DocTestRunner: runs DocTest cases, and accumulates statistics.
121 # +------+ +---------+ +-------+
122 # |object| --DocTestFinder-> | DocTest | --DocTestRunner-> |results|
123 # +------+ +---------+ +-------+
127 # +---------+
170 # 2. Example & DocTest -- store test cases
171 # 3. DocTest Parser -- extracts examples from strings
172 # 4. DocTest Finder -- extracts test cases from objects
173 # 5. DocTest Runner -- runs test cases
174 # 6. Test Functions -- convenient wrappers for testing
185 Return the compiler-flags associated with the future features that
198 - If `module` is a module, then return module.
199 - If `module` is a string, then import and return the
201 - If `module` is None, then return the calling module.
239 every non-blank line in `s`, and return the result.
241 # This regexp matches the start of non-blank lines:
270 # Worst-case linear-time ellipsis matching.
293 w = ws[-1]
296 endpos -= len(w)
297 del ws[-1]
306 # For the rest, we only need to find the leftmost non-overlapping
395 raise ValueError('Module-relative files may not have absolute paths')
428 ## - An "example" is a <source, want> pair, where "source" is a
433 ## - A "doctest" is a collection of examples, typically extracted from
442 - source: A single Python statement, always ending with a newline.
445 - want: The expected output from running the source code (either
450 - exc_msg: The exception message generated by the example, if
458 - lineno: The line number within the DocTest string containing
460 zero-based, with respect to the beginning of the DocTest.
462 - indent: The example's indentation in the DocTest string.
466 - options: A dictionary mapping from option flags to True or
510 - examples: the list of examples.
512 - globs: The namespace (aka globals) that the examples should
515 - name: A name identifying the DocTest (typically, the name of
518 - filename: The name of the file that this DocTest was extracted
521 - lineno: The line number within filename where this DocTest
523 line number is zero-based, with respect to the beginning of
526 - docstring: The string that the examples were extracted from,
595 # Want consists of any non-blank lines that do not start with PS1.
604 # - the traceback header line (`hdr`)
605 # - the traceback stack (`stack`)
606 # - the exception message (`msg`), as generated by
609 # exception message is the first non-indented line starting with a word
632 Line numbers for the Examples are 0-based. The optional
646 # Add the pre-example text to `output`.
648 # Update lineno (lines before this example)
663 # Add any remaining post-example text to `output`.
683 0-based, because it's most common in doctests that nothing
684 interesting appears on the same line as opening triple-quote,
715 # then strip the indentation. Spaces before the last newline should
719 if len(want_lines) > 1 and re.match(r' *$', want_lines[-1]):
720 del want_lines[-1] # forget final newline & spaces after it
740 # positives for string-literals that contain the string
760 if (option[0] not in '+-' or
773 # This regular expression finds the indentation of every non-blank
778 "Return the minimum indentation of any non-blank line in `s`"
857 - As a default namespace, if `globs` is not specified.
858 - To prevent the DocTestFinder from extracting DocTests
860 - To find the name of the file containing the object.
861 - To help find the line number of the object within its
869 considered to belong to the (non-existent) module, so all contained
909 if not file[0]+file[-2:] == '<]>': file = None
942 # verbose-mode output. This was a feature of doctest in Pythons
1082 if filename[-4:] == ".pyc":
1083 filename = filename[:-1]
1107 getattr(obj, '__name__', '-'))
1121 lineno = obj.co_firstlineno - 1
1140 ## 5. DocTest Runner
1151 >>> runner = DocTestRunner(verbose=False)
1154 ... print(test.name, '->', runner.run(test))
1155 _TestClass -> TestResults(failed=0, attempted=2)
1156 _TestClass.__init__ -> TestResults(failed=0, attempted=2)
1157 _TestClass.get -> TestResults(failed=0, attempted=2)
1158 _TestClass.square -> TestResults(failed=0, attempted=1)
1161 have been run by the runner, and returns an aggregated `(f, t)`
1164 >>> runner.summarize(verbose=1)
1178 >>> runner.tries
1180 >>> runner.failures
1190 The test runner's display output can be controlled in two ways.
1205 Create a new test runner.
1212 only failures if false; by default, it's true iff '-v' is in
1216 test runner compares expected output to actual output, and how
1222 verbose = '-v' in sys.argv
1241 Report that the test runner is about to process the given
1373 exc_msg = traceback.format_exception_only(*exception[:2])[-1]
1453 specified, then it will default to the set of future-import
1468 if encoding is None or encoding.lower() == 'utf-8':
1560 print(totalt - totalf, "passed and", totalf, "failed.")
1593 Convert string to hex-escaped ASCII string.
1602 depending on what option flags the test runner is using,
1603 several non-exact match types are also possible. See the
1608 # If `want` contains hex-escaped character such as "\u1234",
1612 # be folded to hex-escaped ASCII string to compare.
1617 # if they're string-identical, always return true.
1668 # too hard ... or maybe not. In two real-life failures Tim saw,
1671 # and could be the basis for a kick-ass diff in this case.
1676 # for 1-line differences.
1705 kind = 'unified diff with -expected +actual'
1713 kind = 'ndiff with -expected +actual'
1734 - test: the DocTest object being run
1736 - example: the Example object that failed
1738 - got: the actual output
1753 - test: the DocTest object being run
1755 - example: the Example object that failed
1757 - exc_info: the exception info
1773 >>> runner = DebugRunner(verbose=False)
1777 ... runner.run(test)
1805 ... runner.run(test)
1835 >>> runner.run(test)
1850 >>> runner.run(test)
1912 only failures if false; by default, it's true iff "-v" is in sys.argv.
1935 post-mortem debugged.
1949 # DWA - m will still be None if this wasn't invoked from the command
1966 runner = DebugRunner(verbose=verbose, optionflags=optionflags)
1968 runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
1971 runner.run(test)
1974 runner.summarize()
1977 master = runner
1979 master.merge(runner)
1981 return TestResults(runner.failures, runner.tries)
1993 - If "module_relative" is True (the default), then "filename"
1994 specifies a module-relative path. By default, this path is
1997 package. To ensure os-independence, "filename" should use
2001 - If "module_relative" is False, then "filename" specifies an
2002 os-specific path. The path may be absolute or relative (to
2025 only failures if false; by default, it's true iff "-v" is in sys.argv.
2047 post-mortem debugged.
2066 raise ValueError("Package may only be specified for module-"
2071 encoding or "utf-8")
2088 runner = DebugRunner(verbose=verbose, optionflags=optionflags)
2090 runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
2094 runner.run(test)
2097 runner.summarize()
2100 master = runner
2102 master.merge(runner)
2104 return TestResults(runner.failures, runner.tries)
2116 it will default to the set of future-import flags that apply to
2125 runner = DocTestRunner(verbose=verbose, optionflags=optionflags)
2127 runner.run(test, compileflags=compileflags)
2138 The old flag is returned so that a runner could restore the old
2211 runner = DocTestRunner(optionflags=optionflags,
2215 runner.DIVIDER = "-"*70
2216 failures, tries = runner.run(
2230 lname = '.'.join(test.name.split('.')[-1:])
2240 and test suites to support post-mortem debugging. The test code
2242 caller can catch the errors and initiate post-mortem debugging.
2303 runner = DebugRunner(optionflags=self._dt_optionflags,
2305 runner.run(self._dt_test, clear_globs=False)
2327 return "%s (%s)" % (name[-1], '.'.join(name[:-1]))
2340 self.skipTest("DocTestSuite will not work with -O2 and above")
2376 A set-up function. This is called before running the
2382 A tear-down function. This is called after running the
2401 # Skip doctests when running with -O2
2414 if filename[-4:] == ".pyc":
2415 filename = filename[:-1]
2443 raise ValueError("Package may only be specified for module-"
2448 encoding or "utf-8")
2471 interpreted as os-independent module-relative paths. By
2474 they are relative to that package. To ensure os-independence,
2480 interpreted as os-specific paths. These paths may be absolute
2492 A set-up function. This is called before running the
2498 A tear-down function. This is called after running the
2534 r"""Extract script from text with examples.
2536 Converts text with examples to a Python script. Example input is
2595 output.append(piece.source[:-1])
2600 output += ['## '+l for l in want.split('\n')[:-1]]
2602 # Add non-example text.
2604 for l in piece.split('\n')[:-1]]
2607 while output and output[-1] == '#':
2616 """Extract the test sources from a doctest docstring as a script.
2637 "Debug a test script. `src` is the script, as a string."
2672 A pointless class, for sanity-checking of docstring testing.
2678 >>> _TestClass(13).get() + _TestClass(-12).get()
2685 """val -> _TestClass object with associated value val.
2695 """square() -> square TestClass's associated value
2705 """get() -> return TestClass's associated value.
2707 >>> x = _TestClass(-42)
2709 -42
2716 Example of a string object, searched as-is.
2722 "bool-int equivalence": r"""
2768 parser = argparse.ArgumentParser(description="doctest runner")
2769 parser.add_argument('-v', '--verbose', action='store_true', default=False,
2771 parser.add_argument('-o', '--option', action='append',
2776 parser.add_argument('-f', '--fail-fast', action='store_true',
2778 ' is a shorthand for -o FAIL_FAST, and is'
2779 ' in addition to any other -o options)'))
2794 # It is a module -- insert its dir into sys.path and try to
2799 m = __import__(filename[:-3])