Can Django flush its database(s) between every unit test

Posted by mikem on Stack Overflow See other posts from Stack Overflow or by mikem
Published on 2010-05-05T09:53:18Z Indexed on 2010/05/05 9:58 UTC
Read the original article Hit count: 165

Filed under:
|
|

Django (1.2 beta) will reset the database(s) between every test that runs, meaning each test runs on an empty DB. However, the database(s) are not flushed. One of the effects of flushing the database is the auto_increment counters are reset.

Consider a test which pulls data out of the database by primary key:

class ChangeLogTest(django.test.TestCase):
    def test_one(self):
        do_something_which_creates_two_log_entries()
        log = LogEntry.objects.get(id=1)
        assert_log_entry_correct(log)
        log = LogEntry.objects.get(id=2)
        assert_log_entry_correct(log)

This will pass because only two log entries were ever created. However, if another test is added to ChangeLogTest and it happens to run before test_one, the primary keys of the log entries are no longer 1 and 2, they might be 2 and 3. Now test_one fails.

This is actually a two part question:

  1. Is it possible to force ./manage.py test to flush the database between each test case?
  2. Since Django doesn't flush the DB between each test by default, maybe there is a good reason. Does anyone know?

© Stack Overflow or respective owner

Related posts about django

Related posts about database