Using coverage, how do I test this line?

2019-03-18 05:25发布

I have a simple test:

class ModelTests(TestCase):

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

My problem is that coverage still shows get_approved_member_count line as NOT tested:

enter image description here

How do I satisfy the above for coverage?

To run the tests I'm using Django Nose with Coverage:

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

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

Console:

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条回答
做自己的国王
2楼-- · 2019-03-18 06:19

The coverage report shows that the method is being called (line 80 is green). But it also shows that it was never defined (line 75 is red).

This is a classic problem of starting coverage too late. The simplest way to fix this is to use coverage to run your test runner, instead of using the test runner to run coverage:

$ coverage run -m nose --verbosity=1

UPDATED: to use with your original command:

$ coverage run manage.py test

but you'd want to uninstall the nose coverage plugin first.

查看更多
登录 后发表回答