Testing json responses in Flask REST apps with pytest

Testing is an essential part of software developmnet process. Unfortunately best prictives for python are established not as good as for example in Java world. Here I try to explain how to test Flask-based web applications. We want to test endpoints behaviour including status codes and parameters encoding. It means testing of handler functions for those endpoints is not enough. Tests for endpoints can be considered/used as high-level acceptance tests....

November 27, 2016 · SergeM

Python logging best practices

Python has a great built-in logging library. It allows to avoid a lot of boilerplate code and provides great features. Here are some examples. Logging allows to set up a unified formatting for logs: 2023-10-03 12:57:22,377|classifier.py| 62|522130|MainThread|INFO|Classifying messages from ./messages.ndjson (images ./save_path)... 2023-10-03 12:57:22,378|classifier.py| 44|522130|MainThread|INFO|Start reading input messages from ./messages.ndjson 2023-10-03 12:57:22,378|classifier.py| 48|522130|MainThread|INFO|Loaded event {'id': 104162, 'date': '2023-09-27T07:12:08+00:00', 'entities': []} 2023-10-03 12:57:22,378|classifier.py| 48|522130|MainThread|INFO|Loaded event {'id': 58934, 'date': '2023-09-27T07:12:09+00:00', 'entities': [{'_': 'MessageEntityBold', 'offset': 92, 'length': 8}]} 2023-10-03 12:57:22,378|classifier....

April 21, 2015 · SergeM