Code


Version 9 (modified by yourcelf, 4 years ago) (diff)

--

Testing the django framework

(as opposed to testing your app).

Up to date official docs can be found here: http://docs.djangoproject.com/en/dev/internals/contributing/#running-the-unit-tests

The following is out of date as of Django 1.2 and multidb support.

If your app is having trouble, it is a good idea to run django's tests to make sure django is functioning properly. it also helps the developers catch bugs that could be system dependent, like OS, python version, db, etc.

To run the tests, you need to supply a very small settings file. you will probably want to write a small script to run tests and log the results. below is what I use.

# settings_memory.py
# this should be faster than the others because it uses an in-memory SQLite
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = ':memory:'
ROOT_URLCONF = None
# settings_sqlite.py
DATABASE_ENGINE = 'sqlite3'   
DATABASE_NAME = 'demodb'
ROOT_URLCONF=None
# settings_postgresql1.py 
DATABASE_ENGINE = 'postgresql'
DATABASE_NAME = 'x'
DATABASE_USER = 'y'
DATABASE_PASSWORD = 'z'
ROOT_URLCONF=None
TEST_DATABASE_CHARSET = 'utf8' 
# settings_mysql.py
DATABASE_ENGINE = 'mysql'
DATABASE_USER = 'foo'   
DATABASE_PASSWORD = 'abc'
ROOT_URLCONF=None
TEST_DATABASE_CHARSET = 'utf8' 

Running tests

# runtests.sh
# runs the django tests, logs the output

set -x

echo svnversion $(svnversion) > runtests.log

cat settings_sqlite.py>> runtests.log
./runtests.py --settings settings_sqlite 2>&1|tee --append runtests.log

cat settings_memory.py>> runtests.log
./runtests.py --settings settings_memory 2>&1|tee --append runtests.log

cat settings_mysql.py>> runtests.log
./runtests.py --settings settings_mysql 2>&1|tee --append runtests.log

cat settings_postgresql1.py>> runtests.log
./runtests.py --settings settings_postgresql1 2>&1|tee --append runtests.log
rem runtests.bat
runtests.py --settings settings_sqlite 2>&1

Note: most of the runtests.py output is to stderr. the 2>&1 redirects that to stdout so that it can be redirected into a file, which is handy for posting when tests fail.

Test Locations

If you write tests place them as follows:

  • if the test tests a core feature of models, it should live in modeltests;
  • if it tests a contrib app, it should live as a test module in the contrib app itself;
  • otherwise it should go in regressiontests