pytest-dev / pytest-flask

A set of pytest fixtures to test Flask applications

Home Page:http://pytest-flask.readthedocs.org/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add context for create_app import in quickstart docs

jwflory opened this issue · comments

Summary

Add more context about how to define application fixture

Background

The quickstart docs has this confusing line:

from myapp import create_app

However, to a newcomer like me, it is not clear where the create_app import comes from. If you refer to the Flask quickstart docs, it does not set up a new Flask app with a create_app method.

This is a problem for me since I am importing app in my application, and then in my pytest-flask fixture, I need to override the app.

Details

I am confused about what the ideal example looks like for a Flask web app tested with pytest-flask. It would help to clarify this in the docs a little bit.

If someone could better explain it to me, I could work on a PR to make this improvement too.

Outcome

More straightforward for a newcomer to learn how to test their Flask web application if their app is structured in a different way

I agree with this. I had to do some digging to figure how what the docs meant.
I found #71 helpful.

Hey @jwflory, thanks for the suggestion. The function create_app is a fairly common pattern in the flask community called application factory. Borrowing from the linked flask docs, benefits of app factories include:

  1. Testing. You can have instances of the application with different settings to test every case.
  2. Multiple instances. Imagine you want to run different versions of the same application. Of course you could have multiple instances with different configs set up in your webserver, but if you use factories, you can have multiple instances of the same application running in the same application process which can be handy.

However, I agree with you in the sense that the docs could bring a little more detail on the matter (considering that this, along with #71, makes two issues related to create_app).

I could work on a PR to make this improvement too

Please, feel free to do so. I would be happy to help if needed.

Hi, I appreciate the follow-up! However, I do not have capacity to work on this anymore. When I opened the issue, I worked at a different company on a Flask project. It has been a while since I touched Flask. But I would still appreciate this, for whenever the day comes that I need to jump back onto a Flask project 🙂

@jwflory No worries, I suppose I'll pick up from here and make a PR for this myself. Thanks for reporting the issue!