Skip to content

Instantly share code, notes, and snippets.

@inklesspen
Last active December 10, 2015 20:28
Show Gist options
  • Select an option

  • Save inklesspen/4488599 to your computer and use it in GitHub Desktop.

Select an option

Save inklesspen/4488599 to your computer and use it in GitHub Desktop.
@pytest.fixture(scope='session', autouse=True)
def set_bcrypt_difficulty():
# The default difficulty is good for regular use, but slows down the tests too much.
Login.set_bcrypt_difficulty(2)
@pytest.fixture(scope='session')
def appsettings(request):
config_uri = os.path.abspath(request.config.option.ini)
setup_logging(config_uri)
settings = get_appsettings(config_uri)
return settings
@pytest.fixture(scope='session', autouse=True)
def sqlengine(request, appsettings):
engine = engine_from_config(appsettings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.create_all(engine)
def teardown():
Base.metadata.drop_all(engine)
request.addfinalizer(teardown)
return engine
def pytest_addoption(parser):
parser.addoption("--ini", action="store", metavar="INI_FILE", help="use INI_FILE to configure SQLAlchemy")
class BaseTest(object):
@pytest.fixture(autouse=True)
def dbtransaction(self, request, sqlengine):
DBSession.remove()
connection = sqlengine.connect()
transaction = connection.begin()
DBSession.configure(bind=connection)
def teardown():
transaction.rollback()
connection.close()
DBSession.remove()
request.addfinalizer(teardown)
def setup_method(self, method):
self._patchers = []
self.config = testing.setUp()
def add_patcher(self, patcher):
self._patchers.append(patcher)
patcher.start()
return patcher
def teardown_method(self, method):
transaction.abort()
testing.tearDown()
for patcher in self._patchers:
patcher.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment