Is there a way to debug Flask app via unittest?

32 views Asked by At

I've build a simple Flask app that takes a json as input, performs some types of operations and returns a json as the output.

What I can do:


Now I would like to be able to test and debug certain components of the app itself, I'm partially able to do this by running the flask app as follows:

app.py

import flask

app = flask.Flask('myapp')

@app.route("/api/my_app_function", methods=['POST'])
def my_function():
    # do things
    return output, {'Content-Type': 'application/json; charset=utf-8'}

if __name__ == '__main__':
    app.run(debug=True)

From this I run the relative from Pycharm as follows:

Run Button

Debug Run

Now I can simply execute a call directly on the running app with another script and then debugging it:

app_that_recall_my_flask_app.py

import requests

session = requests.Session()
api_call= requests.Request('POST', SERVER_LIST['local'],
                            headers={'accept': 'application/json'},
                            json=json_input).prepare()
resp = session.send(api_call)

In this way I land directly within the functions that my app performs.

What I can't do


The same principle does not apply if I try to do it with the unittest library

my_unittest.py

class TestEndpoint(unittest.TestCase):       
    def test_unittest(self):
        session = requests.Session()
        api_call= requests.Request('POST', SERVER_LIST['local'],
                                headers={'accept': 'application/json'},
                                json=json_input).prepare()
        resp = session.send(api_call)

In such a way the the session.send directly returns the json without me letting me see the function where I've placed my breakpoint.


Reproducibility:

Pycharm==2023.2.3
Python==3.9.18
Flask==2.3.3
0

There are 0 answers