Summary
There are many strategies and approaches in testing Flask applications, but this chapter focuses on the components found in our applications from Chapters 1 to 9. Also, the goal is to build test cases using the straightforward syntax of the pytest module.
This chapter started with testing the standard Flask components with the web views, API functions, repository transactions, and native services. Aside from simply running the components and verifying their response details using the assert statement, mocking becomes an essential ingredient in many test cases of this chapter. The patch() decorator from the unittest module mocks the psycopg2 connections, repository transactions in views and services, and the SQLAlchemy utility methods. This chapter also discussed monkey patching, which replaces a function with a mock one, and exception testing, which determines raised exceptions and undetected bugs.
This chapter also established proof that it is easier to test asynchronous...