使LiveServerTestCase在每次测试前都不调用setUp() [英] Make LiveServerTestCase not to call setUp() before each test

查看:120
本文介绍了使LiveServerTestCase在每次测试前都不调用setUp()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

setUp()在每次测试前都会被调用.

setUp() gets called before every test.

如果要一次为entrire测试用例创建对象,则可以改用setUpClass().

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

例如

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

别忘了调用LiveServerTestCase.setUpClass(),否则实时服务器将无法正常运行.

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

这篇关于使LiveServerTestCase在每次测试前都不调用setUp()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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