Browse Source

For Issue #2286: Updating test_flaskr to use yield inside fixture

pull/2307/head
Neil Grey 8 years ago
parent
commit
378a11f992
  1. 11
      docs/testing.rst
  2. 6
      examples/flaskr/tests/test_flaskr.py

11
docs/testing.rst

@ -57,13 +57,9 @@ the application for testing and initializes a new database.::
client = flaskr.app.test_client() client = flaskr.app.test_client()
with flaskr.app.app_context(): with flaskr.app.app_context():
flaskr.init_db() flaskr.init_db()
yield client
def teardown():
os.close(db_fd) os.close(db_fd)
os.unlink(flaskr.app.config['DATABASE']) os.unlink(flaskr.app.config['DATABASE'])
request.addfinalizer(teardown)
return client
This client fixture will be called by each individual test. It gives us a This client fixture will be called by each individual test. It gives us a
simple interface to the application, where we can trigger test requests to the simple interface to the application, where we can trigger test requests to the
@ -82,8 +78,7 @@ database name. We just have to keep the `db_fd` around so that we can use
the :func:`os.close` function to close the file. the :func:`os.close` function to close the file.
To delete the database after the test, we close the file and remove it To delete the database after the test, we close the file and remove it
from the filesystem in the from the filesystem.
:func:`teardown` function.
If we now run the test suite, we should see the following output:: If we now run the test suite, we should see the following output::
@ -118,7 +113,7 @@ test function to :file:`test_flaskr.py`, like this::
Notice that our test functions begin with the word `test`; this allows Notice that our test functions begin with the word `test`; this allows
`pytest`_ to automatically identify the function as a test to run. `pytest`_ to automatically identify the function as a test to run.
By using `client.get` we can send an HTTP ``GET`` request to the application with By using ``client.get`` we can send an HTTP ``GET`` request to the application with
the given path. The return value will be a :class:`~flask.Flask.response_class` object. the given path. The return value will be a :class:`~flask.Flask.response_class` object.
We can now use the :attr:`~werkzeug.wrappers.BaseResponse.data` attribute to inspect We can now use the :attr:`~werkzeug.wrappers.BaseResponse.data` attribute to inspect
the return value (as string) from the application. In this case, we ensure that the return value (as string) from the application. In this case, we ensure that

6
examples/flaskr/tests/test_flaskr.py

@ -22,13 +22,9 @@ def client(request):
client = flaskr.app.test_client() client = flaskr.app.test_client()
with flaskr.app.app_context(): with flaskr.app.app_context():
flaskr.init_db() flaskr.init_db()
yield client
def teardown():
os.close(db_fd) os.close(db_fd)
os.unlink(flaskr.app.config['DATABASE']) os.unlink(flaskr.app.config['DATABASE'])
request.addfinalizer(teardown)
return client
def login(client, username, password): def login(client, username, password):

Loading…
Cancel
Save