请教: Python coverage 和 unittest 结合报错,狗书单元测试这一块

2017-12-12 10:01:54 +08:00
 wuwuwu5

最近在看 flask web 开发一书,看到获取代码测试报告那里,对照的书,使用脚本启动 coverage。运行的时候报错 AssertionError: Expected current collector to be <Collector at 0x10366b0d0: CTracer>, but it's <Collector at 0x103cac4d0: CTracer>

这是我的代码 app = creat_app('development') app.app_context().push() app.config['debug'] = True manager = Manager(app)

COV = None if os.environ.get('FLASK_COVERAGE'): import coverage COV = coverage.coverage(branch=True, include='app/*') COV.start()

@manager.command def test(cover=False): """run the unittest""" if cover and not os.environ.get('FLASK_COVERAGE'): import sys os.environ['FLASK_COVERAGE'] = '1' os.execvp(sys.executable, [sys.executable] + sys.argv) import unittest tests = unittest.TestLoader().discover('test') unittest.TextTestRunner(verbosity=2).run(tests) if COV: COV.stop() COV.save() print 'Coverage Summary:' COV.report() basedir = os.path.abspath(os.path.dirname(file)) covdir = os.path.join(basedir, 'tmp/coverage') COV.html_report(directory=covdir) print 'html version: file://%s/index.html' % covdir COV.erase()

if name == 'main': print app.url_map db.drop_all() db.create_all() manager.run()

COV.stop() 这一句报错 我终端运行的是 python manager.py test --cover

1948 次点击
所在节点    Python
1 条回复
wuwuwu5
2017-12-12 10:08:48 +08:00
app = creat_app('development')
app.app_context().push()
app.config['debug'] = True
manager = Manager(app)

COV = None
if os.environ.get('FLASK_COVERAGE'):
import coverage
COV = coverage.coverage(branch=True, include='app/*')
COV.start()


@manager.command
def test(cover=False):
"""run the unittest"""
if cover and not os.environ.get('FLASK_COVERAGE'):
import sys
os.environ['FLASK_COVERAGE'] = '1'
os.execvp(sys.executable, [sys.executable] + sys.argv)
import unittest
tests = unittest.TestLoader().discover('test')
unittest.TextTestRunner(verbosity=2).run(tests)
if COV:
COV.stop()
COV.save()
print 'Coverage Summary:'
COV.report()
basedir = os.path.abspath(os.path.dirname(__file__))
covdir = os.path.join(basedir, 'tmp/coverage')
COV.html_report(directory=covdir)
print 'html version: file://%s/index.html' % covdir
COV.erase()


if __name__ == '__main__':
print app.url_map
db.drop_all()
db.create_all()
manager.run()

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/413980

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX