我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用doctest.py()。
def wantFile(self, file): """Override to select all modules and any file ending with configured doctest extension. """ # always want .py files if file.endswith('.py'): return True # also want files that match my extension if (self.extension and anyp(file.endswith, self.extension) and (not self.conf.exclude or not filter(None, [exc.search(file) for exc in self.conf.exclude]))): return True return None
def run( self ): # Save working directory and args old_argv = sys.argv old_cwd = os.getcwd() # Build command line for Epydoc sys.argv = """epydoc.py bx --verbose --html --simple-term --exclude=._ --exclude=_tests --docformat=reStructuredText --output=../doc/docbuild/html/apidoc""".split() # Make output directory if not os.path.exists( "./doc/docbuild/html/apidoc" ): os.mkdir( "./doc/docbuild/html/apidoc" ) # Move to lib directory (so bx package is in current directory) os.chdir( "./lib" ) # Invoke epydoc epydoc.cli.cli() # Restore args and working directory sys.argv = old_argv os.chdir( old_cwd ) # Add to extra_commands
def matches(self, name): # FIXME this seems wrong -- nothing is ever going to # fail this test, since we're given a module NAME not FILE if name == '__init__.py': return False # FIXME don't think we need include/exclude checks here? return ((self.doctest_tests or not self.conf.testMatch.search(name) or (self.conf.include and filter(None, [inc.search(name) for inc in self.conf.include]))) and (not self.conf.exclude or not filter(None, [exc.search(name) for exc in self.conf.exclude])))
def monkey_patch_doctest(): # # Doctest and coverage don't get along, so we need to create # a monkeypatch that will replace the part of doctest that # interferes with coverage reports. # # The monkeypatch is based on this zope patch: # http://svn.zope.org/Zope3/trunk/src/zope/testing/doctest.py?rev=28679&r1=28703&r2=28705 # try: import doctest _orp = doctest._OutputRedirectingPdb class NoseOutputRedirectingPdb(_orp): def __init__(self, out): self.__debugger_used = False _orp.__init__(self, out) def set_trace(self): self.__debugger_used = True _orp.set_trace(self) def set_continue(self): # Calling set_continue unconditionally would break unit test coverage # reporting, as Bdb.set_continue calls sys.settrace(None). if self.__debugger_used: _orp.set_continue(self) doctest._OutputRedirectingPdb = NoseOutputRedirectingPdb except: pass
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_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.