Celery Pytest integration test not working properly with django emails

I made a task for celery and tested it with as a unit with this test:

@pytest.mark.django_db
def test_document_expiry_alert_unit(settings):
    settings.CELERY_TASK_ALWAYS_EAGER = True

    manager = UserFactory(
        email="manager@mail.com", role=Role.MANAGER, organization__check_docs=True
    )
    doc = EmployeeDocumentFactory(user__organization=manager.organization)

    mail.outbox = []

    document_expiry_alert.delay()
    assert len(mail.outbox) == 1

    sent = mail.outbox[0]
    html_content = sent.alternatives[0][0]
    assert manager.email in sent.to
    assert doc.get_type_display() in html_content
    assert doc.user.get_full_name() in html_content
    assert "documents are expiring tomorrow" in sent.body

It worked and the test passes, then I tried to make an integration test, so I installed celery[pytest] and add the config in pytest along with the plugin in conftest.py, celery.contrib.pytest

Now I tried running this test:

@pytest.mark.django_db(transaction=True)
def test_document_expiry_alert_intergration(celery_app, celery_worker):
    manager = UserFactory(
        email="manager@mail.com", role=Role.MANAGER, organization__check_docs=True
    )
    EmployeeDocumentFactory(user__organization=manager.organization)

    mail.outbox = []

    celery_app.send_task("organizations.tasks.document_expiry_alert")
    celery_worker.reload()
    assert len(mail.outbox) == 1
    assert manager.email in mail.outbox[0].to

But I get this error FAILED tests/organizations/task_tests.py::test_document_expiry_alert_intergration - assert 0 == 1 which means the mailbox has no mail sent, I tried using @patch to mock the send, but I got also zero call counts. The logs and print statements are showing in my terminal when running the test, so everything before the point I send the email, everything works. I'm using django.core.mail.EmailMultiAlternatives to send the email.

Вернуться на верх