[Django]-Optimizing setup and teardown for sample django model using django_nose and django-dynamic-fixture


TransactionTestCase helps you save an saving an entire DB flush per test, it expects you to start with an unmarred db, you are free to generate it using any fixture generator. TransactionTestCase however leaves the database cluttered, django-nose helps you optimize it. django-nose however has another test runner FastFixtureTestCase helps you optimize the setUp and tearDown.

As already said, you can use any fixture generation, if you want the goodness of django-nose use the FastFixtureTestCase and it will help you optimize the IO time.


Latest version of Django Dynamic Fixture includes a Django Nose plugin to facilitate creating global fixtures. Check if that helps you.


FastFixtureTestCase is used for fast fixtures handling and it will not help in this situation at all.

Using django_dynamic_fixture or factory_boy (which I use personally) and always creating only as little objects as it is needed for tests, and even if it is possible – completely omit database (e.g. when testing only model methods not related to other models) is the fastest approach.

Also if database is what is slowing you down try to use regular database for testing. I know that sqlite in tests should be fast, because it is run in-memory and all that stuff, but it is just still much slower than normal postgres/mysql database. You can also enable REUSE_DB option in django-nose which will make tests startup and shutdown much faster.



I’m not familiar with django-nose’s optimization features, but in answer to your first question, if you repeatedly need the same set of objects for a bunch of tests, I’d just make a utility method or standalone function you can call that creates those objects and returns them (or one of them from which you can get to the others).


you can create a data API handling data creation.

class TestData(objects):
    def create_a_book_with_author(self):
         author1 = G(Author)
         author2 = G(Author)
         book = G(Book, author=[author1])

class BookModelTest(TestCase):

     def setUp(self):
         self.book_obj = Book.objects.all()

     def test_book_creation(self):
         self.assertEquals(self.book_obj.count(), 1)
         self.assertEquals(list(self.book_obj[0].author), [author1])
         self.assertEquals(self.book_obj[0].title, book.title)
         self.assertNotEquals(list(self.book_obj[0].author), [author1])

Leave a comment