5

For a given python file that has the following lines at the top:

import traceback
import datetime
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.conf import settings

How do I write unit tests that will result in nose not showing 'missing' for those lines?

oz123
  • 27,559
  • 27
  • 125
  • 187
Duncan
  • 2,550
  • 2
  • 17
  • 18
  • Can you be more specific? What is showing as missing? Do you mean coverage report? – Oleksiy Feb 26 '15 at 19:59
  • Assuming the lines of code in my post are the first six lines of a file, nose coverage would show those lines as missing code coverage. I would like to know how to write tests to cover those lines. – Duncan Feb 26 '15 at 20:08
  • 2
    It sounds like coverage is started after the file is imported. Show how you run your tests. – Ned Batchelder Feb 26 '15 at 23:40

1 Answers1

0

I think if it shows those lines as missing in code coverage, that means that this module was never used or imported. Those lines will show as covered in the report as long as you are successful importing those modules, and no extra tests are needed to validate the ability to import those well tested django modules. As long as you have a single test that tests something in that module, you should be fine. For example:

import traceback
import datetime
# from django.contrib.contenttypes import generic
# from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.conf import settings


def foo(a):
    return a + 5


def test_foo():
    assert foo(5) == 10

will result in 100% coverage when running under nose with --with-coverage option for this particular file. I commented out django.contrib packages because I'm not using django-nose and do not have proper settings for this example, but it should not matter.

Oleksiy
  • 6,337
  • 5
  • 41
  • 58
  • I know that this is old, but now I have the same problem, and actually the module is imported already because I have green lines inside methods of classes defined in this module. For me it's somehow impossible to call a method from a class without importing it first, but nose think that it is exactly that. – VStoykov Feb 24 '17 at 16:36