Python Django中内容类型模型的单元测试

Python Django中内容类型模型的单元测试,python,django,unit-testing,tdd,Python,Django,Unit Testing,Tdd,我是Django的新手,已经完成了一些测试驱动的开发。我试图坚持TDD的原则,但在某些情况下,我不知道如何像下面的模型那样进行。我有一个与我在这里展示的非常相似的模型。从本质上讲,这个想法是构建一本书。有时该书由章节组成,有时该书包含章节和其他书籍。所以,我的问题是,在我得到一个类似于下面的模型并具有相同功能之前,我一直在尝试测试。我已经在我的pythonshell中测试了这个模型,它输出了我所期望的结果,但是我想要一些更健壮的东西。 我还希望能够在我的项目的核心中使用这个模型,并且需要能够在我

我是Django的新手,已经完成了一些测试驱动的开发。我试图坚持TDD的原则,但在某些情况下,我不知道如何像下面的模型那样进行。我有一个与我在这里展示的非常相似的模型。从本质上讲,这个想法是构建一本书。有时该书由章节组成,有时该书包含章节和其他书籍。所以,我的问题是,在我得到一个类似于下面的模型并具有相同功能之前,我一直在尝试测试。我已经在我的pythonshell中测试了这个模型,它输出了我所期望的结果,但是我想要一些更健壮的东西。 我还希望能够在我的项目的核心中使用这个模型,并且需要能够在我继续在上面构建时对它进行测试。对于测试这样的模型,有什么好的单元测试示例?或者关于在哪里寻找使用ContentType和其他抽象模型的测试的任何建议?谢谢

from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType

class Element(models.Model):
    title = models.TextField()

    class Meta:
        abstract = True

class Chapter(Element):
    body = models.TextField()

    def __unicode__(self):
        return self.body

class Book(Element):
    description = models.TextField()

    def __unicode__(self):
        return self.description

class BookElement(models.Model):
    protocol_id = models.PositiveIntegerField()
    element_content_type = models.ForeignKey(ContentType)
    element_id = models.PositiveIntegerField()
    element = generic.GenericForeignKey('element_content_type', 'element_id')
    # Sort order, heavy things sink.
    element_weight = models.PositiveIntegerField()

def __unicode__(self):
    return u'%s %s' % (self.protocol_id, self.element , self.element_weight)
更新 下面是我为将元素输入数据库并检索它们而设计的一个测试。它是有效的,但似乎长时间的测试不止一件事。如果有更好的办法,我愿意接受建议

class BookAndChapterModelTest(TestCase):

    def test_saving_and_retrieving_book_elements(self):
        # input data objects and save
        book = Book()
        book.title = "First book"
        book.description = "Testing, round one"
        book.save()

        first_chapter = Chapter()
        first_chapter.title = 'step 1'
        first_chapter.body ='This is step 1'
        first_chapter.save()

        second_chapter = Chapter()
        second_chapter.title = 'step 2'
        second_chapter.body = 'This is step 2'
        second_chapter.save()

        # link content types to chapter or book model
        chapter_ct = ContentType.objects.get_for_model(first_chapter)
        book_ct = ContentType.objects.get_for_model(book)

        # Assemble BookElement order by weight of chapter or book
        BookElement.objects.create(
            book_id=book.pk,
            element_content_type=chapter_ct, 
            element_id=first_chapter.pk, 
            element_weight='1')
        BookElement.objects.create(
            book_id=book.pk, 
            element_content_type=chapter_ct, 
            element_id=second_chapter.pk, 
            element_weight='2')
        BookElement.objects.create(
            book_id=book.pk, 
            element_content_type=book_ct, 
            element_id=book.pk, 
            element_weight='3')

        # Test number of elements
        saved_book_element = BookElement.objects.all()
        self.assertEqual(saved_book_element.count(), 3)

        # Test elements are in the proper position based on weighting
        first_book_element = saved_book_element[0]
        self.assertEqual(str(first_book_element), 'This is step 1')
        third_book_element = saved_book_element[2]
        self.assertEqual(str(third_book_element), "Testing, round one")     

工厂男孩来营救!它使测试变得有趣


我从来没有用过工厂男孩。你能详细说明一下如何合并它吗?