我在Django网站上运行简单的鼻子测试(代码可用here)。根据CLI命令的发布顺序,我会得到截然不同的结果!
查看很多帖子,看起来与加载过程的覆盖时间有关。我不明白的是:
我专注于minerals/views.py
Django settings.py
文件包含配置信息:
# Use nose to run all tests
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
# Nose will measure coverage on the folllowing apps
NOSE_ARGS = [
'--with-coverage',
'--cover-package=minerals',
'--cover-html',
]
我正在运行Ubuntu 14.04LTS,使用Python 3.5.1,使用virtualenv需求文件:
coverage==4.1
Django==1.9.6
django-debug-toolbar==1.4
django-nose==1.4.3
nose==1.3.7
sqlparse==0.1.19
当我使用coverage run --source '.' manage.py test minerals
运行测试时,minerals/views.py
缺少语句:12-13,18-49 。
$ coverage run --source '.' manage.py test minerals
nosetests minerals --with-coverage --cover-package=minerals --cover-html --verbosity=1
Creating test database for alias 'default'...
..
Name Stmts Miss Cover
--------------------------------------------------------------------
[...]
minerals/views.py 18 8 56%
--------------------------------------------------------------------
TOTAL 73 48 34%
----------------------------------------------------------------------
Ran 2 tests in 0.809s
注意:是的,由于某些未知原因,上述运行与下面报告之间的错过报表数量会发生变化。
完整报道report -m
(来自文件report_source
):
Name Stmts Miss Cover Missing
---------------------------------------------------------------------------
manage.py 6 0 100%
mineral_catalog/__init__.py 0 0 100%
mineral_catalog/settings.py 21 0 100%
mineral_catalog/urls.py 6 0 100%
mineral_catalog/views.py 4 1 75% 6
mineral_catalog/wsgi.py 4 4 0% 10-16
minerals/__init__.py 0 0 100%
minerals/admin.py 3 0 100%
minerals/apps.py 3 3 0% 1-5
minerals/migrations/0001_initial.py 6 6 0% 3-15
minerals/migrations/0002_auto_20160608_1153.py 5 5 0% 3-14
minerals/migrations/__init__.py 0 0 100%
minerals/models.py 24 1 96% 30
minerals/templatetags/__init__.py 0 0 100%
minerals/templatetags/mineral_extras.py 12 6 50% 13-16, 22-23
minerals/tests.py 20 20 0% 1-31
minerals/urls.py 5 0 100%
minerals/views.py 18 11 39% 12-13, 18-49
------------------------------------------------------------------------------
TOTAL 137 57 58%
如果我使用./manage.py test minerals
运行测试,则minerals/views.py
缺少语句1-10,16,42 。
$ ./manage.py test minerals
nosetests minerals --with-coverage --cover-package=minerals --cover-html --verbosity=1
Creating test database for alias 'default'...
..
Name Stmts Miss Cover
--------------------------------------------------------------------
[...]
minerals/views.py 18 8 56%
--------------------------------------------------------------------
TOTAL 73 48 34%
----------------------------------------------------------------------
Ran 2 tests in 0.818s
完整报道report -m
(来自文件report_manage
):
Name Stmts Miss Cover Missing
------------------------------------------------------------------------------
minerals/__init__.py 0 0 100%
minerals/admin.py 3 3 0% 1-6
minerals/apps.py 3 3 0% 1-5
minerals/migrations/0001_initial.py 6 0 100%
minerals/migrations/0002_auto_20160608_1153.py 5 0 100%
minerals/migrations/__init__.py 0 0 100%
minerals/models.py 24 24 0% 1-30
minerals/templatetags/__init__.py 0 0 100%
minerals/templatetags/mineral_extras.py 12 8 33% 1-10, 19-23
minerals/tests.py 20 0 100%
minerals/urls.py 5 5 0% 1-10
minerals/views.py 18 8 56% 1-10, 16, 42
------------------------------------------------------------------------------
TOTAL 96 51 47%
如果我在测试运行之间删除cover/
目录和.coverage
文件,则上面的方法1和2每次都会产生相同的结果。但是,如果我运行方法1,然后运行方法2(从方法1中保留cover/
目录和.coverage
),我会得到很多不同的结果。 minerals/views.py
仅缺少声明42 。
$ ./manage.py test minerals
nosetests minerals --with-coverage --cover-package=minerals --cover-html --verbosity=1
Creating test database for alias 'default'...
..
Name Stmts Miss Cover
--------------------------------------------------------------------
[...]
minerals/views.py 18 1 94%
--------------------------------------------------------------------
TOTAL 73 4 95%
----------------------------------------------------------------------
Ran 2 tests in 0.788s
完整报道report -m
(来自档案report_sourceTHENmanage
):
Name Stmts Miss Cover Missing
------------------------------------------------------------------------------
manage.py 6 0 100%
mineral_catalog/__init__.py 0 0 100%
mineral_catalog/settings.py 21 0 100%
mineral_catalog/urls.py 6 0 100%
mineral_catalog/views.py 4 1 75% 6
mineral_catalog/wsgi.py 4 4 0% 10-16
minerals/__init__.py 0 0 100%
minerals/admin.py 3 0 100%
minerals/apps.py 3 3 0% 1-5
minerals/migrations/0001_initial.py 6 0 100%
minerals/migrations/0002_auto_20160608_1153.py 5 0 100%
minerals/migrations/__init__.py 0 0 100%
minerals/models.py 24 1 96% 30
minerals/templatetags/__init__.py 0 0 100%
minerals/templatetags/mineral_extras.py 12 2 83% 22-23
minerals/tests.py 20 0 100%
minerals/urls.py 5 0 100%
minerals/views.py 18 1 94% 42
------------------------------------------------------------------------------
TOTAL 137 12 91%
我不确定如何从这一点开始。我希望能够使用单个CLI命令获得一致的结果。
如果我需要提供更多信息以帮助调试,请告知我们。
更新
尝试使用py.test
,我可以使用以下方式获得单行覆盖:
$ coverage run --source minerals -m py.test minerals/tests.py
============================================ test session starts =============================================
platform linux -- Python 3.5.1, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
django settings: mineral_catalog.settings (from ini file)
rootdir: [...]/mineral_catalog, inifile: pytest.ini
plugins: django-2.9.1
collected 2 items
minerals/tests.py ..
========================================= 2 passed in 10.88 seconds ==========================================
$ coverage report -m
Name Stmts Miss Cover Missing
------------------------------------------------------------------------------
minerals/__init__.py 0 0 100%
minerals/admin.py 3 0 100%
minerals/apps.py 3 3 0% 1-5
minerals/migrations/0001_initial.py 6 0 100%
minerals/migrations/0002_auto_20160608_1153.py 5 0 100%
minerals/migrations/__init__.py 0 0 100%
minerals/models.py 24 1 96% 30
minerals/templatetags/__init__.py 0 0 100%
minerals/templatetags/mineral_extras.py 12 2 83% 22-23
minerals/tests.py 20 0 100%
minerals/urls.py 5 0 100%
minerals/views.py 18 1 94% 42
TOTAL 96 7 93%