gpt4 book ai didi

python - 使用 django_nose 和 django-dynamic-fixture 优化示例 django 模型的设置和拆卸

转载 作者:太空宇宙 更新时间:2023-11-03 14:30:13 26 4
gpt4 key购买 nike

鉴于我的模型如下:

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)

我正在使用 django-dynamic-fixture轻松生成用于测试的模型夹具。我也在使用 django_nose这可以帮助我很好地运行和管理测试。在 settings.py 文件中设置 test_runner 并将所有可安装文件放在适当的位置。

要生成上述模型,测试应该是

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

另外如果我写

class AuthorModelTest(TestCase):

def test_some_stuff()

我需要一些固定值。以下是我的疑问:

如何让我的夹具生成保持干爽。意思是不在每个函数中从 G 创建书籍和作者装置?

django_nose 有助于优化setUp 和tearDown 方法并提高速度,我如何在这里使用它们?只需放置 *django_nose.FastFixtureTestCase* 就可以解决 setUp tearDown 的问题?还是我需要使用 TransactionTestCase?如何优化上述夹具和测试?

最佳答案

最新版本Django Dynamic Fixture包括 Django Nose plugin以促进创建全局固定装置。检查是否对您有帮助。

关于python - 使用 django_nose 和 django-dynamic-fixture 优化示例 django 模型的设置和拆卸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12487337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com