Skip to content
Myr

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

2 files+23 added−2 removed

modified:docs/testing.rst+22 added-2 removed
39 unmodified lines
40 class FlaskrTestCase(unittest.TestCase):4142 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()4748 def tearDown(self):
6 unmodified lines
55The code in the :meth:`~unittest.TestCase.setUp` method creates a new test56client and initializes a new database. This function is called before57each individual test function is run. To delete the database after the 58test, 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 error61catching during request handling so that you get better error reports when62performing test requests against the application.6364This test client will give us a simple interface to the application. We can 65trigger test requests to the application, and the client will also keep track 66of cookies for us.
151 unmodified lines
218If you want to test your application with different configurations and219there does not seem to be a good way to do that, consider switching to220application factories (see :ref:`app-factories`).221222Note however that if you are using a test request context, the223:meth:`~flask.Flask.before_request` functions are not automatically called224same fore :meth:`~flask.Flask.after_request` functions. However225:meth:`~flask.Flask.teardown_request` functions are indeed executed when226the test request context leaves the `with` block. If you do want the227:meth:`~flask.Flask.before_request` functions to be called as well, you228need to call :meth:`~flask.Flask.preprocess_request` yourself::229230 app = flask.Flask(__name__)231232 with app.test_request_context('/?name=Peter'):233 app.preprocess_request()234 ...235236This can be necessary to open database connections or something similar237depending on how your application was designed.238239240Keeping the Context Around241--------------------------242243.. versionadded:: 0.4
14 unmodified lines
258is no longer available (because you are trying to use it outside of the actual request).259However, keep in mind that any :meth:`~flask.Flask.after_request` functions260are already called at this point so your database connection and261everything involved is probably already closed down.241
modified:examples/flaskr/flaskr_tests.py+1 added
17 unmodified lines
1819 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()2526 def tearDown(self):
49 unmodified lines