Currently have a project configured to run coverage via Django's manage command like so:
目前有一个项目配置为通过Django的管理命令运行覆盖,如下所示:
./manage.py test --with-coverage --cover-package=notify --cover-branches --cover-inclusive --cover-erase
This results in a report like the following:
这会产生如下报告:
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%
There's a problem with this report however. It's wrong. Coverage is marking lines missing, despite the fact that they are indeed being covered by tests. For example, if I run the tests via nosetests
instead of django's manage command I get the following correct report:
但是,这份报告存在问题。这是不对的。覆盖范围是标记线丢失,尽管它们确实被测试覆盖。例如,如果我通过nosetests而不是django的manage命令运行测试,我会得到以下正确的报告:
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 led me to the coverage website's FAQ, http://nedbatchelder.com/code/coverage/faq.html
谷歌带领我进入了覆盖网站的常见问题解答,http://nedbatchelder.com/code/coverage/faq.html
Q: Why do the bodies of functions (or classes) show as executed, but the def lines do not?
问:为什么函数(或类)的主体显示为已执行,但def行不显示?
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.
要解决此问题,请提前开始报道。如果使用命令行以覆盖范围运行程序,则将监视整个程序。如果您使用的是API,则需要在导入定义函数的模块之前调用coverage.start()。
The question is, can I run the coverage reports properly via Django's manage command? Or do I have to bypass manage to avoid the situation where coverage is started after the "missing" lines are executed?
问题是,我可以通过Django的manage命令正确运行覆盖率报告吗?或者我是否必须绕过管理以避免在执行“缺失”行之后启动覆盖的情况?
4 个解决方案
#1
12
At the moment it's not possible to accurately run coverage alongside with django-nose (because of the way Django 1.7 loads models). So to get the coverage stats, you need to use coverage.py directly from command line, e.g:
目前,不可能准确地与django-nose一起运行覆盖(因为Django 1.7加载模型的方式)。因此,要获取覆盖率统计信息,您需要直接从命令行使用coverage.py,例如:
$ coverage run --branch --source=app1,app2 ./manage.py test
$ coverage report
$ coverage html -d coverage-report
You can put coverage.py settings into .coveragerc file in the project root (the same dir as manage.py).
您可以将coverage.py设置放入项目根目录中的.coveragerc文件(与manage.py相同的目录)。
This issue is reported on django-nose GitHub page: https://github.com/django-nose/django-nose/issues/180 so maintainers know about the problem, you can let them know that you're also experiencing this issue.
这个问题在django-nose GitHub页面上报道:https://github.com/django-nose/django-nose/issues/180所以维护人员知道这个问题,你可以让他们知道你也遇到了这个问题。
UPDATE
UPDATE
eliangcs pointed out (django-nose issues on GiHub), that woraround is to modify your manage.py
:
eliangcs指出(GiHub上的django-nose问题),那就是修改你的manage.py:
import os
import sys
if __name__ == "__main__":
# ...
from django.core.management import execute_from_command_line
is_testing = 'test' in sys.argv
if is_testing:
import coverage
cov = coverage.coverage(source=['package1', 'package2'], omit=['*/tests/*'])
cov.erase()
cov.start()
execute_from_command_line(sys.argv)
if is_testing:
cov.stop()
cov.save()
cov.report()
It works, but it's rather "hacky" approach.
它有效,但它是相当“hacky”的方法。
UPDATE 2
更新2
I recommend for everybody that uses nose to have a look at py.test (http://pytest.org/), which is really good Python testing tool, it integrates well with Django, has a lot of plugins and many more. I was using django-nose, but tried py.test and never looked back.
我建议所有使用鼻子的人看看py.test(http://pytest.org/),这是一个非常好的Python测试工具,它与Django很好地集成,有很多插件等等。我正在使用django-nose,但尝试了py.test并且从未回头。
#2
5
As the docs say, "use the command line to run your program with coverage":
正如文档所说,“使用命令行来运行覆盖范围的程序”:
coverage run --branch --source=notify ./manage.py test
#3
0
I've managed to get this working including a
我设法让这个工作包括一个
import coverage
on top of my manage.py file (I'm using Flask instead but having the very same issue)
在我的manage.py文件之上(我使用的是Flask,但却有同样的问题)
My problem is that it works from console but Jenkins is not aware of it and keeps on saying that those imports are out of the tests...
我的问题是它可以从控制台运行,但Jenkins并不知道它,并继续说这些导入不在测试中......
Any idea?
任何想法?
#4
0
I had the same problem using a remote interpreter in a virtual machine through the ssh configuration. The solution was to set my tests directory and ALL its parent directories in the "Path mappings" of the "Environment" section of "Run" > "Edit Configurations...".
我通过ssh配置在虚拟机中使用远程解释器时遇到了同样的问题。解决方案是在“运行”>“编辑配置...”的“环境”部分的“路径映射”中设置我的测试目录及其所有父目录。
#1
12
At the moment it's not possible to accurately run coverage alongside with django-nose (because of the way Django 1.7 loads models). So to get the coverage stats, you need to use coverage.py directly from command line, e.g:
目前,不可能准确地与django-nose一起运行覆盖(因为Django 1.7加载模型的方式)。因此,要获取覆盖率统计信息,您需要直接从命令行使用coverage.py,例如:
$ coverage run --branch --source=app1,app2 ./manage.py test
$ coverage report
$ coverage html -d coverage-report
You can put coverage.py settings into .coveragerc file in the project root (the same dir as manage.py).
您可以将coverage.py设置放入项目根目录中的.coveragerc文件(与manage.py相同的目录)。
This issue is reported on django-nose GitHub page: https://github.com/django-nose/django-nose/issues/180 so maintainers know about the problem, you can let them know that you're also experiencing this issue.
这个问题在django-nose GitHub页面上报道:https://github.com/django-nose/django-nose/issues/180所以维护人员知道这个问题,你可以让他们知道你也遇到了这个问题。
UPDATE
UPDATE
eliangcs pointed out (django-nose issues on GiHub), that woraround is to modify your manage.py
:
eliangcs指出(GiHub上的django-nose问题),那就是修改你的manage.py:
import os
import sys
if __name__ == "__main__":
# ...
from django.core.management import execute_from_command_line
is_testing = 'test' in sys.argv
if is_testing:
import coverage
cov = coverage.coverage(source=['package1', 'package2'], omit=['*/tests/*'])
cov.erase()
cov.start()
execute_from_command_line(sys.argv)
if is_testing:
cov.stop()
cov.save()
cov.report()
It works, but it's rather "hacky" approach.
它有效,但它是相当“hacky”的方法。
UPDATE 2
更新2
I recommend for everybody that uses nose to have a look at py.test (http://pytest.org/), which is really good Python testing tool, it integrates well with Django, has a lot of plugins and many more. I was using django-nose, but tried py.test and never looked back.
我建议所有使用鼻子的人看看py.test(http://pytest.org/),这是一个非常好的Python测试工具,它与Django很好地集成,有很多插件等等。我正在使用django-nose,但尝试了py.test并且从未回头。
#2
5
As the docs say, "use the command line to run your program with coverage":
正如文档所说,“使用命令行来运行覆盖范围的程序”:
coverage run --branch --source=notify ./manage.py test
#3
0
I've managed to get this working including a
我设法让这个工作包括一个
import coverage
on top of my manage.py file (I'm using Flask instead but having the very same issue)
在我的manage.py文件之上(我使用的是Flask,但却有同样的问题)
My problem is that it works from console but Jenkins is not aware of it and keeps on saying that those imports are out of the tests...
我的问题是它可以从控制台运行,但Jenkins并不知道它,并继续说这些导入不在测试中......
Any idea?
任何想法?
#4
0
I had the same problem using a remote interpreter in a virtual machine through the ssh configuration. The solution was to set my tests directory and ALL its parent directories in the "Path mappings" of the "Environment" section of "Run" > "Edit Configurations...".
我通过ssh配置在虚拟机中使用远程解释器时遇到了同样的问题。解决方案是在“运行”>“编辑配置...”的“环境”部分的“路径映射”中设置我的测试目录及其所有父目录。