Browse Source

update(flaskr): Add conftest.py for fixtures.

Abstracts out the fixtures into a separate conftest.py file to enable fixture reuse.
pull/2560/head
Jake Robers 7 years ago
parent
commit
856cd18898
  1. 47
      examples/flaskr/tests/conftest.py
  2. 38
      examples/flaskr/tests/test_flaskr.py

47
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

38
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,

Loading…
Cancel
Save