Given my model is following:
class Author(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ManyToManyField(Author)
I am using django-dynamic-fixture to generate model fixtures easily for the test. I am also using django_nose which helps me run and manage tests pretty nicely.
Having setup the test_runner in settings.py file and putting all installables at place.
To generate a model as above the test should be
from django_dynamic_fixture import G
class BookModelTest(TestCase):
def test_book_creation(self):
author1 = G(Author)
author2 = G(Author)
book = G(Book, author=[author1])
book_obj = Book.objects.all()
self.assertEquals(book_obj.count(), 1)
self.assertEquals(list(book_obj[0].author), [author1])
self.assertEquals(book_obj[0].title, book.title)
self.assertNotEquals(list(book_obj[0].author), [author1])
def another_test(self):
"Here as well i need the same, author1, author2 and book
Also if i write
class AuthorModelTest(TestCase):
def test_some_stuff()
I would be needing some fixture value.
So following are the queries i had:
How do i keep my fixture generation DRY. Meaning not creating book and author fixtures from G in each of the functions?
django_nose helps to optimize the setUp and tearDown methods and improves speed, how can I use them here? Just putting *django_nose.FastFixtureTestCase* will take care of setUp tearDown pains? Or do I need to use TransactionTestCase? How do I optimize the above fixture and test?
TransactionTestCasehelps 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.TransactionTestCasehowever leaves the database cluttered, django-nose helps you optimize it. django-nose however has another test runnerFastFixtureTestCasehelps 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
FastFixtureTestCaseand it will help you optimize the IO time.