我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用doctest.run_docstring_examples()。
def doctests(filter=[]): try: import psyco; psyco.full() except ImportError: pass import sys from timeit import default_timer as clock for i, arg in enumerate(sys.argv): if '__init__.py' in arg: filter = [sn for sn in sys.argv[i+1:] if not sn.startswith("-")] break import doctest globs = globals().copy() for obj in globs: #sorted(globs.keys()): if filter: if not sum([pat in obj for pat in filter]): continue sys.stdout.write(str(obj) + " ") sys.stdout.flush() t1 = clock() doctest.run_docstring_examples(globs[obj], {}, verbose=("-v" in sys.argv)) t2 = clock() print(round(t2-t1, 3))
def doctests(): try: import psyco; psyco.full() except ImportError: pass import sys from timeit import default_timer as clock filter = [] for i, arg in enumerate(sys.argv): if '__init__.py' in arg: filter = [sn for sn in sys.argv[i+1:] if not sn.startswith("-")] break import doctest globs = globals().copy() for obj in globs: #sorted(globs.keys()): if filter: if not sum([pat in obj for pat in filter]): continue sys.stdout.write(str(obj) + " ") sys.stdout.flush() t1 = clock() doctest.run_docstring_examples(globs[obj], {}, verbose=("-v" in sys.argv)) t2 = clock() print(round(t2-t1, 3))
def doctests(): try: import psyco; psyco.full() except ImportError: pass import sys from timeit import default_timer as clock filter = [] for i, arg in enumerate(sys.argv): if '__init__.py' in arg: filter = [sn for sn in sys.argv[i+1:] if not sn.startswith("-")] break import doctest globs = globals().copy() for obj in globs: #sorted(globs.keys()): if filter: if not sum([pat in obj for pat in filter]): continue print obj, t1 = clock() doctest.run_docstring_examples(globs[obj], {}, verbose=("-v" in sys.argv)) t2 = clock() print round(t2-t1, 3)
def run_func_docstring(tester, test_func, globs=None, verbose=False, compileflags=None, optionflags=doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE): """ Similar to doctest.run_docstring_examples, but takes a single function/bound method, extracts it's singular docstring (no looking for subobjects with tests), runs it, and most importantly raises an exception if the test doesn't pass. tester should be an instance of dtest.Tester test_func should be a function/bound method the docstring to be tested """ name = test_func.__name__ if globs is None: globs = build_doc_context(tester, name) # dumb function that remembers values that it is called with # the DocTestRunner.run function called below accepts a callable for logging # and this is a hacky but easy way to capture the nicely formatted value for reporting def test_output_capturer(content): if not hasattr(test_output_capturer, 'content'): test_output_capturer.content = '' test_output_capturer.content += content test = doctest.DocTestParser().get_doctest(inspect.getdoc(test_func), globs, name, None, None) runner = doctest.DocTestRunner(verbose=verbose, optionflags=optionflags) runner.run(test, out=test_output_capturer, compileflags=compileflags) failed, attempted = runner.summarize() if failed > 0: raise RuntimeError("Doctest failed! Captured output:\n{}".format(test_output_capturer.content)) if failed + attempted == 0: raise RuntimeError("No tests were run!")