Django setUpTestData()与setUp() [英] Django setUpTestData() vs. setUp()

查看:178
本文介绍了Django setUpTestData()与setUp()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 1.8随附了重构的TestCase ,它允许数据初始化在班级使用 setUpTestData()的交易和保存点 a>方法。这与unittest的 setUp()相对,后者在每种测试方法之前运行

Django 1.8 shipped with a refactored TestCase which allows for data initialization at the class level using transactions and savepoints via the setUpTestData() method. This is in contrast to unittest's setUp() which runs before every single test method.

问题:既然<$ c,在Django中 setUp()的用例是什么? $ c> setUpTestData()存在吗?

Question: What is the use case for setUp() in Django now that setUpTestData() exists?

我只是在寻找客观,高级的答案,否则对于堆栈溢出来说,这个问题太笼统了。

I'm looking for objective, high-level answers only, as otherwise this question would be too broad for Stack Overflow.

推荐答案

存在无法运行的设置代码并不少见作为类方法。一个著名的例子是Django 测试客户端 :您可能不希望在测试之间重用相同的客户端实例,否则它们会共享许多相同的数据,实际上,自动包含在Django SimpleTestCase 子类中的客户端实例是<每个测试方法而不是整个类创建的href = https://github.com/django/django/blob/2.2/django/test/testcases.py#L201 rel = noreferrer>创建 。假设您使用 setUp 方法从Django 1.8之前的版本进行了测试,如下所示:

It's not uncommon for there to be set-up code that can't run as a class method. One notable example is the Django test client: you might not want to reuse the same client instance across tests that otherwise share much of the same data, and indeed, the client instances automatically included in subclasses of Django's SimpleTestCase are created per test method rather than for the entire class. Suppose you had a test from the pre-Django 1.8 world with a setUp method like this:


    def setUp(self):
        self.the_user = f.UserFactory.create()
        self.the_post = f.PostFactory.create(author=self.the_user)
        self.client.login(
            username=self.the_user.username, password=TEST_PASSWORD
        )
        # ... &c.

您可能会想通过将 setUp 更改为<$ c来现代化测试用例$ c> setUpTestData ,将 @classmethod 装饰器放在顶部,并更改所有 self s到 cls 。但这将失败,并出现 AttributeError:类型对象 MyTestCase没有属性 client !相反,您应该将 setUpTestData 用于共享数据,并将 setUp 用于每个测试方法客户端:

You might tempted to modernize the test case by changing setUp to setUpTestData, slapping a @classmethod decorator on top, and changing all the selfs to cls. But that will fail with a AttributeError: type object 'MyTestCase' has no attribute 'client'! Instead, you should use setUpTestData for the shared data and setUp for the per-test-method client:


    @classmethod
    def setUpTestData(cls):
        cls.the_user = f.UserFactory.create()
        cls.the_post = f.PostFactory.create(author=cls.the_user)
        # ... &c.

    def setUp(self):
        self.client.login(
            username=self.the_user.username, password=TEST_PASSWORD
        )

注意:如果您想知道示例代码中的变量 f 在做什么,它来自 factoryboy -一个有用的夹具库,用于为测试创建对象。

Note: if you are wondering what that variable f is doing in the example code, it comes from factoryboy - a useful fixtures library for creating objects for your tests.

这篇关于Django setUpTestData()与setUp()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆