Browse Source

update(testing_doc): Fixture code sample updated

This now reflects the current codebase of the testing app which
implements the factory pattern.
pull/2560/head
Jake Robers 7 years ago
parent
commit
271be661ed
  1. 41
      docs/testing.rst

41
docs/testing.rst

@ -37,33 +37,48 @@ The Testing Skeleton
--------------------
We begin by adding a tests directory under the application root. Then
create a Python file to store our test configuration (:file:`conftest.py`).
create a Python file to store our test configuration (:file:`conftest.py`). This
is a special file that will be automatically loaded by pytest on runtime.
Next, we create a `pytest fixture`_ called
:func:`client` that configures
:func:`client` in (:file:`conftest.py`), which configures
the application for testing and initializes a new database.::
import os
import tempfile
import pytest
from flaskr.factory import create_app
from flaskr.blueprints.flaskr import init_db
@pytest.fixture
def app(request):
db_fd, temp_db_location = tempfile.mkstemp()
config = {
'DATABASE': temp_db_location,
'TESTING': True,
'DB_FD': db_fd
}
app = create_app(config=config)
from flaskr import flaskr
with app.app_context():
init_db()
yield app
@pytest.fixture
def client():
db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
flaskr.app.config['TESTING'] = True
client = flaskr.app.test_client()
def client(request, app):
with flaskr.app.app_context():
flaskr.init_db()
client = app.test_client()
yield client
def teardown():
os.close(app.config['DB_FD'])
os.unlink(app.config['DATABASE'])
request.addfinalizer(teardown)
os.close(db_fd)
os.unlink(flaskr.app.config['DATABASE'])
return client
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

Loading…
Cancel
Save