14

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
Prometheus
  • 32,405
  • 54
  • 166
  • 302
  • The way you go is right. What is the test output if you directly call this test? – tjati Jun 04 '15 at 10:32
  • @omeinusch the test just returns a count during tests this is 0 in production this is more. I assumed above covered it but coverage still says ``get_approved_member_count()`` is not covered. – Prometheus Jun 04 '15 at 12:20
  • 2
    Please post the full console output including your call and the output given by your test runner. – tjati Jun 04 '15 at 12:26
  • The likely cause is you import the module under test outside of coverage's coverage. And you did not mention your test runner nor any other details of your environment, and the example is not reproducible in any way 'cause incomplete. – jhermann Jun 04 '15 at 12:45
  • @jhermann updated my OP, hope it helps :) – Prometheus Jun 04 '15 at 20:07

1 Answers1

12

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.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 3
    I can confirm I have seen this same behavior and Ned's solution solved the problem for me. I thought I had seen this documented in the past (either in coverage's docs or nose's docs), but can't seem to find it now. – Waylan Jun 04 '15 at 20:32
  • 1
    Thank you for the reply. That makes a lot of sense. I'm using Django and the Django Nose test runner, do I have any options with that? – Prometheus Jun 04 '15 at 21:23