Pytest Flask Readthedocs Io en Latest
Pytest Flask Readthedocs Io en Latest
Release 1.0.1.dev44+g04c1255
Vital Kudzelka
1 Quickstart 3
2 User’s Guide 5
2.1 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.1 Step 1. Install . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.2 Step 2. Configure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.3 Step 3. Run your test suite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.1.4 What’s next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Feature reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2.1 Fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.1.1 client - application test client . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.1.2 client_class - application test client for class-based tests . . . . . . . . . . . . 7
2.2.1.3 config - application config . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2.1.4 live_server - application live server . . . . . . . . . . . . . . . . . . . . . . . 8
2.2.1.5 request_ctx - request context (Deprecated) . . . . . . . . . . . . . . . . . . . 9
2.2.1.6 HTTP Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2.1.7 Content negotiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.2.2 Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2.2.1 pytest.mark.options - pass options to your application config . . . . . . . . 11
2.3 How to contribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.1 How to report issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.3.2 Setting up your development environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3.3 Start Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3.4 How to run tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.3.5 Checking Test Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4 Changelog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.1 1.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.2 1.2.0 (2021-02-26) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.3 1.1.0 (2020-11-08) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.4 1.0.0 (2020-03-03) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.5 0.15.1 (2020-02-03) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.4.6 0.15.0 (2019-05-13) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4.7 0.14.0 (2018-10-15) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4.8 0.13.0 (2018-09-29) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4.9 0.12.0 (2018-09-06) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4.10 0.11.0 (compared to 0.10.0) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
i
2.4.11 0.10.0 (compared to 0.9.0) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4.12 0.9.0 (compared to 0.8.1) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.13 0.8.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.14 0.8.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.15 0.7.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.16 0.7.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.17 0.7.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.18 0.7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.19 0.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.20 0.7.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.21 0.6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4.22 0.6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.23 0.6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.24 0.6.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.25 0.5.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.26 0.4.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.27 0.3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.28 0.3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.29 0.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.4.30 0.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Index 17
ii
pytest-flask Documentation, Release 1.0.1.dev44+g04c1255
Pytest-flask is a plugin for pytest that provides a set of useful tools to test Flask applications and extensions.
Contents 1
pytest-flask Documentation, Release 1.0.1.dev44+g04c1255
2 Contents
CHAPTER 1
Quickstart
@pytest.fixture
def app():
app = create_app()
return app
Now you can use the app fixture in your test suite. You can run your tests with:
pytest
3
pytest-flask Documentation, Release 1.0.1.dev44+g04c1255
4 Chapter 1. Quickstart
CHAPTER 2
User’s Guide
This part of the documentation will show you how to get started in using pytest-flask with your application.
Pytest is capable to pick up and run existing tests without any or little configuration. This section describes how to get
started quickly.
@pytest.fixture
def app():
app = create_app()
return app
5
pytest-flask Documentation, Release 1.0.1.dev44+g04c1255
pytest
The Feature reference section gives a more detailed view of available features, as well as test fixtures and markers.
Consult the pytest documentation for more information about pytest itself.
If you want to contribute to the project, see the How to contribute section.
def test_api_ping(client):
res = client.get(url_for('api.ping'))
assert res.json == {'ping': 'pong'}
Note: User-defined json attribute/method in application response class will not be overwritten. So you can
define your own response deserialization method:
from flask import Response
from myapp import create_app
class MyResponse(Response):
'''Implements custom deserialization method for response objects.'''
@property
def json(self):
return 42
@pytest.fixture(scope="session")
def app():
app = create_app()
app.response_class = MyResponse
return app
• Running tests in parallel with pytest-xdist. This can lead to significant speed improvements on multi core/multi
CPU machines.
This requires the pytest-xdist plugin to be available, it can usually be installed with:
Not enough pros? See the full list of available fixtures and markers below.
2.2.1 Fixtures
pytest-flask provides a list of useful fixtures to simplify application testing. More information on fixtures and
their usage is available in the pytest documentation.
Hint: During test execution a request context will be automatically pushed for you, so context-bound methods can be
conveniently called (e.g. url_for, session.
Example:
def test_myview(client):
assert client.get(url_for('myview')).status_code == 200
Example:
@pytest.mark.usefixtures('client_class')
class TestSuite:
def test_myview(self):
assert self.client.get(url_for('myview')).status_code == 200
Run application in a separate process (useful for tests with Selenium and other headless browsers).
Hint: The server’s URL can be retrieved using the url_for function.
@pytest.mark.usefixtures('live_server')
class TestLiveServer:
def test_server_is_up_and_running(self):
res = urllib2.urlopen(url_for('index', _external=True))
assert b'OK' in res.read()
assert res.code == 200
By default the server will start automatically whenever you reference live_server fixture in your tests. But starting
live server imposes some high costs on tests that need it when they may not be ready yet. To prevent that behaviour
pass --no-start-live-server into your default options (for example, in your project’s pytest.ini file):
[pytest]
addopts = --no-start-live-server
Note: You should manually start live server after you finish your application configuration and define all required
routes:
def test_add_endpoint_to_live_server(live_server):
@live_server.app.route('/test-endpoint')
def test_endpoint():
return 'got it', 200
live_server.start()
The timeout after which test case is aborted if live server is not started.
By default the server uses a random port. In some cases it is desirable to run the server with a fixed port. You can use
--live-server-port (for example, in your project’s pytest.ini file):
[pytest]
addopts = --live-server-port=5000
Hint: The request context has been pushed implicitly any time the app fixture is applied and is kept around during
test execution, so it’s easy to introspect the data:
from flask import request, url_for
def test_request_headers(client):
res = client.get(url_for('ping'), headers=[('X-Something', '42')])
assert request.headers['X-Something'] == '42'
By default, the server will be scoped to session for performance reasons, however if your server has global state
and you want better test isolation, you can use the live_server_scope ini option to change the fixture scope:
[pytest]
live_server_scope = function
Common request methods are available through the internals of the Flask API. Specifically, the API creates the default
flask.Flask.test_client instance, which works like a regular Werkzeug test client.
Examples:
def test_post_request(client, live_server):
@live_server.app.route('/load-data')
def get_endpoint():
return url_for('name.load', _external=True)
live_server.start()
res = client.post(
get_endpoint(),
headers={'Content-Type': 'application/json'},
data={}
)
live_server.start()
res = client.get(get_endpoint())
Note: The notation name.load_data, corresponds to a endpoint='load' attribute, within a route decorator.
The following is a route decorator using the blueprint implementation:
# local variables
blueprint = Blueprint(
'name',
__name__,
template_folder='interface/templates',
static_folder='interface/static'
)
Alternatively, the route function can be referenced directly from the live_server implementation, rather than
implementing an endpoint:
live_server.start()
Note: Remember to explicitly define which methods are supported when registering the above route function.
An important part of any REST (REpresentational State Transfer) service is content negotiation. It allows you to
implement behaviour such as selecting a different serialization schemes for different media types.
HTTP has provisions for several mechanisms for “content negotiation” - the process of selecting the best
representation for a given response when there are multiple representations available.
—RFC 2616#section-12. Fielding, et al.
The most common way to select one of the multiple possible representation is via Accept request header. The
following series of accept_* fixtures provides an easy way to test content negotiation in your application:
2.2.2 Markers
pytest-flask registers the following markers. See the pytest documentation on what markers are and for notes on
using them.
pytest.mark.options(**kwargs)
The mark used to pass options to your application config.
Parameters kwargs (dict) – The dictionary used to extend application config.
Example usage:
@pytest.mark.options(debug=False)
def test_app(app):
assert not app.debug, 'Ensure the app is not in debug mode'
Facilitating the work of potential contributors is recommended since it increases the likelihood of your issue being
solved quickly. The few extra steps listed below will help clarify problems you might be facing:
• Include a minimal reproducible example when possible.
• Describe the expected behaviour and what actually happened including a full trace-back in case of exceptions.
• Make sure to list details about your environment, such as your platform, versions of pytest, pytest-flask and
python release.
Also, it’s important to check the current open issues for similar reports in order to avoid duplicates.
• Add your fork as a remote to push your contributions.Replace {username} with your username.
• Using Tox, create a virtual environment and install pytest-flask in editable mode with development dependen-
cies.
$ tox -e dev
$ source venv/bin/activate
$ pre-commit install
• Create a new branch to identify what feature you are working on.
You can run the test suite for the current environment with
$ pytest
To run the full test suite for all supported python versions
$ tox
Obs. CI will run tox when you submit your pull request, so this is optional.
To get a complete report of code sections not being touched by the test suite run pytest using coverage.
2.4 Changelog
2.4.1 1.2.1
• Speedup live server start time. Use 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.
2.4. Changelog 13
pytest-flask Documentation, Release 1.0.1.dev44+g04c1255
• New --live-server-host command-line option to set the host name used by the live_server fixture.
Thanks @o1da for the PR (#90).
• 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.
• 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).
2.4.13 0.8.1
2.4.14 0.8.0
• New request_ctx fixture which contains all request relevant information (#29).
2.4.15 0.7.5
2.4.16 0.7.4
• Better test coverage, e.g. tests for available fixtures and markers.
2.4.17 0.7.3
2.4.18 0.7.2
2.4.19 0.7.1
2.4.20 0.7.0
2.4.21 0.6.3
2.4. Changelog 15
pytest-flask Documentation, Release 1.0.1.dev44+g04c1255
2.4.22 0.6.2
• Release the random port before starting the application live server (#17), thanks to @davehunt.
2.4.23 0.6.1
• 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.
2.4.24 0.6.0
2.4.25 0.5.0
• Add live_server fixture uses to run application in the background (#11), thanks to @svenstaro.
2.4.26 0.4.0
2.4.27 0.3.4
2.4.28 0.3.3
2.4.29 0.3.2
• Use codecs module to open files to prevent possible errors on open files which contains non-ascii characters.
2.4.30 0.3.1
P
pytest.mark.options() (built-in function), 11
R
RFC
RFC 2616#section-12, 10
17