我们从Python开源项目中,提取了以下34个代码示例,用于说明如何使用doctest.testfile()。
def collect(self): import doctest # inspired by doctest.testfile; ideally we would use it directly, # but it doesn't support passing a custom checker text = self.fspath.read() filename = str(self.fspath) name = self.fspath.basename globs = {'__name__': '__main__'} optionflags = get_optionflags(self) runner = doctest.DebugRunner(verbose=0, optionflags=optionflags, checker=_get_checker()) parser = doctest.DocTestParser() test = parser.get_doctest(text, globs, name, filename, 0) if test.examples: yield DoctestItem(test.name, self, runner, test)
def runtest(self): import doctest fixture_request = _setup_fixtures(self) # inspired by doctest.testfile; ideally we would use it directly, # but it doesn't support passing a custom checker text = self.fspath.read() filename = str(self.fspath) name = self.fspath.basename globs = dict(getfixture=fixture_request.getfuncargvalue) if '__name__' not in globs: globs['__name__'] = '__main__' optionflags = get_optionflags(self) runner = doctest.DebugRunner(verbose=0, optionflags=optionflags, checker=_get_unicode_checker()) parser = doctest.DocTestParser() test = parser.get_doctest(text, globs, name, filename, 0) _check_all_skipped(test) runner.run(test)
def doctest_files(file_list=files_for_doctest, **kwargs): """doctest all (listed) files of the `cma` package. Details: accepts ``verbose`` and all other keyword arguments that `doctest.testfile` would accept, while negative ``verbose`` values are passed as 0. """ # print("__name__ is", __name__, sys.modules[__name__]) # print(__package__) if not isinstance(file_list, list) and is_str(file_list): file_list = [file_list] verbosity_here = kwargs.get('verbose', 0) if verbosity_here < 0: kwargs['verbose'] = 0 failures = 0 for file_ in file_list: file_ = file_.strip().strip(os.path.sep) if file_.startswith('cma' + os.path.sep): file_ = file_[4:] if verbosity_here >= 0: print('doctesting %s ...' % file_, ' ' * (max(len(_file) for _file in file_list) - len(file_)), end="") # does not work in Python 2.5 sys.stdout.flush() protected_files = os.listdir('.') report = doctest.testfile(file_, package=__package__, # 'cma', # sys.modules[__name__], **kwargs) _clean_up('.', _files_written, protected_files) failures += report[0] if verbosity_here >= 0: print(report) return failures
def testDoc(filename, name=None): print "--- %s: Run tests" % filename failure, nb_test = testfile( filename, optionflags=ELLIPSIS, name=name) if failure: exit(1) print "--- %s: End of tests" % filename
def test(cls, verbose=False): res = doctest.testfile( TEST_FILE, globs={'ConcreteTombola': cls}, # <5> verbose=verbose, optionflags=doctest.REPORT_ONLY_FIRST_FAILURE) tag = 'FAIL' if res.failed else 'OK' print(TEST_MSG.format(cls.__name__, res, tag)) # <6>
def test(cls, verbose=False): res = doctest.testfile( TEST_FILE, globs={'Sentence': cls}, verbose=verbose, optionflags=doctest.REPORT_ONLY_FIRST_FAILURE) tag = 'FAIL' if res.failed else 'OK' print(TEST_MSG.format(cls.__module__, res, tag))
def test(gen_factory, verbose=False): res = doctest.testfile( TEST_FILE, globs={'aritprog_gen': gen_factory}, verbose=verbose, optionflags=doctest.REPORT_ONLY_FIRST_FAILURE) tag = 'FAIL' if res.failed else 'OK' print(TEST_MSG.format(gen_factory.__module__, res, tag))
def test(): import doctest doctest.testfile('iso2709_test.txt')
def test(verbose=None): """Run some tests, taken from the chapter. Since the hillclimbing algorithm is randomized, some tests may fail.""" import doctest print('Running tests...') doctest.testfile('ngrams-test.txt', verbose=verbose) ################ Word Segmentation (p. 223)
def doDoctestFile(self, module): log = [] old_master, doctest.master = doctest.master, None try: doctest.testfile( 'xyz.txt', package=module, module_relative=True, globs=locals() ) finally: doctest.master = old_master self.assertEqual(log,[True])
def test_readme(self): # XXX: This fails but doesn't fail the build. # (and the syntax isn't valid on all pythons so that seems a little # hard to get right. doctest.testfile('../README.rst')
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 test(): import doctest doctest.NORMALIZE_WHITESPACE = 1 doctest.testfile("README.txt", verbose=1)
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, 'w') as fobj: ... fobj.write('Test:\r\n\r\n >>> x = 1 + 1\r\n\r\nDone.\r\n') >>> doctest.testfile(fn, False) TestResults(failed=0, attempted=1) >>> os.remove(fn) And now *nix line endings: >>> fn = tempfile.mktemp() >>> with open(fn, 'w') as fobj: ... fobj.write('Test:\n\n >>> x = 1 + 1\n\nDone.\n') >>> doctest.testfile(fn, 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 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(b'Test:\r\n\r\n >>> x = 1 + 1\r\n\r\nDone.\r\n') 35 >>> doctest.testfile(fn, 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(b'Test:\n\n >>> x = 1 + 1\n\nDone.\n') 30 >>> doctest.testfile(fn, False) TestResults(failed=0, attempted=1) >>> os.remove(fn) """
def test_rst(name): failure_count, test_count = doctest.testfile(os.path.join(EX, name + '.rst'), globs={'meta': meta, 'pprint': pprint}, module_relative=False) if name in ('datetime_now', 'property_codec_json'): return if name == 'codec' and PY2: return assert failure_count == 0
def test_guide(name): failure_count, test_count = doctest.testfile(os.path.join(GUIDE, name + '.rst'), globs={'meta': meta, 'pprint': pprint}, module_relative=False) assert failure_count == 0
def check_doc(self, filename, subdir=None, name=None): if self.verbose: print("--- %s: Run tests" % filename) if not subdir: fullpath = os.path.join('..', 'doc', filename) else: fullpath = os.path.join(subdir, filename) failure, nb_test = doctest.testfile( fullpath, optionflags=doctest.ELLIPSIS, name=name) if failure: self.fail("error") if self.verbose: print("--- %s: End of tests" % filename)
def test_doctest_show_scroll(self): print('... display show and scroll doctests - be patient! ...') doctest.testfile(TestDisplay.__DOCTEST_FILE)
def testDoc(filename, name=None): print("--- %s: Run tests" % filename) failure, nb_test = testfile( filename, optionflags=ELLIPSIS, name=name) if failure: exit(1) print("--- %s: End of tests" % filename)