pytest
pytest copied to clipboard
Need help in mocking flask routes using pytest
this is my flask route... i want to mock it since it's response is dependent on external db connection..
@blueprint.route('/gethistory/', methods=['POST','GET'])
def get_history():
(c, conn) = connection()
param = None
try:
custom_query = "select * from osupgrade_staging_history"
result_rows = db_execute_sql(c, custom_query, param)
row_count = result_rows.fetchall()
logger.debug(row_count)
return make_response(jsonify(row_count), 200)
except Exception as e:
logger.error("Exception raised :- {}".format(str(e)), exc_info=True)
return make_response(jsonify({"result": "failed"}), 500)
finally:
conn.close()
# am trying to mock the response but seems it's still returning status 200
# ( which is coming from db)
@patch("main.inside.get_history")
def test_get_history_exception(mock_get_history):
with currentapp.test_request_context():
mock_get_history.return_value.status_code = 500
assert get_history().status_code == 500
mock_get_history.return_value.status_code = 500 --> this is not working.. am sure am doing something terribly wrong..
my actual route is using an external DB query to get the response. i want to mock that part .
Closing this question as inactive. Maybe try StackOverflow?