使用coverage,如何测试此行?

Pro*_*eus 15 python django django-testing

我有一个简单的测试:

class ModelTests(TestCase):

    def test_method(self):
        instance = Activity(title="Test")
        self.assertEqual(instance.get_approved_member_count(), 0)
Run Code Online (Sandbox Code Playgroud)

我的问题是覆盖仍然显示未get_approved_member_count测试的行:

在此输入图像描述

我如何满足上述覆盖范围?

要运行测试我正在使用Django Nose和Coverage:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

NOSE_ARGS = [
    '--with-coverage',
    '--cover-html',
    '--cover-package=apps.users,apps.activities',
]
Run Code Online (Sandbox Code Playgroud)

安慰:

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
Run Code Online (Sandbox Code Playgroud)

Ned*_*der 13

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

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

$ coverage run -m nose --verbosity=1
Run Code Online (Sandbox Code Playgroud)

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

$ coverage run manage.py test
Run Code Online (Sandbox Code Playgroud)

但是你想首先卸载nose coverage插件.

  • 我可以确认我已经看到了同样的行为,Ned的解决方案为我解决了这个问题.我以为我过去曾经看过这个文件(无论是在报道的文档还是鼻子的文档中),但现在似乎无法找到它. (3认同)
  • @OrbiterFleet我已经更新了答案. (2认同)