我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用doctest.Tester()。
def __init__(self, mod=None, globs=None, verbose=None, optionflags=0): warnings.warn("class Tester is deprecated; " "use class doctest.DocTestRunner instead", DeprecationWarning, stacklevel=2) if mod is None and globs is None: raise TypeError("Tester.__init__: must specify mod or globs") if mod is not None and not inspect.ismodule(mod): raise TypeError("Tester.__init__: mod must be a module; %r" % (mod,)) if globs is None: globs = mod.__dict__ self.globs = globs self.verbose = verbose self.optionflags = optionflags self.testfinder = DocTestFinder() self.testrunner = DocTestRunner(verbose=verbose, optionflags=optionflags)
def __init__(self, mod=None, globs=None, verbose=None, isprivate=None, optionflags=0): warnings.warn("class Tester is deprecated; " "use class doctest.DocTestRunner instead", DeprecationWarning, stacklevel=2) if mod is None and globs is None: raise TypeError("Tester.__init__: must specify mod or globs") if mod is not None and not inspect.ismodule(mod): raise TypeError("Tester.__init__: mod must be a module; %r" % (mod,)) if globs is None: globs = mod.__dict__ self.globs = globs self.verbose = verbose self.isprivate = isprivate self.optionflags = optionflags self.testfinder = DocTestFinder(_namefilter=isprivate) self.testrunner = DocTestRunner(verbose=verbose, optionflags=optionflags)
def old_test2(): r""" >>> from doctest import Tester >>> t = Tester(globs={}, verbose=1) >>> test = r''' ... # just an example ... >>> x = 1 + 2 ... >>> x ... 3 ... ''' >>> t.runstring(test, "Example") Running string Example Trying: x = 1 + 2 Expecting nothing ok Trying: x Expecting: 3 ok 0 of 2 examples failed in string Example TestResults(failed=0, attempted=2) """
def test_main(): # Check the doctest cases in doctest itself: test_support.run_doctest(doctest, verbosity=True) from test import test_doctest # Ignore all warnings about the use of class Tester in this module. deprecations = [] if __debug__: deprecations.append(("class Tester is deprecated", DeprecationWarning)) if sys.py3kwarning: deprecations += [("backquote not supported", SyntaxWarning), ("execfile.. not supported", DeprecationWarning)] with test_support.check_warnings(*deprecations): # Check the doctest cases defined here: test_support.run_doctest(test_doctest, verbosity=True)
def run_docstring_examples(f, globs, verbose=False, name="NoName", compileflags=None, optionflags=0): """ Test examples in the given object's docstring (`f`), using `globs` as globals. Optional argument `name` is used in failure messages. If the optional argument `verbose` is true, then generate output even if there are no failures. `compileflags` gives the set of flags that should be used by the Python compiler when running the examples. If not specified, then it will default to the set of future-import flags that apply to `globs`. Optional keyword arg `optionflags` specifies options for the testing and output. See the documentation for `testmod` for more information. """ # Find, parse, and run all tests in the given module. finder = DocTestFinder(verbose=verbose, recurse=False) runner = DocTestRunner(verbose=verbose, optionflags=optionflags) for test in finder.find(f, name, globs=globs): runner.run(test, compileflags=compileflags) ###################################################################### ## 7. Tester ###################################################################### # This is provided only for backwards compatibility. It's not # actually used in any way.
def test_lineendings(): r""" *nix systems use \n line endings, while Windows systems use \r\n. Python handles this using universal newline mode for reading files. Let's make sure doctest does so (issue 8473) by creating temporary test files using each of the two line disciplines. One of the two will be the "wrong" one for the platform the test is run on. Windows line endings first: >>> import tempfile, os >>> fn = tempfile.mktemp() >>> with open(fn, 'wb') as f: ... f.write('Test:\r\n\r\n >>> x = 1 + 1\r\n\r\nDone.\r\n') >>> doctest.testfile(fn, module_relative=False, verbose=False) TestResults(failed=0, attempted=1) >>> os.remove(fn) And now *nix line endings: >>> fn = tempfile.mktemp() >>> with open(fn, 'wb') as f: ... f.write('Test:\n\n >>> x = 1 + 1\n\nDone.\n') >>> doctest.testfile(fn, module_relative=False, verbose=False) TestResults(failed=0, attempted=1) >>> os.remove(fn) """ # old_test1, ... used to live in doctest.py, but cluttered it. Note # that these use the deprecated doctest.Tester, so should go away (or # be rewritten) someday.
def old_test1(): r""" >>> from doctest import Tester >>> t = Tester(globs={'x': 42}, verbose=0) >>> t.runstring(r''' ... >>> x = x * 2 ... >>> print x ... 42 ... ''', 'XYZ') ********************************************************************** Line 3, in XYZ Failed example: print x Expected: 42 Got: 84 TestResults(failed=1, attempted=2) >>> t.runstring(">>> x = x * 2\n>>> print x\n84\n", 'example2') TestResults(failed=0, attempted=2) >>> t.summarize() ********************************************************************** 1 items had failures: 1 of 2 in XYZ ***Test Failed*** 1 failures. TestResults(failed=1, attempted=4) >>> t.summarize(verbose=1) 1 items passed all tests: 2 tests in example2 ********************************************************************** 1 items had failures: 1 of 2 in XYZ 4 tests in 2 items. 3 passed and 1 failed. ***Test Failed*** 1 failures. TestResults(failed=1, attempted=4) """
def old_test3(): r""" >>> from doctest import Tester >>> t = Tester(globs={}, verbose=0) >>> def _f(): ... '''Trivial docstring example. ... >>> assert 2 == 2 ... ''' ... return 32 ... >>> t.rundoc(_f) # expect 0 failures in 1 example TestResults(failed=0, attempted=1) """