V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wuwuwu5
V2EX  ›  Python

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

  •  
  •   wuwuwu5 · 2017-12-12 10:01:54 +08:00 · 1949 次点击
    这是一个创建于 2541 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近在看 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

    wuwuwu5
        1
    wuwuwu5  
    OP
       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()
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2135 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 00:01 · PVG 08:01 · LAX 16:01 · JFK 19:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.