[TIP] Coverage testing a Flask app running behind uWSGI?

Éric Araujo merwok at netwok.org
Tue Mar 13 13:07:47 PDT 2018

  Hello Skip,

Le 2018-03-13 à 15:41, Skip Montanaro a écrit :
> I have a Flask application whose functions I'd like to check for
> coverage. It unfortunately runs behind uWSGI, and I'm not enough of a
> maven about the intersection of Flask, uWSGI, and coverage to have
> much idea where to begin.

  The idea is to consider uWSGI a detail of your server environment.
A flask app is a Python program that exposes an object implementing the
WSGI spec; that object is used by uWSGI to handle requests, but it can
also be used by webtest, a library that sends requests to your app and
inspects the responses.  Add a testing framework with a test runner
(pyunit or pytest to make life easier) and you are set.

  coverage instruments a Python program and reports coverage of some
source files; here the program would be the test runner.  uWSGI is not
involved at all.

  I would recommend installing pytest, webtest and coverage and write a
first test, then run it again under coverage.  I am used to
pytest-django and pytest-pyramid which create a WebTest fixture wrapping
my WSGI application, but there must be a flask equivalent with docs.

  (I prefer to run «coverage run pytest» and «coverage report» rather
than using a plugin i.e. «pytest --coverage» to get more control about
the options and flags, but YMMV.)


More information about the testing-in-python mailing list