Testing Tools in Django: Nose and Coverage

by Nadeem Aslam

Testing is vital in every field of programming. Without testing your code correctly, you will never know if the code works properly now as well as in the future when the codebase changes. Countless hours can be lost fixing problems caused by changes to the codebase. What’s worse, you may not even know that there are problems at all until your end users complain about it, which is obviously not how you want to find out about code breaks! Having tests in place will help ensure that if a specific function breaks you will know about it. Tests also make debugging breaks in code much easier, which saves time and money. Take testing seriously. You will have more confidence in your code, and your employer will have more confidence in you. It’s essentially an insurance policy. Testing helps you structure good code, find bugs, and write documentation.

Some Important Tools for Testing in Django: Nose and Coverage 

Django-nose:

Django-nose provides following features for testing your Django tests, like:

  • Testing just your apps by default and not all the standard ones that happen to be in INSTALLED_APPS.
  • Running the tests in one or more specific modules (or apps, or classes, or folders, or just running a specific test).
  • Obviating the need to import all your tests into tests/__init__.py. This not only saves busy-work but also eliminates the possibility of accidentally shadowing test classes.
  • Taking advantage of all the useful nose plugins.

These are some of the reasons why you should try Nose for testing:

  • Extends the functionalities of unittest.
  • It tests your apps by default, and not all the standard ones that are included in the INSTALLED_APPS of your settings file.
  • It detects all the functions and classes that start with Test or test that you have defined in your code, so no more worries about how you can make your tests visible to the Test Suite?
  • You can test specific apps, folders, files or tests.

Let’s start with Django Nose!

First, activate your testing environment and install nose and django-nose via pip:

$ pip install nose
$ pip install django-nose

Don’t forget to add them in your requirements file for the testing environment. Then, add django-nose to the installed apps in your testing settings.py file:

INSTALLED_APPS = (

‘django_nose’,

)

Finaly, you have to indicate Django to use the Nose test runner. In the test settings file add:

TEST_RUNNER = ‘django_nose.NoseTestSuiteRunner’

Save and make sure everything is installed properly by running,

$ python manage.py test

Django test with Coverage Library

Coverage library is an nice tool that shows you which parts of your code are covered by tests. It is nicely integrated with django-nose, so to install it, just run:

$ pip install coverage

Then, you can run coverage with

$ python manage.py test –with-coverage

If you want to run your tests with this default behavior, open your test settings file and add the following:

NOSE_ARGS = [
‘–with-coverage’,
‘–cover-package=app1, app2’,
‘–cover-inclusive’,
]

The second option, –cover-package indicates the packages to cover with coverage. In this case we cover two apps, myapp1 and myapp2 (these names are the ones you use in INSTALLED_APPS). And the third option, –cover-inclusive, indicates coverage to scan all the files in the working directories (this is useful to find files that are not being tested).

After this configuration, you run

$ python manage.py test

Only the packages app1 and app2 will be tested, and you’ll see the corresponding coverage report.

Now it’s time for you to write some tests and also generate coverage report to find out how much code you have covered by your tests.

There is still a lot to discuss about Testing tools but that is beyond the scope of this blog. I hope this was useful for achieving the results from these testing tools in Django.

Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts