Skip to content
Myr

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

1 file+21 added−14 removed

modified:docs/testing.rst+21 added-14 removed
45 unmodified lines
46the application for testing and initializes a new database.::4748 import os49 import tempfile5051 import pytest5253 from flaskr import flaskr545556 @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()6162 with flaskr.app.app_context():63 flaskr.init_db()6465 yield client6667 os.close(db_fd)68 os.unlink(flaskr.app.config['DATABASE'])6970This client fixture will be called by each individual test. It gives us a
11 unmodified lines
82low-level file handle and a random file name, the latter we use as83database name. We just have to keep the `db_fd` around so that we can use84the :func:`os.close` function to close the file.8580To delete the database after the test, we close the file and remove it81from the filesystem.86To delete the database after the test, the fixture closes the file and removes87it from the filesystem.8889If we now run the test suite, we should see the following output::9091 $ pytest
20 unmodified lines
112test function to :file:`test_flaskr.py`, like this::113114 def test_empty_db(client):115 """Start with a blank database."""116117 rv = client.get('/')118 assert b'No entries here so far' in rv.data119120Notice that our test functions begin with the word `test`; this allows
33 unmodified lines
154 username=username,155 password=password156 ), follow_redirects=True)157158159 def logout(client):160 return client.get('/logout', follow_redirects=True)161162Now we can easily test that logging in and out works and that it fails with163invalid credentials. Add this new test function::164165 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."""167168 rv = login(client, flaskr.app.config['USERNAME'], flaskr.app.config['PASSWORD'])169 assert b'You were logged in' in rv.data170171 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'])173174 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')176177 rv = login(client, flaskr.app.config['USERNAME'], flaskr.app.config['PASSWORD'] + 'x')178 assert b'Invalid password' in rv.data179180Test Adding Messages181--------------------
1 unmodified line
183We should also test that adding messages works. Add a new test function184like this::185186 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."""188189 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 lines
215For more complex tests with headers and status codes, check out the216`MiniTwit Example`_ from the sources which contains a larger test217suite.218210219.. _MiniTwit Example:220 https://github.com/pallets/flask/tree/master/examples/minitwit/221214222Other Testing Tricks223--------------------224225Besides using the test client as shown above, there is also the
152 unmodified lines