PyTest - Probleme beim Hinzufügen von Messeln und Patches zu einem Test auf einer RoutePython

Python-Programme
Anonymous
 PyTest - Probleme beim Hinzufügen von Messeln und Patches zu einem Test auf einer Route

Post by Anonymous »

Ich habe eine Flask-API mit einer Route wie: < /p>

Code: Select all

from flask import current_app
from utils.linked_list_handler import get_ll
import json

@detail_bp.route("/get_base_details", methods=["GET"])
def get_all_base_details():
"""Get the details of all nodes with the q_type 'base'"""
ll = get_ll(current_app)
base_list=ll.getByQType("base")
return json.dumps(base_list)
< /code>
, dass ich versuche zu testen. Dazu möchte ich Get_ll () 
und Mock Ll und ll.getByQType () patch patch patch patch. Ich habe versucht, dies zu tun wie: < /p>

Code: Select all

class TestGetBaseDetails:
def test_get_some_base_details(self, test_client: FlaskClient, mocker):
"""
GIVEN a Flask app configured for testing
WHEN the '/get_base_details' route is requested with the
'GET' method while there is a linked list with at least
one Node with a Question with a q_type of "base"
THEN check that the response shows the q_detail of those
base questions
"""
# mock get_ll
mock_get_ll = mocker.patch("backend.routes.blueprints.details.get_ll")
# mock a ll
mock_ll = mocker.Mock()
mock_ll.getByQType.return_value = ["q_detail_1", "q_detail_2"]
# mock the assignment
mock_get_ll.return_value = mock_ll

response = test_client.get("/get_base_details")
assert response.status_code == 200
assert json.loads(response.data) == ["q_detail_1", "q_detail_2"]

mock_get_ll.assert_called_once()
mock_ll.assert_called_once()
< /code>
Diese Tests schlägt jedoch mit der folgenden Nachricht fehl:
FAILED backend/tests/test_details.py::TestGetBaseDetails::test_get_some_base_details - AssertionError: assert [] == ['q_detail_1', 'q_detail_2']
Eine leere Liste ist der Rückgabewert, den die Route angibt, wenn die Suche nichts übereinstimmt. Überprüfen Sie die zwei Assert_called_once () Methoden, es zeigt, dass keiner jemals aufgerufen wird. Ich gehe davon aus, dass dies alles auf einen Fehler in meiner Implementierung von Patch und Mocks zurückzuführen ist, aber ich weiß nicht, was speziell schief gelaufen ist oder wie man darüber nachgibt. < /P>
Ein Rat wäre sehr geschätzt. Vielen Dank im Voraus.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post