A set of py.test fixtures to test Flask applications.
.. image:: https://img.shields.io/pypi/v/pytest-flask.svg :target: https://pypi.python.org/pypi/pytest-flask :alt: PyPi version
.. image:: https://img.shields.io/conda/vn/conda-forge/pytest-flask.svg :target: https://anaconda.org/conda-forge/pytest-flask :alt: conda-forge version
.. image:: https://github.com/pytest-dev/pytest-flask/workflows/build/badge.svg :target: https://github.com/pytest-dev/pytest-flask/actions :alt: CI status
.. image:: https://img.shields.io/pypi/pyversions/pytest-flask.svg :target: https://pypi.org/project/pytest-flask :alt: PyPi downloads
.. image:: https://readthedocs.org/projects/pytest-flask/badge/?version=latest :target: https://pytest-flask.readthedocs.org/en/latest/ :alt: Documentation status
.. image:: https://img.shields.io/maintenance/yes/2022?color=blue :target: https://github.com/pytest-dev/pytest-flask :alt: Maintenance
.. image:: https://img.shields.io/github/last-commit/pytest-dev/pytest-flask?color=blue :target: https://github.com/pytest-dev/pytest-flask/commits/master :alt: GitHub last commit
.. image:: https://img.shields.io/github/issues-pr-closed-raw/pytest-dev/pytest-flask?color=blue :target: https://github.com/pytest-dev/pytest-flask/pulls?q=is%3Apr+is%3Aclosed :alt: GitHub closed pull requests
.. image:: https://img.shields.io/github/issues-closed/pytest-dev/pytest-flask?color=blue :target: https://github.com/pytest-dev/pytest-flask/issues?q=is%3Aissue+is%3Aclosed :alt: GitHub closed issues
.. image:: https://img.shields.io/pypi/dm/pytest-flask?color=blue :target: https://pypi.org/project/pytest-flask/ :alt: PyPI - Downloads
.. image:: https://img.shields.io/github/languages/code-size/pytest-dev/pytest-flask?color=blue :target: https://github.com/pytest-dev/pytest-flask :alt: Code size
.. image:: https://img.shields.io/badge/license-MIT-blue.svg?color=blue :target: https://github.com/pytest-dev/pytest-flask/blob/master/LICENSE :alt: License
.. image:: https://img.shields.io/github/issues-raw/pytest-dev/pytest-flask.svg?color=blue :target: https://github.com/pytest-dev/pytest-flask/issues :alt: Issues
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/ambv/black :alt: style
An extension of pytest
_ test runner which
provides a set of useful tools to simplify testing and development
of the Flask extensions and applications.
To view a more detailed list of extension features and examples go to
the PyPI
_ overview page or
package documentation
_.
Considering the minimal flask application factory
_ below in myapp.py
as an example:
.. code-block:: python
from flask import Flask
def create_app(): # create a minimal app app = Flask(name)
# simple hello world view
@app.route('/hello')
def hello():
return 'Hello, World!'
return app
You first need to define your application fixture in conftest.py
:
.. code-block:: python
from myapp import create_app
@pytest.fixture
def app():
app = create_app()
return app
Finally, install the extension with dependencies and run your test suite::
$ pip install pytest-flask
$ pytest
Don’t hesitate to create a GitHub issue
_ for any bug or
suggestion. For more information check our contribution guidelines
_.
.. _pytest: https://docs.pytest.org/en/stable/ .. _PyPI: https://pypi.python.org/pypi/pytest-flask .. _Github issue: https://github.com/vitalk/pytest-flask/issues .. _package documentation: http://pytest-flask.readthedocs.org/en/latest/ .. _guidelines: https://github.com/pytest-dev/pytest-flask/blob/master/CONTRIBUTING.rst .. _application factory: https://flask.palletsprojects.com/patterns/appfactories/
.. _changelog:
Flask 3.0
-- the consequence is that the deprecated and incompatible request_ctx
has been removed.:meth:pytest_flask.fixtures.live_server
where SESSION_COOKIE_DOMAIN
was set to false due to
original_server_name
defaulting to "localhost".
The new default is "localhost.localdomain".:meth:live_server.url
request_ctx is now deprecated
and will be removed in the futureJSONReponse.json
removed in favour of
Werkzeug.wrappers.Response.json
socket
instead of server
pulling (#58
) to check server availability and add new
--live-server-wait
option to set the live server wait timeout.
Thanks to @jadkik
.Important
live_server
is now session
-scoped by default. This can be changed by using the live-server_scope
option in your pytest.ini
(#113
). Thanks @havok2063
for the initial patch and @TWood67
_ for finishing it up.
pytest 5.2 or later is now required.
Python 2.7 and 3.4 are no longer supported.
.. _@havok2063: https://github.com/havok2063 .. _@TWood67: https://github.com/TWood67 .. _#113: https://github.com/pytest-dev/pytest-flask/pull/113
ImportError
with Werkzeug 1.0.0rc1
(#105
_)... _#105: https://github.com/pytest-dev/pytest-flask/pull/105
options
marker (#97
_)... _#97: https://github.com/pytest-dev/pytest-flask/pull/97
New --live-server-host
command-line option to set the host name used by
the live_server
fixture.
Thanks @o1da
_ for the PR (#90
_).
.. _@o1da: https://github.com/o1da .. _#90: https://github.com/pytest-dev/pytest-flask/pull/90
JSONReponse
now supports comparison directly with status codes:
.. code-block:: python
assert client.get('invalid-route', headers=[('Accept', 'application/json')]) == 404
Thanks @dusktreader
_ for the PR (#86
_).
.. _@dusktreader: https://github.com/dusktreader .. _#86: https://github.com/pytest-dev/pytest-flask/pull/86
pytest-flask
now requires pytest>=3.6
(#84
_).
Add new --live-server-port
option to select the port the live server will use (#82
).
Thanks @RazerM
for the PR.
Now live_server
will try to stop the server cleanly by emitting a SIGINT
signal and
waiting 5 seconds for the server to shutdown. If the server is still running after 5 seconds,
it will be forcefully terminated. This behavior can be changed by passing
--no-live-server-clean-stop
in the command-line (#49
).
Thanks @jadkik
for the PR.
Internal fixes silence pytest warnings, more visible now with pytest-3.8.0
(#84
_).
.. _@jadkik: https://github.com/jadkik .. _@RazerM: https://github.com/RazerM .. _#49: https://github.com/pytest-dev/pytest-flask/issues/49 .. _#82: https://github.com/pytest-dev/pytest-flask/pull/82 .. _#84: https://github.com/pytest-dev/pytest-flask/pull/84
Implement deployment using Travis, following in line with many other pytest plugins.
Allow live server to handle concurrent requests (#56
), thanks to
@mattwbarry
for the PR.
Fix broken link to pytest documentation (#50
), thanks to
@jineshpaloor
for the PR.
Tox support (#48
), thanks to @steenzout
for the PR.
Add LICENSE
into distribution (#43
), thanks to @danstender
.
Minor typography improvements in documentation.
Add changelog to documentation.
.. _#43: https://github.com/vitalk/pytest-flask/issues/43 .. _#48: https://github.com/pytest-dev/pytest-flask/pull/48 .. _#50: https://github.com/pytest-dev/pytest-flask/pull/50 .. _#56: https://github.com/pytest-dev/pytest-flask/pull/56 .. _#58: steenzouthttps://github.com/pytest-dev/pytest-flask/pull/58 .. _@danstender: https://github.com/danstender .. _@jadkik: https://github.com/jadkik .. _@jineshpaloor: https://github.com/jineshpaloor .. _@mattwbarry: https://github.com/mattwbarry .. _@steenzout: https://github.com/steenzout
Add --start-live-server
/--no-start-live-server
options to prevent
live server from starting automatically (#36
), thanks to @EliRibble
.
Fix title formatting in documentation.
.. _#36: https://github.com/vitalk/pytest-flask/issues/36 .. _@EliRibble: https://github.com/EliRibble
Rename marker used to pass options to application, e.g. pytest.mark.app
is now pytest.mark.options
(#35
_).
Documentation badge points to the package documentation.
Add Travis CI configuration to ensure the tests are passed in supported
environments (#32
_).
.. _#32: https://github.com/vitalk/pytest-flask/issues/32 .. _#35: https://github.com/vitalk/pytest-flask/issues/35
request_ctx
fixture which contains all request relevant
information (#29
_)... _#29: https://github.com/vitalk/pytest-flask/issues/29
monkeypath
fixture to teardown application config (#27
_)... _#27: https://github.com/vitalk/pytest-flask/issues/27
#21
_)... _#21: https://github.com/vitalk/pytest-flask/issues/21
monkeypatch
fixture to rewrite live server name.#24
), as per "Python Packaging User Guide" <https://packaging.python.org/en/latest/single_source_version.html#single-sourcing-the-version>
... _#24: https://github.com/vitalk/pytest-flask/issues/24
#20
_)... _#20: https://github.com/vitalk/pytest-flask/issues/20
#18
), thanks
to @greedo
... _#18: https://github.com/vitalk/pytest-flask/issues/18 .. _@greedo: https://github.com/greedo
#17
),
thanks to @davehunt
... _#17: https://github.com/vitalk/pytest-flask/issues/17 .. _@davehunt: https://github.com/davehunt
Bind live server to a random port instead of 5000 or whatever is passed on
the command line, so it’s possible to execute tests in parallel via
pytest-dev/pytest-xdist (#15
). Thanks to @davehunt
.
Remove --liveserver-port
option.
.. _#15: https://github.com/vitalk/pytest-flask/issues/15 .. _@davehunt: https://github.com/davehunt
--liveserver-port
, thanks to @svenstaro
_... _@svenstaro: https://github.com/svenstaro
live_server
fixture uses to run application in the background (#11
),
thanks to @svenstaro
... _#11: https://github.com/vitalk/pytest-flask/issues/11 .. _@svenstaro: https://github.com/svenstaro
client_class
fixture for class-based tests.#8
_)... _#8: https://github.com/vitalk/pytest-flask/issues/8
codecs
module to open files to prevent possible errors on open
files which contains non-ascii characters.First release on PyPI.
The MIT License (MIT)
Copyright © 2014–2016 Vital Kudzelka and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.