简体   繁体   English

一次加载Django装置?

[英]Load Django Fixtures Once?

I know this question was asked before, but I was wanting to see if there is a more updated solution. 我知道以前曾问过这个问题 ,但我想看看是否有更新的解决方案。 Would there be a way to load all my fixtures in setUp and and flush them when all tests are finished? 有没有办法将我所有的夹具加载到setUp ,并在所有测试完成后冲洗它们?

Right now, I'm loading in my fixtures like so... 现在,我正在像这样加载我的装置...

from django.test import TestCase
from django.core.management import call_command

class GlobalSetup(TestCase):

    def setUp(self):
        # Load fixtures
        call_command('loaddata', 'test_cfst.json', verbosity=0)
        call_command('loaddata', 'test_lmt.json', verbosity=0)
        call_command('loaddata', 'test_qt.json', verbosity=0)

class BaseTest(GlobalSetup):
    fixtures = [
        'test_cfst.json',
        'test_lmt.json',
        'test_qt.json'
        ]

    def setUp(self):
        super(BaseTest, self).setUp()

    def test_base(self):
        # Some random tests

With the newer version of django is there a way, or better way, to do this? 有了较新版本的django,有没有一种方法或更好的方法可以做到这一点?

I am not sure that you are aware or not but you just load fixtures as below: 我不确定您是否知道,但是您只是按如下方式加载灯具:

 from django.test import TestCase from myapp.models import Animal class AnimalTestCase(TestCase): fixtures = ['mammals.json', 'birds'] def setUp(self): # Test definitions as before. call_setup_methods() def test_fluffy_animals(self): # A test that uses the fixtures. call_some_test_code() 

example from the docs 文档中的示例

So you don't need to write GlobalSetup with call_command as you did in your current example which is leading to load the fixtures twice. 因此,您无需像在当前示例中那样用call_command编写GlobalSetup ,这将导致两次加载灯具。 because the method is already called in setUpClass(refer this link ) 因为该方法已经在setUpClass中调用(请参阅此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM