Search code examples
pythonpyramidpylonsconfigparser

Gettings settings and config from INI file for Pyramid functional testing


In a real Pyramid app it does not work per docs http://docs.pylonsproject.org/projects/pyramid//en/latest/narr/testing.html :

class FunctionalTests(unittest.TestCase):
    def setUp(self):
        from myapp import main
        app = main({})

Exception:

Traceback (most recent call last):
  File "C:\projects\myapp\tests\model\task_dispatcher_integration_test.py", line 35, in setUp
    app = main({})
  File "C:\projects\myapp\myapp\__init__.py", line 207, in main
    engine = engine_from_config(settings, 'sqlalchemy.')
  File "C:\projects\myapp\ve\lib\site-packages\sqlalchemy\engine\__init__.py", line 407, in engine_from_config
    url = options.pop('url')
KeyError: 'url'

The reason is trivial: an empty dictionary is passed to main, while it seems that while running real app (from __init__.py) it gets settings pre-filled with values from [app:main] section of development.ini / production.ini:

settings {'ldap_port': '4032', 'sqlalchemy.url': 'postgresql://.....}

Is there some way of reconstructing settings easily from an .ini file for functional testing?


Solution

  • pyramid.paster.get_appsettings is the only thing you need:

    from pyramid.paster import get_appsettings
    
    settings = get_appsettings('test.ini', name='main')
    app = main(settings)
    

    That test.ini can include all the settings of another .ini file easily like this:

    [app:main]
    use = config:development.ini#main
    

    and then you only need to override those keys that change (I guess you'd want to rather test against a separate DB):

    [app:main]
    use = config:development.ini#main
    sqlalchemy.uri = postgresql://....