使LiveServerTestCase不在每次测试之前调用setUp()

时间:2022-01-02 20:16:37

I have one problem with testing django app by using LiveServerTestCase. LiveServerTestCase execute setUp() function before executing each test. But I'm using factory-boy's factories to create objects for testing (users, items, etc...). And the same objects are created before executing each test. How can I create this objects one time and make all tests to see this objects in database?

我有一个问题,使用LiveServerTestCase测试django应用程序。 LiveServerTestCase在执行每个测试之前执行setUp()函数。但我正在使用工厂男孩的工厂来创建测试对象(用户,物品等......)。并且在执行每个测试之前创建相同的对象。如何创建此对象一次并进行所有测试以在数据库中查看此对象?

1 个解决方案

#1


1  

setUp() gets called before every test.

每次测试之前都会调用setUp()。

If you want to create the objects once for the entrire test case, you can use setUpClass() instead.

如果要为委托测试用例创建一次对象,可以使用setUpClass()代替。

E.g.

class SomeTest(LiveServerTestCase):
        @classmethod
        def setUpClass(cls):
            # create objects here
            LiveServerTestCase.setUpClass()

Don't forget to call LiveServerTestCase.setUpClass() or the live server won't function properly.

不要忘记调用LiveServerTestCase.setUpClass(),否则实时服务器将无法正常运行。

#1


1  

setUp() gets called before every test.

每次测试之前都会调用setUp()。

If you want to create the objects once for the entrire test case, you can use setUpClass() instead.

如果要为委托测试用例创建一次对象,可以使用setUpClass()代替。

E.g.

class SomeTest(LiveServerTestCase):
        @classmethod
        def setUpClass(cls):
            # create objects here
            LiveServerTestCase.setUpClass()

Don't forget to call LiveServerTestCase.setUpClass() or the live server won't function properly.

不要忘记调用LiveServerTestCase.setUpClass(),否则实时服务器将无法正常运行。