Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running all tests post django 1.6

In django 1.5 and earlier, running python manage.py test would, by default, run all tests in a project (including all those in django.contrib). Subsequent to version 1.6, the default behaviour is to run all the tests in the current directory.

What is the best way (v 1.6) to run all tests, either with or without the django.contrib tests?

like image 387
Luke Avatar asked Jan 03 '14 13:01

Luke


1 Answers

Django 1.6 changed the default test runner to:

TEST_RUNNER = 'django.test.runner.DiscoverRunner'

You can get the old behaviour back by adding to your settings.py:

TEST_RUNNER = 'django.test.simple.DjangoTestSuiteRunner'

As explained in the release notes:

The previous runner (django.test.simple.DjangoTestSuiteRunner) found tests only in the models.py and tests.py modules of a Python package in INSTALLED_APPS.

The new runner (django.test.runner.DiscoverRunner) uses the test discovery features built into unittest2 (the version of unittest in the Python 2.7+ standard library, and bundled with Django). With test discovery, tests can be located in any module whose name matches the pattern test*.py.

The new runner expects a list of dotted path of modules where tests shall be discovered, so you can also run the tests from django contrib this way:

python manage.py test myproject django.contrib path.to.someotherapp

This will not run all tests from apps in INSTALLED_APPS automatically though. For a more complex solution, you could write your own runner, taking from both the old and new runner.

Also note that it usually shouldn't be necessary to run tests from django.contrib, as these are not testing your application, but rather the Django distribution. Django ships with even more tests, which are not run by either runner.

like image 187
Nicolas Cortot Avatar answered Oct 11 '22 18:10

Nicolas Cortot