difference between django.test.TestCase vs unittest vs django.utils.unittest.TestCase

前端 未结 1 658
太阳男子
太阳男子 2021-02-07 04:44

I am still using Django 1.2.1, and I think with the newer Django we don\'t import unittest and then do unittest.TestCase.

Illustration<

相关标签:
1条回答
  • 2021-02-07 05:01

    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.

    0 讨论(0)
提交回复
热议问题