【发布时间】:2014-07-10 03:50:14
【问题描述】:
目前有一个项目配置为通过 Django 的管理命令运行覆盖,如下所示:
./manage.py test --with-coverage --cover-package=notify --cover-branches --cover-inclusive --cover-erase
这会生成如下报告:
Name Stmts Miss Branch BrMiss Cover Missing
--------------------------------------------------------------------------
notify.decorators 4 1 0 0 75% 4
notify.handlers 6 1 2 0 88% 11
notify.notification_types 46 39 2 0 19% 8-55, 59, 62, 66
notify.notifications 51 51 0 0 0% 11-141
--------------------------------------------------------------------------
TOTAL 107 92 4 0 17%
但是,此报告存在问题。这是错的。覆盖是标记线缺失,尽管它们确实被测试覆盖。例如,如果我通过 nosetests 而不是 django 的管理命令运行测试,我会得到以下正确报告:
Name Stmts Miss Branch BrMiss Cover Missing
-----------------------------------------------------------------------------
notify.decorators 4 0 0 0 100%
notify.handlers 6 0 2 0 100%
notify.notification_types 46 0 2 0 100%
notify.notifications 51 25 0 0 51% 13, 18, 23, 28, 33, 38, 43, 48, 53, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 116, 121, 126, 131, 136, 141
-----------------------------------------------------------------------------
TOTAL 107 25 4 0 77%
Google 将我带到了报道网站的常见问题解答,http://nedbatchelder.com/code/coverage/faq.html
问:为什么函数(或类)的主体显示为已执行,但 def 行没有?
这是因为在定义函数之后才开始覆盖。定义行在没有覆盖测量的情况下执行,然后开始覆盖,然后调用函数。这意味着身体被测量,但函数本身的定义不是。
要解决此问题,请尽早开始覆盖。如果你使用命令行来运行你的程序,那么你的整个程序都会被监控。如果您使用 API,则需要在导入定义您的函数的模块之前调用 coverage.start()。
问题是,我可以通过 Django 的管理命令正确运行覆盖率报告吗?还是我必须绕过管理以避免在执行“缺失”行之后开始覆盖的情况?
【问题讨论】:
标签: python django code-coverage django-nose