Update testing.rst
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by GitHub · parent 378a11f · Browse files · 1 file·+21 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+21 added−14 removed
- other:+21 added−14 removed
Provenance
- Commit
8858135- Tree
edf1e7c- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
378a11f
1 file changed+21 added−14 removed
modified:docs/testing.rst+21 added-14 removed
45 unmodified lines46 the application for testing and initializes a new database.::47 48 import os49 import tempfile50+51 import pytest52+53 from flaskr import flaskr54 55+56 @pytest.fixture57 def client(request):58 db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()59 flaskr.app.config['TESTING'] = True60 client = flaskr.app.test_client()61+62 with flaskr.app.app_context():63 flaskr.init_db()64+65 yield client66+67 os.close(db_fd)68 os.unlink(flaskr.app.config['DATABASE'])69 70 This client fixture will be called by each individual test. It gives us a11 unmodified lines82 low-level file handle and a random file name, the latter we use as83 database name. We just have to keep the `db_fd` around so that we can use84 the :func:`os.close` function to close the file.85 80-To delete the database after the test, we close the file and remove it81-from the filesystem.86+To delete the database after the test, the fixture closes the file and removes87+it from the filesystem.88 89 If we now run the test suite, we should see the following output::90 91 $ pytest20 unmodified lines112 test function to :file:`test_flaskr.py`, like this::113 114 def test_empty_db(client):115 """Start with a blank database."""116+117 rv = client.get('/')118 assert b'No entries here so far' in rv.data119 120 Notice that our test functions begin with the word `test`; this allows33 unmodified lines154 username=username,155 password=password156 ), follow_redirects=True)157 158+159 def logout(client):160 return client.get('/logout', follow_redirects=True)161 162 Now we can easily test that logging in and out works and that it fails with163 invalid credentials. Add this new test function::164 165 def test_login_logout(client):158- """Make sure login and logout works"""159- rv = login(client, flaskr.app.config['USERNAME'],160- flaskr.app.config['PASSWORD'])166+ """Make sure login and logout works."""167+168+ rv = login(client, flaskr.app.config['USERNAME'], flaskr.app.config['PASSWORD'])169 assert b'You were logged in' in rv.data170+171 rv = logout(client)172 assert b'You were logged out' in rv.data164- rv = login(client, flaskr.app.config['USERNAME'] + 'x',165- flaskr.app.config['PASSWORD'])173+174+ rv = login(client, flaskr.app.config['USERNAME'] + 'x', flaskr.app.config['PASSWORD'])175 assert b'Invalid username' in rv.data167- rv = login(client, flaskr.app.config['USERNAME'],168- flaskr.app.config['PASSWORD'] + 'x')176+177+ rv = login(client, flaskr.app.config['USERNAME'], flaskr.app.config['PASSWORD'] + 'x')178 assert b'Invalid password' in rv.data179 180 Test Adding Messages181 --------------------1 unmodified line183 We should also test that adding messages works. Add a new test function184 like this::185 186 def test_messages(client):178- """Test that messages work"""179- login(client, flaskr.app.config['USERNAME'],180- flaskr.app.config['PASSWORD'])187+ """Test that messages work."""188+189+ login(client, flaskr.app.config['USERNAME'], flaskr.app.config['PASSWORD'])190 rv = client.post('/add', data=dict(191 title='<Hello>',192 text='<strong>HTML</strong> allowed here'193 ), follow_redirects=True)21 unmodified lines215 For more complex tests with headers and status codes, check out the216 `MiniTwit Example`_ from the sources which contains a larger test217 suite.218 210-219 .. _MiniTwit Example:220 https://github.com/pallets/flask/tree/master/examples/minitwit/221 214-222 Other Testing Tricks223 --------------------224 225 Besides using the test client as shown above, there is also the152 unmodified lines