Coverage.py prompts me to check for ImportError - how to do this?

Just started using coverage.py and I'm on a booze test!

At the top of one of my python file, I have the following (which is used to keep track of different Django versions):

try: # newer versions
    from django.contrib.contenttypes.fields import GenericForeignKey 
except ImportError: # older versions
    from django.contrib.contenttypes.generic import GenericForeignKey 

      

When I run coverage.py

it highlights the last two lines ( except ImportError

) and places it as needed for testing.

How can I test this in my unittests? Do I need? It seems I shouldn't, because I am not testing all of my other imports. Is it coverage.py

just horny?

Thank you ~

+3


source to share


2 answers


The easiest way to test this alternate import is to remember why you have this line of code in the first place. The comment says "older versions". I assume these are earlier versions of Django. This way you are maintaining older versions, but it looks like you are not testing them.



You should run your test suite in all versions supported by Django, or at least the oldest and newest versions. You can then combine coverage results from each of these runs. If you do this, all of these lines will be covered.

+1


source


There are simple lines of code that sometimes really don't make sense to devote time to writing test cases for them. Perhaps this piece of code falls into this category.

Therefore, you can instruct coverage to ignore it by adding a comment # pragma nocover

next to any live you wish to exclude from coverage. Note that by adding this to the line that enters the block (like a statement if

), it will highlight the entire block.



Ref: Exclude code from coverage .

+1


source







All Articles