Test with mock pass when run individually but not when together

I want test if a function is called in a view.

My view is something like that:

@api_view(["POST"])
def my_view(request):
    data = request.data
    my_function(data)
    return Response("ok")

And my test:

@pytest.fixture
def mock_my_function(mocker):
    return mocker.patch("path.for.my.function")

def test_my_test(
    mock_my_function, db, client
):
    data = {"some": "thing"}
    resp = client.post(
        "/my/url/",
        data=data,
        format="json",
    )
    assert resp.data == "ok"
    mock_my_function.assert_called()

Running this test individually, it's ok. Works! But, when I run all tests, this test fails. Pytest show me this error:

E       AssertionError: Expected 'mock_my_function' to have been called.
Back to Top