python中doctest_Python doctest.DocTest方法代码示例

本文介绍了如何在Python中使用doctest模块,包括导入、DocTestParser、测试套件的创建以及选项标志的设置。通过示例展示了如何从包的元数据中提取测试用例,并将它们添加到测试套件中进行执行。
摘要由CSDN通过智能技术生成

# 需要导入模块: 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值