gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 20:25:55 25 4
gpt4 key购买 nike

我的测试清楚地执行了每个函数,并且也没有未使用的导入。然而,根据覆盖率报告,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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com