【问题标题】:Make django orm inside celery task use unit test database在 celery 任务中制作 django orm 使用单元测试数据库
【发布时间】:2017-08-17 12:22:33
【问题描述】:

在 django 应用程序中,我有一个运行 Celery 任务的视图。 在 celery 任务中,我使用 Django ORM 对数据库执行了一些操作。

我这样称呼我的测试用例:

class MyViewTestCase(APITestCase):
   def test_my_view(self):
       self.client.post('url',data)

我的测试用例在测试数据库中进行了所有的 orm 操作。 但在 celery 任务中,所有 orm 操作都发生在产品数据库上。

我用 celery 4.

是否可以选择让 celery 与 test django 数据库一起使用?

【问题讨论】:

    标签: django unit-testing celery


    【解决方案1】:

    默认情况下,Django 使用内存中的测试数据库,外部进程无法访问该数据库,例如从命令行启动的 Celery 或守护进程。但是,it is possible to launch a worker as a thread in the Django test processcelery.contrib.testing.worker.start_worker 将根据需要使用测试数据库。

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 2011-10-07
      • 1970-01-01
      • 2011-06-19
      • 2012-06-12
      • 2014-08-04
      • 2014-04-02
      • 2023-04-09
      • 2023-02-12
      相关资源
      最近更新 更多