Hands-On Docker for Microservices with Python
上QQ阅读APP看书,第一时间看更新

Testing the code

To test our application, we use the excellent pytest framework, which is the gold standard in test runners for Python applications.

Basically, pytest has a lot of plugins and add-ons to deal with a lot of situations. We will be using pytest-flask, which helps with running tests for Flask applications.

To run all the tests, just call pytest in the command line:

$ pytest
============== test session starts ==============
....
==== 17 passed, 177 warnings in 1.50 seconds =====

Note that pytest has a lot of features available to deal with a lot of situations while testing. Things running a subset of matched tests (the  -k option), running the last failed tests ( --lf), or stopping after the first failure ( -x) are incredibly useful when working with tests. I highly recommend checking its full documentation ( https://docs.pytest.org/en/latest/) and discovering all its possibilities.

There are also a lot of plugins and extensions for using databases or frameworks, reporting code coverage, profiling, BDD, and many others. It is worth finding out about them.

We configure the basic usage, including always enabling flags in the pytest.ini file and the fixtures in conftest.py.