Browse Source

Update testing.rst

pull/2307/head
David Lord 8 years ago committed by GitHub
parent
commit
8858135043
  1. 35
      docs/testing.rst

35
docs/testing.rst

@ -47,17 +47,23 @@ the application for testing and initializes a new database.::
import os import os
import tempfile import tempfile
import pytest import pytest
from flaskr import flaskr from flaskr import flaskr
@pytest.fixture @pytest.fixture
def client(request): def client(request):
db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp() db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
flaskr.app.config['TESTING'] = True flaskr.app.config['TESTING'] = True
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 yield client
os.close(db_fd) os.close(db_fd)
os.unlink(flaskr.app.config['DATABASE']) os.unlink(flaskr.app.config['DATABASE'])
@ -77,8 +83,8 @@ low-level file handle and a random file name, the latter we use as
database name. We just have to keep the `db_fd` around so that we can use 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, the fixture closes the file and removes
from the filesystem. it from the filesystem.
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::
@ -107,6 +113,7 @@ test function to :file:`test_flaskr.py`, like this::
def test_empty_db(client): def test_empty_db(client):
"""Start with a blank database.""" """Start with a blank database."""
rv = client.get('/') rv = client.get('/')
assert b'No entries here so far' in rv.data assert b'No entries here so far' in rv.data
@ -148,6 +155,7 @@ Add the following two functions to your :file:`test_flaskr.py` file::
password=password password=password
), follow_redirects=True) ), follow_redirects=True)
def logout(client): def logout(client):
return client.get('/logout', follow_redirects=True) return client.get('/logout', follow_redirects=True)
@ -155,17 +163,18 @@ Now we can easily test that logging in and out works and that it fails with
invalid credentials. Add this new test function:: invalid credentials. Add this new test function::
def test_login_logout(client): def test_login_logout(client):
"""Make sure login and logout works""" """Make sure login and logout works."""
rv = login(client, flaskr.app.config['USERNAME'],
flaskr.app.config['PASSWORD']) rv = login(client, flaskr.app.config['USERNAME'], flaskr.app.config['PASSWORD'])
assert b'You were logged in' in rv.data assert b'You were logged in' in rv.data
rv = logout(client) rv = logout(client)
assert b'You were logged out' in rv.data assert b'You were logged out' in rv.data
rv = login(client, flaskr.app.config['USERNAME'] + 'x',
flaskr.app.config['PASSWORD']) rv = login(client, flaskr.app.config['USERNAME'] + 'x', flaskr.app.config['PASSWORD'])
assert b'Invalid username' in rv.data assert b'Invalid username' in rv.data
rv = login(client, flaskr.app.config['USERNAME'],
flaskr.app.config['PASSWORD'] + 'x') rv = login(client, flaskr.app.config['USERNAME'], flaskr.app.config['PASSWORD'] + 'x')
assert b'Invalid password' in rv.data assert b'Invalid password' in rv.data
Test Adding Messages Test Adding Messages
@ -175,9 +184,9 @@ We should also test that adding messages works. Add a new test function
like this:: like this::
def test_messages(client): def test_messages(client):
"""Test that messages work""" """Test that messages work."""
login(client, flaskr.app.config['USERNAME'],
flaskr.app.config['PASSWORD']) login(client, flaskr.app.config['USERNAME'], flaskr.app.config['PASSWORD'])
rv = client.post('/add', data=dict( rv = client.post('/add', data=dict(
title='<Hello>', title='<Hello>',
text='<strong>HTML</strong> allowed here' text='<strong>HTML</strong> allowed here'
@ -207,11 +216,9 @@ For more complex tests with headers and status codes, check out the
`MiniTwit Example`_ from the sources which contains a larger test `MiniTwit Example`_ from the sources which contains a larger test
suite. suite.
.. _MiniTwit Example: .. _MiniTwit Example:
https://github.com/pallets/flask/tree/master/examples/minitwit/ https://github.com/pallets/flask/tree/master/examples/minitwit/
Other Testing Tricks Other Testing Tricks
-------------------- --------------------

Loading…
Cancel
Save