Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Neil Grey · committed by Neil Grey · parent 4e9d51b · Browse files · 2 files+8 added−17 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+8 added−17 removed

  • tests:+3 added−7 removed
  • other:+5 added−10 removed

2 files+8 added−17 removed

modified:docs/testing.rst+5 added-10 removed
55 unmodified lines
56 flaskr.app.config['TESTING'] = True57 client = flaskr.app.test_client()58 with flaskr.app.app_context():59 flaskr.init_db()60 yield client61 os.close(db_fd)62 os.unlink(flaskr.app.config['DATABASE'])6361 def teardown():62 os.close(db_fd)63 os.unlink(flaskr.app.config['DATABASE'])64 request.addfinalizer(teardown)6566 return client6764This client fixture will be called by each individual test. It gives us a65simple interface to the application, where we can trigger test requests to the66application. The client will also keep track of cookies for us.67
9 unmodified lines
77database name. We just have to keep the `db_fd` around so that we can use78the :func:`os.close` function to close the file.7980To delete the database after the test, we close the file and remove it85from the filesystem in the86:func:`teardown` function.81from the filesystem.8283If we now run the test suite, we should see the following output::8485 $ pytest
26 unmodified lines
112113Notice that our test functions begin with the word `test`; this allows114`pytest`_ to automatically identify the function as a test to run.115121By using `client.get` we can send an HTTP ``GET`` request to the application with116By using ``client.get`` we can send an HTTP ``GET`` request to the application with117the given path. The return value will be a :class:`~flask.Flask.response_class` object.118We can now use the :attr:`~werkzeug.wrappers.BaseResponse.data` attribute to inspect119the return value (as string) from the application. In this case, we ensure that120``'No entries here so far'`` is part of the output.
250 unmodified lines
modified:examples/flaskr/tests/test_flaskr.py+3 added-7 removed
20 unmodified lines
21 flaskr.app.config['TESTING'] = True22 client = flaskr.app.test_client()23 with flaskr.app.app_context():24 flaskr.init_db()25 yield client26 os.close(db_fd)27 os.unlink(flaskr.app.config['DATABASE'])2826 def teardown():27 os.close(db_fd)28 os.unlink(flaskr.app.config['DATABASE'])29 request.addfinalizer(teardown)2931 return client323330def login(client, username, password):31 return client.post('/login', data=dict(32 username=username,33 password=password
39 unmodified lines