简体   繁体   中英

Using flask in a unit test as a callback endpoint?

The REST interface I'm testing involves the use of an HTTP callback. I need a way to write a test against this interface. I test my primary REST server with Flask's test_client feature. One of the endpoints I test uses requests to talk to another server. I need a way to create that other server as part of the unit test.

My preference is to write another Flask server which works as the callback, but I'm not clear on how I can run this during the test. run is a blocking call thus my setUp function won't return. I could put it in a thread, but then I have a teardown issue (I don't know how to tell Flask to stop). I've also wonder if maybe the test_client can give me an actual endpoint.

Your question is too broad to receive a clear answer.

The right way to test it is with the test_client . Refer to Testing Flask applications in Flask docs.

Also, you can use Flask-Testing to help with the skeleton of your tests.

You could mock the response data from the requests calls, using something like:

httpmock

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM