I have some unit tests I’ve written to test my Django application. One test suite in particular has a lot of code in its setUp() function. The purpose of said code is to create test data for the database. (Yes I know about fixtures and have chosen not to use them in this case). When I run the unit test suite the first test that is run passes, but then the rest of the tests in the suite fail. The message for all the failures is the same: it mentions that the error’s location is “self.database_object.save()” and that the cause is “IntegrityError: column name is not unique”. So, my best guess is that Django is not tearing down the database properly after each test.
Earlier today it was working, but I guess some refactoring I did messed it up. Any ideas on why Django is not properly tearing down the database after each test?
Do you use TestCase or TransactionTestCase for your base class? Sometimes this behavior is related to the optimization Django makes for TestCase in favor of TransactionTestCase. Here is the difference:
https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.TransactionTestCase