使用coverage,如何测试此行?

时间:2015-06-01 20:35:56

标签: python django django-testing

我有一个简单的测试:

class ModelTests(TestCase):

    def test_method(self):
        instance = Activity(title="Test")
        self.assertEqual(instance.get_approved_member_count(), 0)

我的问题是,覆盖范围仍显示get_approved_member_count行未经测试:

enter image description here

如何满足上述要求?

要运行测试我正在使用带覆盖范围的Django Nose:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

NOSE_ARGS = [
    '--with-coverage',
    '--cover-html',
    '--cover-package=apps.users,apps.activities',
]

控制台:

python manage.py test
/Users/user/Documents/workspace/api/env/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: django.contrib.contenttypes.generic is deprecated and will be removed in Django 1.9. Its contents have been moved to the fields, forms, and admin submodules of django.contrib.contenttypes.
  return f(*args, **kwds)

/Users/user/Documents/workspace/api/env/lib/python3.4/importlib/_bootstrap.py:321: RemovedInDjango19Warning: The utilities in django.db.models.loading are deprecated in favor of the new application loading system.
  return f(*args, **kwds)

nosetests --with-coverage --cover-html --cover-package=apps.users,apps.activities --verbosity=1




Name                                                      Stmts   Miss  Cover   Missing
---------------------------------------------------------------------------------------
apps.activities                                          0      0   100%
apps.activities.admin                                    8      8     0%   1-14
activities.migrations                               0      0   100%
activities.migrations.0001_initial                  9      0   100%
apps.activities.urls                                     8      0   100%


etc etc etc
---------------------------------------------------------------------------------------
TOTAL                                                       670    232    65%
----------------------------------------------------------------------
Ran 79 tests in 17.101s

1 个答案:

答案 0 :(得分:13)

覆盖率报告显示正在调用该方法(第80行为绿色)。但它也表明它从未定义过(第75行是红色)。

这是开始报道太晚的经典问题。解决此问题的最简单方法是使用coverage来运行测试运行器,而不是使用测试运行器来运行覆盖:

$ coverage run -m nose --verbosity=1

更新:与原始命令一起使用:

$ coverage run manage.py test

但您想首先卸载鼻子覆盖率插件。