Mention the TESTING flag in the docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent d62e277 · Browse files · 2 files·+23 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+23 added−2 removed
- other:+23 added−2 removed
Provenance
- Commit
7e55b50- Tree
d0b6eca- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d62e277
2 files changed+23 added−2 removed
modified:docs/testing.rst+22 added-2 removed
39 unmodified lines40 class FlaskrTestCase(unittest.TestCase):41 42 def setUp(self):43 self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()44+ flaskr.app.config['TESTING'] = True45 self.app = flaskr.app.test_client()46 flaskr.init_db()47 48 def tearDown(self):6 unmodified lines55 The code in the :meth:`~unittest.TestCase.setUp` method creates a new test56 client and initializes a new database. This function is called before57 each individual test function is run. To delete the database after the 58 test, we close the file and remove it from the filesystem in the58-:meth:`~unittest.TestCase.tearDown` method. 59+:meth:`~unittest.TestCase.tearDown` method. Additionally during setup the60+``TESTING`` config flag is activated. What it does is disabling the error61+catching during request handling so that you get better error reports when62+performing test requests against the application.63 64 This test client will give us a simple interface to the application. We can 65 trigger test requests to the application, and the client will also keep track 66 of cookies for us.151 unmodified lines218 If you want to test your application with different configurations and219 there does not seem to be a good way to do that, consider switching to220 application factories (see :ref:`app-factories`).221 222+Note however that if you are using a test request context, the223+:meth:`~flask.Flask.before_request` functions are not automatically called224+same fore :meth:`~flask.Flask.after_request` functions. However225+:meth:`~flask.Flask.teardown_request` functions are indeed executed when226+the test request context leaves the `with` block. If you do want the227+:meth:`~flask.Flask.before_request` functions to be called as well, you228+need to call :meth:`~flask.Flask.preprocess_request` yourself::229 230+ app = flask.Flask(__name__)231+232+ with app.test_request_context('/?name=Peter'):233+ app.preprocess_request()234+ ...235+236+This can be necessary to open database connections or something similar237+depending on how your application was designed.238+239+240 Keeping the Context Around241 --------------------------242 243 .. versionadded:: 0.414 unmodified lines258 is no longer available (because you are trying to use it outside of the actual request).259 However, keep in mind that any :meth:`~flask.Flask.after_request` functions260 are already called at this point so your database connection and261 everything involved is probably already closed down.241-modified:examples/flaskr/flaskr_tests.py+1 added
17 unmodified lines18 19 def setUp(self):20 """Before each test, set up a blank database"""21 self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()22+ flaskr.app.config['TESTING'] = True23 self.app = flaskr.app.test_client()24 flaskr.init_db()25 26 def tearDown(self):49 unmodified lines