pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Need help in mocking flask routes using pytest

Open Set4now opened this issue 6 years ago • 3 comments

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

Set4now avatar Mar 09 '20 19:03 Set4now

mock_get_history.return_value.status_code = 500 --> this is not working.. am sure am doing something terribly wrong..

Set4now avatar Mar 09 '20 19:03 Set4now

my actual route is using an external DB query to get the response. i want to mock that part .

Set4now avatar Mar 09 '20 19:03 Set4now

Closing this question as inactive. Maybe try StackOverflow?

Zac-HD avatar May 24 '20 09:05 Zac-HD