本文整理汇总了Python中doctest.DocTest方法的典型用法代码示例。如果您正苦于以下问题:Python doctest.DocTest方法的具体用法?Python doctest.DocTest怎么用?Python doctest.DocTest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类doctest
的用法示例。
在下文中一共展示了doctest.DocTest方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_examples
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import DocTest [as 别名]
def run_examples(self, exs):
# runs the Example objects, keeps track of right/wrong etc
total_failed = 0
total_attempted = 0
case = 'shared'
for sui in exs.keys():
if not total_failed:
final_env = dict(self.good_env)
if 'shared' in exs[sui].keys():
dtest = DocTest(exs[sui]['shared'], self.good_env, 'shared', None, None, None)
result = self.runner.run(dtest, clear_globs=False)
# take the env from shared dtest and save it for other exs
final_env = dict(self.good_env, **dtest.globs)
total_failed += result.failed
total_attempted += result.attempted
for case in exs[sui].keys():
if case != 'shared':
if not total_failed:
example_name = "Suite {}, Case {}".format(sui, case)
dtest = DocTest(exs[sui][case], final_env, example_name, None, None, None)
result = self.runner.run(dtest)
total_failed += result.failed
total_attempted += result.attempted
return total_failed, total_attempted
示例2: run
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import DocTest [as 别名]
def run(self, test: DocTest, *args: Any, **kwargs: Any) -> Any:
for ex in test.examples:
ex.source = test_template.format(test=indent(ex.source, " ").strip())
return self._runner.run(test, *args, **kwargs)
示例3: run_doctest
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import DocTest [as 别名]
def run_doctest(name, doctest_string, global_environment):
"""
Run a single test with given global_environment.
Returns (True, '') if the doctest passes.
Returns (False, failure_message) if the doctest fails.
"""
examples = doctest.DocTestParser().parse(
doctest_string,
name
)
test = doctest.DocTest(
[e for e in examples if isinstance(e, doctest.Example)],
global_environment,
name,
None,
None,
doctest_string
)
doctestrunner = doctest.DocTestRunner(verbose=True)
runresults = io.StringIO()
with redirect_stdout(runresults), redirect_stderr(runresults), hide_outputs():
doctestrunner.run(test, clear_globs=False)
with open(os.devnull, 'w') as f, redirect_stderr(f), redirect_stdout(f):
result = doctestrunner.summarize(verbose=True)
# An individual test can only pass or fail
if result.failed == 0:
return (True, '')
else:
return False, runresults.getvalue()
示例4: run_doctest
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import DocTest [as 别名]
def run_doctest(name, doctest_string, global_environment):
"""
Run a single test with given global_environment.
Returns (True, '') if the doctest passes.
Returns (False, failure_message) if the doctest fails.
Args:
name (str): Name of doctest
doctest_string (str): Doctest in string form
global_environment (dict of str: str): Global environment resulting from the execution
of a python script/notebook
Returns:
(bool, str): Results from running the test
"""
examples = doctest.DocTestParser().parse(
doctest_string,
name
)
test = doctest.DocTest(
[e for e in examples if isinstance(e, doctest.Example)],
global_environment,
name,
None,
None,
doctest_string
)
doctestrunner = doctest.DocTestRunner(verbose=True)
runresults = io.StringIO()
with redirect_stdout(runresults), redirect_stderr(runresults), hide_outputs():
doctestrunner.run(test, clear_globs=False)
with open(os.devnull, 'w') as f, redirect_stderr(f), redirect_stdout(f):
result = doctestrunner.summarize(verbose=True)
# An individual test can only pass or fail
if result.failed == 0:
return (True, '')
else:
return False, runresults.getvalue()
示例5: make_suite
# 需要导入模块: import doctest [as 别名]
# 或者: from doctest import DocTest [as 别名]
def make_suite(): # pragma: no cover
from calmjs.parse.lexers import es5 as es5lexer
from calmjs.parse import walkers
from calmjs.parse import sourcemap
def open(p, flag='r'):
result = StringIO(examples[p] if flag == 'r' else '')
result.name = p
return result
parser = doctest.DocTestParser()
optflags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
dist = get_distribution('calmjs.parse')
if dist:
if dist.has_metadata('PKG-INFO'):
pkgdesc = dist.get_metadata('PKG-INFO').replace('\r', '')
elif dist.has_metadata('METADATA'):
pkgdesc = dist.get_metadata('METADATA').replace('\r', '')
else:
pkgdesc = ''
pkgdesc_tests = [
t for t in parser.parse(pkgdesc) if isinstance(t, doctest.Example)]
test_loader = unittest.TestLoader()
test_suite = test_loader.discover(
'calmjs.parse.tests', pattern='test_*.py',
top_level_dir=dirname(__file__)
)
test_suite.addTest(doctest.DocTestSuite(es5lexer, optionflags=optflags))
test_suite.addTest(doctest.DocTestSuite(walkers, optionflags=optflags))
test_suite.addTest(doctest.DocTestSuite(sourcemap, optionflags=optflags))
test_suite.addTest(doctest.DocTestCase(
# skipping all the error case tests which should all be in the
# troubleshooting section at the end; bump the index whenever
# more failure examples are added.
# also note that line number is unknown, as PKG_INFO has headers
# and also the counter is somehow inaccurate in this case.
doctest.DocTest(pkgdesc_tests[:-1], {
'open': open}, 'PKG_INFO', 'README.rst', None, pkgdesc),
optionflags=optflags,
))
return test_suite