测试时Django夹具的加载顺序是否不正确?

2024-05-12 20:33:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试我的应用程序,我遇到了一个问题,我不知道为什么。我正在为我的测试加载fixture,并且fixture有相互依赖的外键。它们必须按一定的顺序装载,否则就不能工作了。在

我正在装载的设备是:

["test_company_data", "test_rate_index", 'test_rate_description']

公司数据是第一个。test_rate_index有一个公司的外键,test_rate_description有一个指向test_rate_index中声明的模型的外键。(顺便说一句,不同的测试需要不同的固定装置,这就是为什么我不只是把所有的东西都放在一块)

如果我使用django的标准过程进行加载测试,那么测试不会按照正确的顺序加载。在

class TestPackages(test.TestCase):
    fixtures = ["test_company_data", "test_rate_index", "test_rate_description",]

我收到消息

^{pr2}$

但如果我颠倒固定装置的顺序(这毫无意义),它会起作用:

fixtures = ["test_rate_description", "test_company_data", "test_rate_index",]

Django's documentation声明fixture按声明的顺序加载,但情况似乎并非如此。在

而不是使用django的

    call_command('loaddata', *fixtures, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                         })

我在setUp方法中使用了一个不同的函数,一次加载一个fixture。在

def load_fixtures(fixtures):
    for fixture in fixtures:
        call_command('loaddata', fixture, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                            })

在尝试使用标准方法时,是否有一些我做得不正确或不理解的事情导致我的fixture不能按正确的顺序加载?在


Tags: djangotest声明data标准indexrate顺序
1条回答
网友
1楼 · 发布于 2024-05-12 20:33:18

Django's documentation states that the fixtures load in the order they are declared, but this doesn't seem to be the case.

这当然很奇怪。当我测试我的一个项目(django1.2.1、python2.6.2、postgresql8.3.11)时,fixture的加载顺序是正确的。在

下面是我要做的来排除故障。在

DoesNotExist: RateDescription matching query does not exist.

  1. 在加载fixture或执行测试时,是否会出现此错误?你能找到引起这个问题的装置/代码吗?如果需要,可以增加详细程度。

  2. 您可以尝试从命令行启动loaddata命令吗?调用它三次,以正确的预期顺序为每个调用传递一个fixture的名称。看看固定装置有没有装好。

  3. 我知道您可能已经这样做了,但是您能确保第一个和第二个fixture不包含任何RateDescription数据吗?

相关问题 更多 >