首页 > Flask Web Development 第十五章 覆盖检测代码为什么这么写?

Flask Web Development 第十五章 覆盖检测代码为什么这么写?

代码覆盖集成进flask-script的test命令中。为什么要写的这么麻烦,用到环境变量,直接写进test函数里不行吗?这个FLASK_COVERAGE环境变量如果自已设定在命令行环境里,那不是运行其他脚本命令(如:python manage.py shell, python manage.py db upgrade)也要开启coverage引擎?
下面是Flask Web Development书中第十五章 测试 中的代码

manage.py: 覆盖检测

#!/usr/bin/env python
import os
COV = None
if os.environ.get('FLASK_COVERAGE'):
    import coverage
    COV = coverage.coverage(branch=True, include='app/*')
    COV.start()

# ...

@manager.command
def test(coverage=False):
    """Run the unit tests."""
    if coverage 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('tests')
    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()

# ...
【热门文章】
【热门文章】