Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking `background_tasks.add_task`

In my FastAPI application, I would like to mock background_tasks.add_task so I can spy on the call to it.

However, I can't figure out how to get access to it in order to do so.

like image 248
pondermatic Avatar asked May 21 '26 19:05

pondermatic


1 Answers

Here's a working example. Create two files :

main.py

from fastapi import FastAPI, BackgroundTasks

app = FastAPI()

def write_notification(email: str, message=""):
    with open("log.txt", mode="w") as email_file:
        content = f"notification for {email}: {message}"
        email_file.write(content)


@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks):
    background_tasks.add_task(write_notification, email, message="some notification")
    return {"message": "Notification sent in the background"}

test_main.py

import pytest
from fastapi.testclient import TestClient
from fastapi import BackgroundTasks
from requests import Response

from main import app, write_notification
from unittest.mock import mock_open


@pytest.fixture
def client():
    return TestClient(app)


def test_send_notification(
    client, mocker,
):
    mock_file_open = mocker.patch(
        "main.open", new=mock_open()
    )  # https://docs.python.org/3/library/unittest.mock.html#mock-open
    spy = mocker.spy(BackgroundTasks, "add_task")

    r: Response = client.post("/send-notification/[email protected]")

    # validate the HTTP response
    assert r.status_code == 200
    assert r.json() == {"message": "Notification sent in the background"}

    # validate the add_task call was made
    spy.assert_called_with(
        mocker.ANY,  # add_task() is a method, and as such its first argument is `self`
        write_notification,
        "[email protected]",
        message="some notification",
    )

    # validate the background task did its job
    assert mocker.call("log.txt", mode="w") in mock_file_open.call_args_list
    handle = mock_file_open()
    handle.write.assert_called_once_with(
        "notification for [email protected]: some notification"
    )

and install the following dependencies :

pip install fastapi==0.53.0 pytest==5.4.1 pytest-mock==2.0.0 requests==2.23.0

finally run the pytest command :

pytest test_main.py

Explanations

  • the mocker fixture comes from pytest-mock and is quite convenient for mocking/spying when using pytest.
  • spy = mocker.spy(BackgroundTasks, "add_task") is what you are looking for.

You can clone this gist to try it out.

like image 148
Thomasleveil Avatar answered May 23 '26 07:05

Thomasleveil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!