lemur/lemur/tests/conftest.py

76 lines
1.9 KiB
Python
Raw Normal View History

2015-06-25 01:48:40 +02:00
import pytest
from lemur import create_app
from lemur.database import db as _db
from lemur.users import service as user_service
from lemur.roles import service as role_service
2015-06-25 01:48:40 +02:00
def pytest_addoption(parser):
parser.addoption("--runslow", action="store_true", help="run slow tests")
def pytest_runtest_setup(item):
if 'slow' in item.keywords and not item.config.getoption("--runslow"):
pytest.skip("need --runslow option to run")
if "incremental" in item.keywords:
previousfailed = getattr(item.parent, "_previousfailed", None)
if previousfailed is not None:
pytest.xfail("previous test failed ({0})".format(previousfailed.name))
def pytest_runtest_makereport(item, call):
if "incremental" in item.keywords:
if call.excinfo is not None:
parent = item.parent
parent._previousfailed = item
@pytest.yield_fixture(scope="session")
def app():
"""
Creates a new Flask application for a test duration.
Uses application factory `create_app`.
"""
app = create_app()
app.config['TESTING'] = True
2015-06-26 19:31:55 +02:00
app.config['LEMUR_ENCRYPTION_KEY'] = 'test'
2015-06-25 01:48:40 +02:00
ctx = app.app_context()
ctx.push()
yield app
ctx.pop()
@pytest.yield_fixture(scope="session")
def db(app, request):
_db.drop_all()
_db.create_all()
2015-06-25 01:48:40 +02:00
_db.app = app
2015-06-25 01:48:40 +02:00
2015-06-26 19:31:55 +02:00
user = user_service.create('user', 'test', 'user@example.com', True, None, [])
admin_role = role_service.create('admin')
admin = user_service.create('admin', 'admin', 'admin@example.com', True, None, [admin_role])
_db.session.commit()
yield _db
2015-06-25 01:48:40 +02:00
@pytest.yield_fixture(scope="function")
def session(db, request):
2015-06-25 01:48:40 +02:00
"""
Creates a new database session with (with working transaction)
for test duration.
"""
db.session.begin_nested()
yield session
db.session.rollback()
2015-06-25 01:48:40 +02:00
@pytest.yield_fixture(scope="function")
2015-07-06 19:53:12 +02:00
def client(app, session, client):
yield client