【问题标题】:Unit tests in Django for Content type model内容类型模型的 Django 单元测试
【发布时间】:2014-10-02 05:45:10
【问题描述】:

我对 Django 还很陌生,并且已经完成了一些测试驱动开发。我尝试遵守 TDD 的原则,但在某些情况下我不知道如何进行(如下面的模型)。我有一个与我在这里展示的非常相似的模型。本质上,这个想法是构建一本书。有时那本书由章节组成,有时这本书包含章节和其他书籍。所以,我的问题实际上是关于尝试测试,直到我得到一个类似于下面具有相同功能的模型。我已经在我的 python shell 中测试了这个模型,它输出了我期望的结果,但我想要更健壮的东西。 我还希望能够在我的项目的核心中使用这个模型,并且需要能够在我继续在上面构建时对其进行测试。测试这样的模型的一些很好的示例单元测试是什么?或者关于在哪里寻找与 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")     

【问题讨论】:

    标签: python django unit-testing tdd


    【解决方案1】:

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

    http://factoryboy.readthedocs.org/en/latest/examples.html

    【讨论】:

    • 我从来没有用过factory-boy。您能否详细说明如何合并它?
    猜你喜欢
    • 2017-12-19
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-05
    • 2019-07-29
    • 2018-10-19
    • 2019-06-21
    相关资源
    最近更新 更多