[TIP] Testing Flask apps in a pyvenv environment

holger krekel holger at merlinux.eu
Thu Jul 9 00:59:59 PDT 2015

On Thu, Jul 09, 2015 at 11:33 +0800, Paradox wrote:
> I am trying to learn TDD using the tutorial here:
> https://github.com/mjhea0/flaskr-tdd
> The only thing I have done differently from the tutorial is to use
> pyvenv rather than virtualenv and to use py.test for my test runner.
> When I get to the point of running the first test (on returning the
> "Hello, World!" page) the second time (after the app.py file is
> created and filled) it still fails as follows:
> app-test.py:1: in <module>
>     from app import app
> app.py:1: in <module>
>     from flask import Flask
> E   ImportError: No module named 'flask'
> Running the flask app works fine, it starts the development server
> and returns the proper page, there seems to be something wrong with
> the test file or the test runner.

Seems like you have a problem with the import paths.

is py.test coming from the pyvenv ("which py.test")?  If you start 
"python" from within the pyvenv can you "import flask"?  

you can also try "PYTHONPATH=`pwd` py.test app-test.py" to make sure 
that "app" is discovered from the import path.


> Incidentally when I run the test without py.test it finds the module
> but fails with an error in the returned string:
> AssertionError: b'Hello, World!' != 'Hello, World!'
> I haven't figured that one out yet either!
> Is there a reason py.test can't find the flask module in the pyvenv
> created virtual environment?
> thomas
> ===============
> Thomas C. Hicks, MD, MPH
> Training Manager
> Gansu Gateway, Lanzhou, Gansu
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python

about me:    http://holgerkrekel.net/about-me/
contracting: http://merlinux.eu

More information about the testing-in-python mailing list