Writing doctests for pyramid web app which depend on settings in ini file

走远了吗. 提交于 2019-12-13 02:08:44

问题


I would like to write doctests for my pyramid web app, using the webtest module. I tried it like this:

from my_webapp import main
from webtest import TestApp

app = TestApp(main({}))
result = app.get('/')

This raises a KeyError (because some.url is not known) when my code reaches this line:

url = request.registry.settings['some.url']

The value of some.url is specified in the paster ini file of my application. Is there a simple way to use my development.ini when running my test code? I did not yet fully understand how/when the ini file is loaded during pyramid start up, so it's hard to figure out where to load it while testing.


回答1:


main is invoked with the contents of your ini file. A simple way to load your app from an ini is:

from pyramid.paster import get_app

app = get_app('testing.ini#main')
test_app = TestApp(app)

This expects "testing.ini" to be in the current working directory, so you may need to tweak that. If you'd like it to be relative to a spot in your tree you can use:

import os.path
import some_module

here = os.path.dirname(some_module.__file__)
app = get_app(os.path.join(here, 'testing.ini'))


来源:https://stackoverflow.com/questions/16750753/writing-doctests-for-pyramid-web-app-which-depend-on-settings-in-ini-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!