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.getFLASK_COVERAGE: import coverage COV = coverage.coveragebranch=True, include=app/* COV.start # ... @manager.command
def testcoverage=False: """Run the unit tests.""" if coverage and not os.environ.getFLASK_COVERAGE: import sys os.environ[FLASK_COVERAGE] = 1 os.execvpsys.executable, [sys.executable] + sys.argv import unittest tests = unittest.TestLoader.discovertests unittest.TextTestRunnerverbosity=2.runtests if COV: COV.stop COV.save printCoverage Summary: COV.report basedir = os.path.abspathos.path.dirname__file__ covdir = os.path.joinbasedir, tmp/coverage COV.html_reportdirectory=covdir printHTML version: file://%s/index.html % covdir COV.erase # ...

发表评论

电子邮件地址不会被公开。 必填项已用*标注