【问题标题】:Fixtures seem to be ignored in Django tests在 Django 测试中,fixture 似乎被忽略了
【发布时间】:2014-01-08 11:00:08
【问题描述】:

我有一个 Django 应用程序,它基本上看起来像这样:

app/
    fixtures/
        file.json
    ...
    tests/
        __init__.py
        settings.py
        tests.py

我已经在tests.py 中加载了我的夹具,它基本上是这样的:

from django.test import TestCase

class TestFile(TestCase):

    fixtures = ['file.json']

    ...

我的夹具文件如下所示:

[
    {
        "model": "sites.site", 
        "pk": 100, 
        "fields": {
          "domain": "myproject.mydomain.com", 
          "name": "My Project"
        }
    },
]

我的settings.py 在INSTALLED_APPS 中有django.contrib.sites。

但是,当我尝试使用 py.test 运行我的测试时,这个夹具似乎被忽略了,因为我使用 ipdb 中断了我的一些测试,查询 Site.objects.all(),而我的夹具站点不是显示。可能是什么问题?

我运行我的测试:py.test --ds=app.tests.settings --pyargs app.tests.tests -s(-s 用于允许ipdb 的输出)。 `

【问题讨论】:

  • 使用 manage.py loaddata app/fixtures/file.jsson 是否正确加载了灯具?
  • 没有,但我发现我忘记在INSTALLED_APPS 中包含app...现在灯具已安装,我可以看到我的灯具站点。

标签: python django fixtures pytest


【解决方案1】:

似乎问题实际上是一个非常明显的细节:我忘记在我的settings.py 文件中的INSTALLED_APPS 中包含app。夹具现在已安装,我可以看到我的夹具站点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-28
    • 2016-09-30
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2012-03-08
    • 2015-12-11
    相关资源
    最近更新 更多