Testing Flask Applications with pytest
Thursday, 02 December, 03:30PM – 03:55PM (UTC)
Testing a Flask application helps ensure that your app will work as expected for your end users. This talk details how to create and run tests with pytest, utilize fixtures to initialize the state of the Flask app, and check the coverage of the tests using coverage.py.
Testing a Flask application helps ensure that your app will work as expected for your end users. This talk provides an introduction to using pytest for testing Flask applications. Before diving into how to use pytest, this talk explains what should be tested in a Flask application. Next, detailed examples are covered on how to write unit and functional test functions with pytest that are specific to Flask applications. Documentation of the tests is also highlighted using the GIVEN-WHEN-THEN approach. After writing tests, running the tests using pytest (with different options) is presented to demonstrate how easy and powerful pytest is. Examples include verbose mode, running only the last failed tests, and running specific test cases. Next, the concept of fixtures in pytest is presented and detailed examples of how to initialize the state of a Flask application are covered. This section includes properly initializing a Flask application to avoid not having the application context created. Finally, the idea of code coverage is introduced and an example of running coverage.py on a Flask application is demonstrated.