Skip to content
Myr

testing.rst

This file

Path

docs/testing.rst

319 lines · 10.2 KiB

Last changed by

Merge branch '2.1.x'David Lordbefore adoptioncb4f742

History of this file

Take it

The walk stopped after 200 commits: lines older than 8952a17 point at it.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 1–3why, one step back
1Testing Flask Applications
2==========================3

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 4–6why, one step back
4Flask provides utilities for testing an application. This documentation
5goes over techniques for working with different parts of the application6in tests.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 7why, one step back
7

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 8why, one step back
8We will use the `pytest`_ framework to set up and run our tests.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 9why, one step back
9

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 10why, one step back
10.. code-block:: text

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 11–15why, one step back
11
12 $ pip install pytest1314.. _pytest: https://docs.pytest.org/15

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 16–19why, one step back
16The :doc:`tutorial </tutorial/index>` goes over how to write tests for
17100% coverage of the sample Flaskr blog application. See18:doc:`the tutorial on tests </tutorial/tests>` for a detailed19explanation of specific tests for an application.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 20–21why, one step back
20
21

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 22–23why, one step back
22Identifying Tests
23-----------------

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 24why, one step back
24

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 25–27why, one step back
25Tests are typically located in the ``tests`` folder. Tests are functions
26that start with ``test_``, in Python modules that start with ``test_``.27Tests can also be further grouped in classes that start with ``Test``.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 28why, one step back
28

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 29–32why, one step back
29It can be difficult to know what to test. Generally, try to test the
30code that you write, not the code of libraries that you use, since they31are already tested. Try to extract complex behaviors as separate32functions to test individually.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 33–34why, one step back
33
34

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 35–36why, one step back
35Fixtures
36--------

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 37why, one step back
37

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 38–42why, one step back
38Pytest *fixtures* allow writing pieces of code that are reusable across
39tests. A simple fixture returns a value, but a fixture can also do40setup, yield a value, then do teardown. Fixtures for the application,41test client, and CLI runner are shown below, they can be placed in42``tests/conftest.py``.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 43why, one step back
43

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 44–48why, one step back
44If you're using an
45:doc:`application factory </patterns/appfactories>`, define an ``app``46fixture to create and configure an app instance. You can add code before47and after the ``yield`` to set up and tear down other resources, such as48creating and clearing a database.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 49why, one step back
49

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 50–52why, one step back
50If you're not using a factory, you already have an app object you can
51import and configure directly. You can still use an ``app`` fixture to52set up and tear down resources.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 53why, one step back
53

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 54why, one step back
54.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 55why, one step back
55

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 56–57why, one step back
56 import pytest
57 from my_project import create_app

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 58why, one step back
58

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 59–64why, one step back
59 @pytest.fixture()
60 def app():61 app = create_app()62 app.config.update({63 "TESTING": True,64 })

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 65why, one step back
65

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 66why, one step back
66 # other setup can go here

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 67why, one step back
67

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 68why, one step back
68 yield app

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 69why, one step back
69

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 70why, one step back
70 # clean up / reset resources here

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 71–72why, one step back
71
72

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 73–75why, one step back
73 @pytest.fixture()
74 def client(app):75 return app.test_client()

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 76–77why, one step back
76
77

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 78–80why, one step back
78 @pytest.fixture()
79 def runner(app):80 return app.test_cli_runner()

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 81–82why, one step back
81
82

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 83–84why, one step back
83Sending Requests with the Test Client
84-------------------------------------

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 85why, one step back
85

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 86–89why, one step back
86The test client makes requests to the application without running a live
87server. Flask's client extends88:doc:`Werkzeug's client <werkzeug:test>`, see those docs for additional89information.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 90why, one step back
90

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 91–95why, one step back
91The ``client`` has methods that match the common HTTP request methods,
92such as ``client.get()`` and ``client.post()``. They take many arguments93for building the request; you can find the full documentation in94:class:`~werkzeug.test.EnvironBuilder`. Typically you'll use ``path``,95``query``, ``headers``, and ``data`` or ``json``.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 96why, one step back
96

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 97–102why, one step back
97To make a request, call the method the request should use with the path
98to the route to test. A :class:`~werkzeug.test.TestResponse` is returned99to examine the response data. It has all the usual properties of a100response object. You'll usually look at ``response.data``, which is the101bytes returned by the view. If you want to use text, Werkzeug 2.1102provides ``response.text``, or use ``response.get_data(as_text=True)``.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 103why, one step back
103

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 104why, one step back
104.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 105why, one step back
105

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 106–108why, one step back
106 def test_request_example(client):
107 response = client.get("/posts")108 assert b"<h2>Hello, World!</h2>" in response.data

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 109–110why, one step back
109
110

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 111–113why, one step back
111Pass a dict ``query={"key": "value", ...}`` to set arguments in the
112query string (after the ``?`` in the URL). Pass a dict ``headers={}``113to set request headers.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 114why, one step back
114

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 115–117why, one step back
115To send a request body in a POST or PUT request, pass a value to
116``data``. If raw bytes are passed, that exact body is used. Usually,117you'll pass a dict to set form data.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 118–119why, one step back
118
119

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 120–121why, one step back
120Form Data
121~~~~~~~~~

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 122why, one step back
122

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 123–125why, one step back
123To send form data, pass a dict to ``data``. The ``Content-Type`` header
124will be set to ``multipart/form-data`` or125``application/x-www-form-urlencoded`` automatically.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 126why, one step back
126

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 127–131why, one step back
127If a value is a file object opened for reading bytes (``"rb"`` mode), it
128will be treated as an uploaded file. To change the detected filename and129content type, pass a ``(file, filename, content_type)`` tuple. File130objects will be closed after making the request, so they do not need to131use the usual ``with open() as f:`` pattern.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 132why, one step back
132

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 133–134why, one step back
133It can be useful to store files in a ``tests/resources`` folder, then
134use ``pathlib.Path`` to get files relative to the current test file.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 135why, one step back
135

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 136why, one step back
136.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 137why, one step back
137

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 138why, one step back
138 from pathlib import Path

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 139why, one step back
139

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 140–141why, one step back
140 # get the resources folder in the tests folder
141 resources = Path(__file__).parent / "resources"

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 142why, one step back
142

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 143–144why, one step back
143 def test_edit_user(client):
144 response = client.post("/user/2/edit", data={

Merge branch '2.1.x'David Lordbefore adoptioncb4f742

lines 145–147why, one step back
145 "name": "Flask",
146 "theme": "dark",147 "picture": (resources / "picture.png").open("rb"),

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 148–149why, one step back
148 })
149 assert response.status_code == 200

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 150–151why, one step back
150
151

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 152–153why, one step back
152JSON Data
153~~~~~~~~~

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 154why, one step back
154

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 155–156why, one step back
155To send JSON data, pass an object to ``json``. The ``Content-Type``
156header will be set to ``application/json`` automatically.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 157why, one step back
157

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 158–159why, one step back
158Similarly, if the response contains JSON data, the ``response.json``
159attribute will contain the deserialized object.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 160why, one step back
160

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 161why, one step back
161.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 162why, one step back
162

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 163–176why, one step back
163 def test_json_data(client):
164 response = client.post("/graphql", json={165 "query": """166 query User($id: String!) {167 user(id: $id) {168 name169 theme170 picture_url171 }172 }173 """,174 variables={"id": 2},175 })176 assert response.json["data"]["user"]["name"] == "Flask"

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 177–178why, one step back
177
178

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 179–180why, one step back
179Following Redirects
180-------------------

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 181why, one step back
181

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 182–185why, one step back
182By default, the client does not make additional requests if the response
183is a redirect. By passing ``follow_redirects=True`` to a request method,184the client will continue to make requests until a non-redirect response185is returned.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 186why, one step back
186

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 187–190why, one step back
187:attr:`TestResponse.history <werkzeug.test.TestResponse.history>` is
188a tuple of the responses that led up to the final response. Each189response has a :attr:`~werkzeug.test.TestResponse.request` attribute190which records the request that produced that response.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 191why, one step back
191

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 192why, one step back
192.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 193why, one step back
193

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 194–199why, one step back
194 def test_logout_redirect(client):
195 response = client.get("/logout")196 # Check that there was one redirect response.197 assert len(response.history) == 1198 # Check that the second request was to the index page.199 assert response.request.path == "/index"

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 200–201why, one step back
200
201

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 202–203why, one step back
202Accessing and Modifying the Session
203-----------------------------------

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 204why, one step back
204

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 205–208why, one step back
205To access Flask's context variables, mainly
206:data:`~flask.session`, use the client in a ``with`` statement.207The app and request context will remain active *after* making a request,208until the ``with`` block ends.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 209why, one step back
209

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 210why, one step back
210.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 211why, one step back
211

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 212why, one step back
212 from flask import session

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 213why, one step back
213

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 214–218why, one step back
214 def test_access_session(client):
215 with client:216 client.post("/auth/login", data={"username": "flask"})217 # session is still accessible218 assert session["user_id"] == 1

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 219why, one step back
219

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 220why, one step back
220 # session is no longer accessible

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 221why, one step back
221

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 222–226why, one step back
222If you want to access or set a value in the session *before* making a
223request, use the client's224:meth:`~flask.testing.FlaskClient.session_transaction` method in a225``with`` statement. It returns a session object, and will save the226session once the block ends.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 227why, one step back
227

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 228why, one step back
228.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 229why, one step back
229

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 230why, one step back
230 from flask import session

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 231why, one step back
231

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 232–235why, one step back
232 def test_modify_session(client):
233 with client.session_transaction() as session:234 # set a user id without going through the login route235 session["user_id"] = 1

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 236why, one step back
236

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 237why, one step back
237 # session is saved now

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 238why, one step back
238

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 239–240why, one step back
239 response = client.get("/users/me")
240 assert response.json["username"] == "flask"

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 241–242why, one step back
241
242

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 243why, one step back
243.. _testing-cli:

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 244why, one step back
244

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 245–246why, one step back
245Running Commands with the CLI Runner
246------------------------------------

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 247why, one step back
247

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 248–252why, one step back
248Flask provides :meth:`~flask.Flask.test_cli_runner` to create a
249:class:`~flask.testing.FlaskCliRunner`, which runs CLI commands in250isolation and captures the output in a :class:`~click.testing.Result`251object. Flask's runner extends :doc:`Click's runner <click:testing>`,252see those docs for additional information.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 253why, one step back
253

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 254–256why, one step back
254Use the runner's :meth:`~flask.testing.FlaskCliRunner.invoke` method to
255call commands in the same way they would be called with the ``flask``256command from the command line.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 257why, one step back
257

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 258why, one step back
258.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 259why, one step back
259

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 260why, one step back
260 import click

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 261why, one step back
261

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 262–265why, one step back
262 @app.cli.command("hello")
263 @click.option("--name", default="World")264 def hello_command(name):265 click.echo(f"Hello, {name}!")

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 266why, one step back
266

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 267why, one step back
267 def test_hello_command(runner):

Merge branch '2.1.x'David Lordbefore adoptiona03719b

line 268why, one step back
268 result = runner.invoke(args="hello")

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 269why, one step back
269 assert "World" in result.output

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 270why, one step back
270

Merge branch '2.1.x'David Lordbefore adoptiona03719b

line 271why, one step back
271 result = runner.invoke(args=["hello", "--name", "Flask"])

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 272why, one step back
272 assert "Flask" in result.output

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

lines 273–274why, one step back
273
274

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 275–276why, one step back
275Tests that depend on an Active Context
276--------------------------------------

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 277why, one step back
277

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 278–283why, one step back
278You may have functions that are called from views or commands, that
279expect an active :doc:`application context </appcontext>` or280:doc:`request context </reqcontext>` because they access ``request``,281``session``, or ``current_app``. Rather than testing them by making a282request or invoking the command, you can create and activate a context283directly.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 284why, one step back
284

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 285–287why, one step back
285Use ``with app.app_context()`` to push an application context. For
286example, database extensions usually require an active app context to287make queries.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 288why, one step back
288

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 289why, one step back
289.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 290why, one step back
290

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 291–293why, one step back
291 def test_db_post_model(app):
292 with app.app_context():293 post = db.session.query(Post).get(1)

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 294why, one step back
294

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 295–296why, one step back
295Use ``with app.test_request_context()`` to push a request context. It
296takes the same arguments as the test client's request methods.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 297why, one step back
297

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 298why, one step back
298.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 299why, one step back
299

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 300–305why, one step back
300 def test_validate_user_edit(app):
301 with app.test_request_context(302 "/user/2/edit", method="POST", data={"name": ""}303 ):304 # call a function that accesses `request`305 messages = validate_edit_user()

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 306why, one step back
306

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 307why, one step back
307 assert messages["name"][0] == "Name cannot be empty."

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 308why, one step back
308

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 309–312why, one step back
309Creating a test request context doesn't run any of the Flask dispatching
310code, so ``before_request`` functions are not called. If you need to311call these, usually it's better to make a full request instead. However,312it's possible to call them manually.

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 313why, one step back
313

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

line 314why, one step back
314.. code-block:: python

Merge pull request #3739 from WolframAlph/typo-fixJoshua Bronsonbefore adoption8952a17

line 315why, one step back
315

Merge branch '2.0.x'David Lordbefore adoptionb6a8ccd

lines 316–319why, one step back
316 def test_auth_token(app):
317 with app.test_request_context("/user/2/edit", headers={"X-Auth-Token": "1"}):318 app.preprocess_request()319 assert g.user.name == "Flask"