【问题标题】:Django Rest Framework - Loading fixtures in an APITestCase?Django Rest Framework - 在 APITestCase 中加载夹具?
【发布时间】:2014-08-13 22:56:10
【问题描述】:

我正在使用 django-rest-framework 创建 API。我正在使用框架的APITestCase 来测试我的一些端点。

文档没有指定加载固定装置的方法,传统的fixtures= ['initial_data.json'] 似乎不起作用。

如何加载固定装置?

【问题讨论】:

  • 你想知道django中如何加载fixture?

标签: django django-rest-framework


【解决方案1】:

您描述的方式也应该有效。确保您在测试类中声明了固定装置。下面是一个例子:

class MyViewsTestCase(APITestCase):

    fixtures = ['some_testdata.json']

    def test_random_thingy(self):
        variable = 'hello'
        self.assertEqual(variable, 'hello')

每次运行测试时,都会在测试运行后加载和移除固定装置。

【讨论】:

  • 谢谢@timop。我的错误是将行 fixtures = [...] 放在我的 setup 函数中。
【解决方案2】:

Fixtures 与您正在测试应用程序这一事实没有任何关系。您所需要的只是应用文件夹中的 fixtures 文件夹,其中必须包含一个名为 initial_data.json 的文件。当您运行 migrate 或 syncdb 时,fixture 会自动插入到您的数据库中。

它应该是这样的:

your_app
  |
  |__ models.py
  |__ views.py
  |__ fixtures
         |
         |__ initial_data.json

您不需要包含/编写任何其他内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2018-03-03
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多