本文整理汇总了Python中doctest.py方法的典型用法代码示例。如果您正苦于以下问题:Python doctest.py方法的具体用法?Python doctest.py怎么用?Python doctest.py使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类doctest
的用法示例。
在下文中一共展示了doctest.py方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wantFile
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import py [as 别名]
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
示例2: test_lineendings
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import py [as 别名]
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.
示例3: matches
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import py [as 别名]
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])))
示例4: test_lineendings
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import py [as 别名]
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()
>>> open(fn, 'w').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()
>>> open(fn, 'w').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.
示例5: debug_script
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import py [as 别名]
def debug_script(src, pm=False, globs=None):
"Debug a test script. `src` is the script, as a string."
import pdb
# Note that tempfile.NameTemporaryFile() cannot be used. As the
# docs say, a file so created cannot be opened by name a second time
# on modern Windows boxes, and execfile() needs to open it.
srcfilename = tempfile.mktemp(".py", "doctestdebug")
with open(srcfilename, 'w') as fp:
fp.write(src)
try:
if globs:
globs = globs.copy()
else:
globs = {}
if pm:
try:
execfile(srcfilename, globs, globs)
except:
print(sys.exc_info()[1])
pdb.post_mortem(sys.exc_info()[2])
else:
# Note that %r is vital here. '%s' instead can, e.g., cause
# backslashes to get treated as metacharacters on Windows.
pdb.run("execfile(%r)" % srcfilename, globs, globs)
finally:
os.remove(srcfilename)
示例6: run_test_file_with_config
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import py [as 别名]
def run_test_file_with_config(filename, globs, optionflags):
"""Modified from doctest.py to use custom checker."""
text, filename = _load_testfile(filename)
name = os.path.basename(filename)
if globs is None:
globs = {}
else:
globs = globs.copy()
if '__name__' not in globs:
globs['__name__'] = '__main__'
checker = Py23DocChecker()
runner = TestRunner(checker=checker, verbose=None, optionflags=optionflags)
parser = doctest.DocTestParser()
test = parser.get_doctest(text, globs, name, filename, 0)
flags = (
print_function.compiler_flag
| absolute_import.compiler_flag
| division.compiler_flag
)
runner.run(test, flags)
results = runner.summarize()
if doctest.master is None:
doctest.master = runner
else:
doctest.master.merge(runner)
return results