diff --git a/examples/flaskr/tests/conftest.py b/examples/flaskr/tests/conftest.py new file mode 100644 index 00000000..3b4889e5 --- /dev/null +++ b/examples/flaskr/tests/conftest.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +""" + Flaskr conftest + ~~~~~~~~~~~~ + + Defines fixtures for the Flaskr test suite. + + :copyright: (c) 2015 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" +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) + + with app.app_context(): + init_db() + yield app + + +@pytest.fixture +def client(request, app): + + client = app.test_client() + + def teardown(): + os.close(app.config['DB_FD']) + os.unlink(app.config['DATABASE']) + request.addfinalizer(teardown) + + return client + + diff --git a/examples/flaskr/tests/test_flaskr.py b/examples/flaskr/tests/test_flaskr.py index b5ade2ec..9bd420d5 100644 --- a/examples/flaskr/tests/test_flaskr.py +++ b/examples/flaskr/tests/test_flaskr.py @@ -8,44 +8,6 @@ :copyright: (c) 2015 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ - -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) - - with app.app_context(): - init_db() - yield app - - -@pytest.fixture -def client(request, app): - - client = app.test_client() - - def teardown(): - os.close(app.config['DB_FD']) - os.unlink(app.config['DATABASE']) - request.addfinalizer(teardown) - - return client - - def login(client, username, password): return client.post('/login', data=dict( username=username,