pytest 中的 Celery_worker 固定装置,连接已关闭错误

发布于 2025-01-14 05:33:53 字数 290 浏览 4 评论 0原文

我有这样的 pytest 结构

import pytest

@pytest.mark.django_db
class TestClass:
    def test_celery_mht_notification_create(self, celery_worker, user):
        # some test logic

当我使用 celery_worker 固定装置时,我收到这样的错误 psycopg2.InterfaceError: 连接已经关闭

如何解决这个问题?

I have such pytest structure

import pytest

@pytest.mark.django_db
class TestClass:
    def test_celery_mht_notification_create(self, celery_worker, user):
        # some test logic

When I use celery_worker fixture, I get such error psycopg2.InterfaceError: connection already closed

How to fix that?

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

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

发布评论

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

评论(1

许仙没带伞 2025-01-21 05:33:53

根据解决方案他们在问题创建于pytest-django:

另一方面,我的一个解决方法是作为事务测试运行
@pytest.mark.django_db(transaction=True)

在第一个链接的评论线程中进一步挖掘,恕我直言,一个更干净的解决方案 被提出,并解释了为什么会发生:

如果您正在使用 Py.test 并希望使用进程内工作程序,您可以执行类似的操作

def pytest_configure():
    from celery.fixups.django import DjangoWorkerFixup
    DjangoWorkerFixup.install = lambda x: None

禁用工作修复,这在此上下文中是不必要的。

According to the solution they gave you in the issue created on pytest-django:

On another note, a workaround for me was running as transaction tests with
@pytest.mark.django_db(transaction=True)

Digging a little more in the comments thread of the first link, what IMHO is a cleaner solution is presented, along of an explication of why it happens:

If you're using e.g. Py.test and want to use the in-process worker, you can do something like

def pytest_configure():
    from celery.fixups.django import DjangoWorkerFixup
    DjangoWorkerFixup.install = lambda x: None

to disable the worker fixups, which are unnecessary within this context.

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