By default when Django runs against sqlite backend it creates a new in memory database for a test. That means for every class that derives from unittest.TestCase, I get a new database. Can this be changed so that it is cleared before every test method is run?
Example: I am testing a manager class that provides additional abstraction on top of Django persistent objects. The code looks more-less like that
class TestForManager(unittest.TestCase): def testAddingBlah(self): manager = Manager() self.assertEquals(manager.getBlahs(), 0) manager.addBlah(...) self.assertEquals(manager.getBlahs(), 1) def testAddingBlahInDifferentWay(self): manager = Manager() self.assertEquals(manager.getBlahs(), 0) manager.addBlahInDifferentWay(...) self.assertEquals(manager.getBlahs(), 1)
Now, the first assertion of second test fails, because the state of the database is preserved between test calls and there already is an instance of Blah in the database.
Use
django.test.TestCasenotunittest.TestCase. And it works in all major versions of Django!