Django 测试未加载夹具数据

发布于 2024-10-11 16:48:07 字数 756 浏览 4 评论 0原文

我已经为我正在开发的 Django 项目编写了测试,但一个特定的装置无法加载。 该装置是使用转储数据生成的,我根本没有摆弄它。 我可以使用该夹具上的manage.py 加载数据,不会出现错误。我已经使用 shell 和查询数据验证了实际加载的数据。 这让我发疯,任何帮助将不胜感激。

这是我的测试文件(删除了不相关的部分):

class ViewsFromUrls(TestCase):
    fixtures = [
        'centers/fixtures/test_data.json',
        'intranet/fixtures/test_data.json',
        'training/fixtures/test_data.json', #The one that fails to load
        ]

    def setUp(self):
        self.c = Client()
        self.c.login(username='USER', password='PASS')

    ...

    def test_ViewBatch(self):
        b = Batch.objects.all()[0].ticket_number
        response = self.c.get(reverse('training.views.view_batch', kwargs={'id':b}))
        self.assertTrue(response.status_code, 200)
    ...

I have written tests for a Django project that i am working on, but one particular fixture fails to load.
The fixture is generated using dumpdata and i havent fiddled with it at all.
I can load the data using manage.py on that fixture without errors. I have verified that the data actually loaded using shell and querying the data.
This is driving me nuts, any help would be much appreciated.

Here is my test file (irrelevant portions removed):

class ViewsFromUrls(TestCase):
    fixtures = [
        'centers/fixtures/test_data.json',
        'intranet/fixtures/test_data.json',
        'training/fixtures/test_data.json', #The one that fails to load
        ]

    def setUp(self):
        self.c = Client()
        self.c.login(username='USER', password='PASS')

    ...

    def test_ViewBatch(self):
        b = Batch.objects.all()[0].ticket_number
        response = self.c.get(reverse('training.views.view_batch', kwargs={'id':b}))
        self.assertTrue(response.status_code, 200)
    ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

始于初秋 2024-10-18 16:48:07

django.test导入测试用例:

from django.test import TestCase

class test_something(TestCase):
    fixtures = ['one.json', 'two.json']
    ...
  • 不是:import unittest
  • 不是:import django.utils.unittest
  • 而是:import django.test

那是令人沮丧的一天。
停止抱怨 - 它在文档中:-/

Import the TestCase from django.test:

from django.test import TestCase

class test_something(TestCase):
    fixtures = ['one.json', 'two.json']
    ...
  • Not: import unittest
  • Not: import django.utils.unittest
  • But: import django.test

That's a day of frustration right there.
Stop complaining - it's in the docs :-/

还如梦归 2024-10-18 16:48:07

我不确定这是否可以解决您的问题,但在此站点上:

https://code.djangoproject.com/wiki/Fixtures

我发现一个有趣的评论:

您会看到 Django 搜索appnames/fixtures 并
settings.FIXTURE_DIRS 并加载第一个匹配项。所以如果你使用名字
就像你的装置的 testdata.json 一样,你必须确保没有其他
活动应用程序使用同名的装置。如果没有,您可以
永远不要确定你实际加载的是什么装置。因此它是
建议您在灯具前加上应用程序名称前缀,
例如 myapp/fixtures/myapp_testdata.json 。

应用此(使用应用程序名称作为文件名中的前缀重命名灯具)解决了我的问题(我遇到了与此处描述的相同的问题)

I Am not sure if this fixes your problem, but on this site:

https://code.djangoproject.com/wiki/Fixtures

I found an interesting remark:

you see that Django searches for appnames/fixtures and
settings.FIXTURE_DIRS and loads the first match. So if you use names
like testdata.json for your fixtures you must make sure that no other
active application uses a fixture with the same name. If not, you can
never be sure what fixtures you actually load. Therefore it is
suggested that you prefix your fixtures with the application names,
e.g. myapp/fixtures/myapp_testdata.json .

Applying this (renaming the fixtures with appname as prefix in the filename), solved my problem (I had the same issue as described here)

戏蝶舞 2024-10-18 16:48:07

检查夹具是否确实处于正确的位置。来自文档

Django 将在三个位置进行搜索
对于固定装置:

  1. 在每个已安装应用程序的装置目录中
  2. 在 FIXTURE_DIRS 设置中指定的任何目录中
  3. 在夹具命名的文字路径中

Check if the fixture is really in the right place. From the docs:

Django will search in three locations
for fixtures:

  1. In the fixtures directory of every installed application
  2. In any directory named in the FIXTURE_DIRS setting
  3. In the literal path named by the fixture
合约呢 2024-10-18 16:48:07

需要注意的一件事是,在设置文件中创建 FIXTURE_DIRS 常量时,如果您的项目根目录中有通用固定装置目录,请务必省略前导“/”。

例如:
'/actual/path/to/my/app/fixtures/'

现在,在 settings.py 文件中:
不会工作:
FIXTURE_DIRS = '/fixtures/'

将起作用:
FIXTURE_DIRS = 'fixtures/'

这可能取决于您的其他路由的配置方式,但这是一个让我摸不着头脑的问题。希望这有用。干杯。

One thing to note, when creating the FIXTURE_DIRS constant in your settings file, be sure to leave out the leading '/' if you have a general fixtures directory off of the root of your project.

Ex:
'/actual/path/to/my/app/fixtures/'

Now, in the settings.py file:
Will NOT work:
FIXTURE_DIRS = '/fixtures/'

Will work:
FIXTURE_DIRS = 'fixtures/'

It's possible this depends on how your other routes are configured, but it was a gotcha that had me scratching my head for a little while. Hope this is useful. Cheers.

锦上情书 2024-10-18 16:48:07

我犯的一个简单错误是添加了一个自定义 setUpClass() 并忘记在其中包含 super().setUpClass() (当然,这就是 Django 加载逻辑的位置)固定装置寿命)

A simple mistake I made was adding a custom setUpClass() and forgetting to include super().setUpClass() with it (which of course, is where Django's logic for loading fixtures lives)

枕头说它不想醒 2024-10-18 16:48:07

对我来说:需要 TestCase 而不是 SimpleTestCase! import pdb后在postgres中仍然看不到数据; pdb.set_trace() 但数据在那里,正如我的 API 调用测试所报告的那样。

For me: required TestCase rather than SimpleTestCase! Still don't see the data in postgres after import pdb; pdb.set_trace() but the data is there as reported by my API Call tests..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文