6

我在 Django 网站上运行简单的鼻子测试(代码可在此处获得)。根据发出 CLI 命令的顺序,我会得到截然不同的结果!

查看许多帖子,这似乎与关于加载过程的报道开始时间有关。我不明白的是:

  • 为什么两种方法的遗漏陈述如此不同?
  • 如果我一个接一个地运行一种方法,为什么覆盖率会大幅上升?

我专注于覆盖minerals/views.py

Djangosettings.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缺少语句: 12-13, 18-49minerals/views.py

$ 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%

运行测试方法2:

如果我使用 运行测试./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%

运行测试方法3:

如果我在测试运行之间删除cover/目录和.coverage文件,上面的方法 1 和 2 每次都会产生与预期相同的结果。但是,如果我运行方法 1,然后运行方法 2(将cover/目录保留.coverage在方法 1 中的位置),我会得到截然不同的结果。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%
4

0 回答 0