Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using coverage, how do I test this line?

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
like image 899
Prometheus Avatar asked Jun 01 '15 20:06

Prometheus


People also ask

How do you run tests with coverage?

Run a test with code coverageOpen the desired file in the Project tool window and choose Run <name> with Coverage from the context menu. You can also select a directory with test files and choose the corresponding command from the context menu to run several tests with coverage.

How do you test code coverage?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

What is line test coverage?

Line coverage measures how many statements you took (a statement is usually a line of code, not including comments, conditionals, etc). Branch coverages checks if you took the true and false branch for each conditional (if, while, for). You'll have twice as many branches as conditionals.

How do you check vs test coverage?

On the Test menu, select Analyze Code Coverage for All Tests. You can also run code coverage from the Test Explorer tool window. Show Code Coverage Coloring in the Code Coverage Results window. By default, code that is covered by tests is highlighted in light blue.


1 Answers

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.

like image 114
Ned Batchelder Avatar answered Sep 30 '22 18:09

Ned Batchelder