From 378a11f99275a6b80d365a5d51d469844bf9ca17 Mon Sep 17 00:00:00 2001 From: Neil Grey Date: Mon, 22 May 2017 18:22:08 -0700 Subject: [PATCH] For Issue #2286: Updating test_flaskr to use yield inside fixture --- docs/testing.rst | 15 +++++---------- examples/flaskr/tests/test_flaskr.py | 10 +++------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/docs/testing.rst b/docs/testing.rst index 7a16e336..67b8aaea 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -57,13 +57,9 @@ the application for testing and initializes a new database.:: client = flaskr.app.test_client() with flaskr.app.app_context(): flaskr.init_db() - - def teardown(): - os.close(db_fd) - os.unlink(flaskr.app.config['DATABASE']) - request.addfinalizer(teardown) - - return client + yield client + os.close(db_fd) + os.unlink(flaskr.app.config['DATABASE']) 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 @@ -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. To delete the database after the test, we close the file and remove it -from the filesystem in the -:func:`teardown` function. +from the filesystem. 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 `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. 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 diff --git a/examples/flaskr/tests/test_flaskr.py b/examples/flaskr/tests/test_flaskr.py index 663e92e0..df32cd4b 100644 --- a/examples/flaskr/tests/test_flaskr.py +++ b/examples/flaskr/tests/test_flaskr.py @@ -22,13 +22,9 @@ def client(request): client = flaskr.app.test_client() with flaskr.app.app_context(): flaskr.init_db() - - def teardown(): - os.close(db_fd) - os.unlink(flaskr.app.config['DATABASE']) - request.addfinalizer(teardown) - - return client + yield client + os.close(db_fd) + os.unlink(flaskr.app.config['DATABASE']) def login(client, username, password):