How to test uwsgi cron tasks

Hi everyone!

I am happy to be part of this forum. I would like to ask some may be a bit newbie question.
How I can unit test uwsgi task defined this way:

@cron(-1, -1, -1, -1, -1, target='spooler')
def send_mail(signum):
    """Send email every minute if any."""

    from django.core.management import call_command
    call_command('send_mail')

This cron is executed only when uwsgi is running.
On the other hand, coverage shows me that this line
is missed, i.e. not tested.

How I can test this code/line ?

I’m not sure how you’d test the cron behavior itself, but to test the code in the function, I think I would try to call it directly. I’d probably mock out the call_command because that would run a lot of code.

Something like (assuming a pytest-style test function):

from unittest import mock

from mymodule import send_mail

@mock.patch('mymodule.call_command')
def test_send_email_cront(mock_call_command):
    send_mail(42)

    mock_call_command.assert_called_once_with('send_email')
1 Like