paulocheque / django-dynamic-fixture

A complete library to create dynamic model instances for testing purposes.
http://django-dynamic-fixture.readthedocs.io/
Other
391 stars 67 forks source link

Improve Documentation about setUp and tearDown for tests #11

Closed infyloop closed 11 years ago

infyloop commented 12 years ago

Currently the documentation does not talk of how setUp and tearDown methods for classes work. If possible, include optimization of setUp and tearDown as well.

A simple example would be as follows:

class BookModelTest(TestCase):

     def setUp(self):
          self.author = G(Author)
          self.book = G(Book, author=[author])

     def test_book_creation(self):
          book_in_db = Book.objects.all()
          self.assertEquals(book_in_db.count(), 1)
          first_book = book_in_db[0]
          self.assertEquals(first_book.name, self.book.name)
          self.assertEquals(list(first_book.author), [self.author])

class Author(models.Model):

     name = models.CharField(max_length=100)

class Book(models.Model):

     name = models.CharField(max_length=100)
     author = models.ManyToManyField(Author)
tobych commented 12 years ago

That should be:

def setUp(self):
    self.author = G(Author)
    self.book = G(Book, author=[author])

:-)

infyloop commented 12 years ago

thanks, corrected :-)

paulocheque commented 12 years ago

DDF help to decrease the number of lines in setUp methods. A good unit test is a cohesive test. Avoid spread information of the test in many methods/classes.

PS: Django test suite let the tearDown method pratically useless. Just in very special scenarios it will be useful. DDF FileSystemTestCase has the same reason to exist.