Creating test cases for web views, repository classes, and native services
The pytest
module supports unit and integration or functional testing. It requires simple syntax to build test cases, which makes it very easy to use, and it has a platform that can automatically run all test files. Moreover, pytest
is a free and open-source module, so install it using the following pip
command:
pip install pytest
However, pytest
will only work with Flask projects with directory structures managed by Blueprints and application factories. Our Online Personal Counselling System in Chapter 1 does not follow the Flask standards on directory structure. All view modules import the app
instance through __main__
, which becomes the pytest
module and not the main.py
module during testing. Thus, testing our ch01
project gives us the following runtime error message:
ImportError cannot import name 'app' from '__main__'
The error means there is no app
object to import in the...