一次加载Django装置?

时间:2018-07-05 14:50:37

标签: python django django-tests

我知道曾问过这个question,但我想看看是否有更新的解决方案。有没有办法将我所有的夹具加载到setUp中,并在所有测试完成后冲洗它们?

现在,我正在像这样加载灯具...

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

使用django的较新版本,有没有办法或者更好的方法来做到这一点?

1 个答案:

答案 0 :(得分:1)

我不确定您是否知道,但是您只是按以下方式加载灯具:

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()
     

docs

中的示例

因此,您无需像当前示例中那样用GlobalSetup来编写call_command,这将导致两次加载灯具。因为该方法已经在setUpClass中调用(请参阅此link