python - Flask 测试 - 为什么覆盖范围不包括导入语句和装饰器?

标签 python testing flask coverage.py

我的测试清楚地执行了每个函数,并且也没有未使用的导入。然而,根据覆盖率报告,62% 的代码从未在以下文件中执行:

enter image description here

有人可以指出我可能做错了什么吗?

这是我初始化测试套件和覆盖率的方法:

    cov = coverage(branch=True, omit=['website/*', 'run_test_suite.py'])
    cov.start()

    try:
        unittest.main(argv=[sys.argv[0]])
    except:
        pass

    cov.stop()
    cov.save()

    print "\n\nCoverage Report:\n"
    cov.report()

    print "HTML version: " + os.path.join(BASEDIR, "tmp/coverage/index.html")
    cov.html_report(directory='tmp/coverage')
    cov.erase()

最佳答案

这是coverage.py FAQ中的第三个问题:

Q: Why do the bodies of functions (or classes) show as executed, but the def lines do not?

This happens because coverage is started after the functions are defined. The definition lines are executed without coverage measurement, then coverage is started, then the function is called. This means the body is measured, but the definition of the function itself is not.

To fix this, start coverage earlier. If you use the command line to run your program with coverage, then your entire program will be monitored. If you are using the API, you need to call coverage.start() before importing the modules that define your functions.



最简单的做法是在覆盖范围内运行测试:
$ coverage run -m unittest discover

您的自定义测试脚本并没有做超出覆盖命令行所能做的事情,只是使用命令行会更简单。

关于python - Flask 测试 - 为什么覆盖范围不包括导入语句和装饰器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39748569/

相关文章:

python - 对残差平方的numpy数组进行快速迭代

Python:使用 FOR 循环插入字典

java - 记录 selenium 测试并从 Java 运行它们

python - 我如何模拟 PyMongo 以使用 Flask 应用程序进行测试?

python - 用 Python 读取 CSV

python - 在访问日志中标记并总结每月的差距(流失)

testing - 软件测试时使用日志分析工具?

t-sql - 如何使用 tSQLt 测试更新表(相对于返回结果集)的 SQL 存储过程

python - 访问 sqlalchemy 关联对象中的额外数据

python - 如何将 flask 配置为可通过公共(public) IP 接口(interface)访问?