[Django]-Difference between django.test.TestCase vs unittest vs django.utils.unittest.TestCase

20👍

Django’s TestCase enhances unittest.TestCase with some extra features:

  • Automatic loading of fixtures.
  • Wraps each test in a transaction.
  • Creates a TestClient instance.
  • Django-specific assertions for testing for things like redirection and form errors.

Generally speaking, you should most likely be using one of Django’s TestCase subclasses. Usually this will be django.test.TestCase, which, for efficiency, wraps the test in a DB transaction and uses rollback to ‘undo’ the test in the DB. If you need to manually manage transactions within your test, you would need to use django.test.TransactionTestCase, since you can’t start / rollback a transaction within a transaction.

There are some minor caveats to using django.test.TestCase, see the note here for more information.

ALSO:

If you’re just looking for a way to run your tests faster, have a look at running your tests in memory, and (if you’re using South), set SOUTH_TESTS_MIGRATE = False to tell South to use a (much faster) syncdb when creating the test DB, rather than running migrations.

Leave a comment