Django unit test not breaking database?

I have some unit tests that I wrote to test my Django application. In one test suite, in particular, there is a lot of code in its function setUp(). The purpose of this code is to create test data for the database. (Yes, I know about lamps and decided not to use them in this case). When I run the unit test package, the first test that runs fails, but then the rest of the tests in the package fail. The message for all crashes is the same: it mentions that the error location is "self.database_object.save ()" and the reason is that "IntegrityError: column name is not unique." So, I think Django is not breaking the database properly after each test.

He worked earlier today, but I think I ruined some refactoring. Any ideas on why Django is not properly monitoring the database after each test?

+5
source share
1 answer

Are you using TestCase or TransactionTestCase for your base class? Sometimes this behavior is related to optimization Django does for TestCase in favor of TransactionTestCase. Here is the difference:

https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.TransactionTestCase

class TransactionTestCase

Django TestCase , , . , , , commit rollback Django TestCase. , Django TransactionTestCase.

TransactionTestCase TestCase , reset , . TransactionTestCase . TransactionTestCase .

TestCase, , . , . - , , . , TestCase , Django TestCase, (, ), .

+8

All Articles