Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django test coverage vs code coverage

I've successfully installed and configured django-nose with coverage

Problem is that if I just run coverage for ./manage.py shell and exit out of that shell - it shows me 37% code coverage. I fully understand that executed code doesn't mean tested code. My only question is -- what now?

What I'm envisioning is being able to import all the python modules and "settle down" before executing any tests, and directly communicating with coverage saying "Ok, start counting reached code here."

Ideally this would be done by nose essentially resetting the "touched" lines of code right before executing each test suite.

I don't know where to start looking/developing. I've searched online and haven't found anything fruitful. Any help/guidelines would be greatly appreciated.

P.S.

I tried executing something like this:

DJANGO_SETTINGS_MODULE=app.settings_dev coverage run app/tests/gme_test.py 

And it worked (showed 1% coverage) but I can't figure out how to do this for the entire app

Edit: Here's my coverage config:

[run] source = . branch = False timid = True [report] show_missing = False include = *.py omit =     tests.py     *_test.py     *_tests.py     */site-packages/*     */migrations/* [html] title = Code Coverage directory = local_coverage_report 
like image 569
Mikhail Avatar asked Mar 29 '14 03:03

Mikhail


People also ask

Is code coverage Same as test coverage?

Key Differences Between Code Coverage vs Test CoverageCode Coverage describes how much application code is being executed when an application is being run. On the other hand, test coverage describes the test cases which are written and mentioned in any document.

How do I run test coverage in Django?

With Django's Test Runner. If you're using manage.py test , you need to change the way you run it. You need to wrap it with three coverage commands like so: $ coverage erase # Remove any coverage data from previous runs $ coverage run manage.py test # Run the full test suite Creating test database for alias 'default'.. ...

What is code coverage analysis What are main differences between code coverage and test coverage?

Code coverage is measured by the percentage of code that is covered during testing, whereas test coverage is measured by the features that are covered via tests.

Is code coverage a good metric?

Code coverage is a metric that can help you understand how much of your source is tested. It's a very useful metric that can help you assess the quality of your test suite, and we will see here how you can get started with your projects.


1 Answers

since you use django-nose you have two options on how to run coverage. The first was already pointed out by DaveB:

coverage run ./manage.py test myapp 

The above actually runs coverage which then monitors all code executed by the test command.

But then, there is also a nose coverage plugin included by default in the django-nose package (http://nose.readthedocs.org/en/latest/plugins/cover.html). You can use it like this:

./manage.py test myapp --with-coverage 

(There are also some additional options like which modules should be covered, whether to include an html report or not etc . These are all documented in the above link - you can also type ./manage.py test --help for some quick info).

Running the nose coverage plugin will result in coverage running after the django bootstrapping code is executed and therefore the corresponding code will not be reported as covered.

Most of the code you see reported as covered when running coverage the original way, are import statements, class definitions, class members etc. As python evaluates them during import time, coverage will naturally mark them as covered. However, running the nose plugin will not report bootstrapping code as covered since the test runner starts after the django environment is loaded. Of course, a side effect of this is you can never achieve 100% coverage (...or close :)) as your global scope statements will never get covered.

After switching back and forth and playing around with coverage options, I now have ended up using coverage like this:

coverage run --source=myapp,anotherapp ---omit=*/migrations/* ./manage.py test 

so that a) coverage will report import statements, class member definitions etc as covered (which is actually the truth - this code was successfully imported and interpreted) and b) it will only cover my code and not django code, or any other third-party app I use; the coverage percentage will reflect how well my project is covered. Hope this helps!

like image 52
ppetrid Avatar answered Sep 20 '22 05:09

ppetrid