Author: adiyatmubarak

Running Specific Test in django-nose

Since I used django-nose for my project. Django-nose runner cannot behave like default django test runner.

Following command will not work in django-nose test runner.

# Run all the tests found within the 'animals' package
$ ./manage.py test appname

# Run just one test case
$ ./manage.py test appname.tests.TestCaseName

# Run just one test method
$ ./manage.py test appname.tests.TestCaseName.test_function_name

Because django-nose structured all the test in one package called tests following below.

tests:
   |- __init__.py
   |- test_views.py
   |- test_forms.py
   |- test_endpoints.py
   |- test_utils.py

Here is the alternative to run specific task in django-nose.

# Run all the tests found within the 'animals' package
$ ./manage.py test appname

# Run just one test case
$ ./manage.py test appname.tests.test_script:TestCaseName

# Run just one test method
$ ./manage.py test appname.tests.test_script:TestCaseName.test_function_name

Reference: https://stackoverflow.com/a/18834222/1936697

Advertisements

Exclude folders and line of codes from Test Coverage

Here is my coverage setup in my project. I excluded some folders and line of codes that not important in my testing.

I put these config in .coveragerc

[run]
omit =
    */urls/*
    */migrations/*
    */tests/*
    */__init__.py
    */tests.py
    */apps.py
    */rss.py
    */urls.py

[report]
exclude_lines =
    def __unicode__
    if setttings.TESTING
    def __repr__
    def __str__

Disable django-haystack During Testing

I use django-haystack and elasticsearch for the search engine in our apps. But I don’t want my model that registered to haystack do indexing during the testing. I avoid haystack indexing by adding these following lines of code below in my settings.py.

import sys

TESTING = 'test' in sys.argv
if TESTING:
    HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.BaseSignalProcessor'
    HAYSTACK_CONNECTIONS['default'] = {
        'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
    }

Reference: https://stackoverflow.com/q/33108569/1936697