Skip to content

Instantly share code, notes, and snippets.

@miohtama
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miohtama/54671b3b1bd43a1f7bcb to your computer and use it in GitHub Desktop.
Save miohtama/54671b3b1bd43a1f7bcb to your computer and use it in GitHub Desktop.
How to past INI configuration file for your py.test based Pyramid unit tests
import os
import pyramid.testing
import pytest
import transaction
from sqlalchemy.orm.session import Session
from pyramid.paster import (
get_appsettings,
setup_logging,
bootstrap)
# TODO: Remove this method
from websauna.system import get_init
from websauna.system.model import Base
@pytest.fixture(scope='session')
def ini_settings(request):
"""Load INI settings from py.test command line.
Example:
py.test yourpackage -s --ini=test.ini
"""
if not getattr(request.config.option, "ini", None):
raise RuntimeError("You need to give --ini test.ini command line option to py.test to find our test settings")
config_uri = os.path.abspath(request.config.option.ini)
setup_logging(config_uri)
config = get_appsettings(config_uri)
# To pass forward in fixtures
config["_ini_file"] = config_uri
return config
@pytest.fixture(scope='session')
def app(request, ini_settings):
"""Initialize WSGI application from INI file given on the command line.
:return: WSGI application instance as created by ``Initializer.make_wsgi_app()``.
"""
if not getattr(request.config.option, "ini", None):
raise RuntimeError("You need to give --ini test.ini command line option to py.test to find our test settings")
data = bootstrap(ini_settings["_ini_file"])
return data["app"]
def pytest_addoption(parser):
parser.addoption("--ini", action="store", metavar="INI_FILE", help="use INI_FILE to configure SQLAlchemy")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment