Flask + pytest example/ boilerplate

Hey everybody. During the last few days I have been reading about "tests" and how to do them with '''pytest'''. The problem is Im having troubles to make it work with Flask. Do any of you have a simple example or boiler plate I can use as a guide? (Im using sqlite as a database). Thanks.

3 comments · 94% upvoted · other discussions

comments

alexisprince

Consider looking at flask-testing. That might not be 100% the library you want, but the documentation will give you a good place to start.

FYI, if you’re interacting with a relational database in flask, I highly suggest using an ORM (Object Relational Mapper). The most popular is SQLAlchemy, and there’s an integration with Flask called flask-sqlalchemy. This should completely abstract away which database you’re using, allowing you to not worry about whether you’re currently connected to SQLite, Postgres, or MySQL. You need to worry about choosing which of them to use of course, but the code is the same according to the ORM.

[deleted]

There are cookie cutter projects on git hub, these are great starting points for a new package and have a lot of the difficulties in packaging already set up. The one below uses flask, to use it you need to pip or conda install cookiecutter and give it this url, there are more details in the read me on this page:

https://github.com/cookiecutter-flask/cookiecutter-flask

guazir

You will need to call the flask test_client method. Look into creating a conftest.py file that yields a client to reuse in your test file.

See here

https://github.com/taverntesting/tavern/issues/39