1

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.

4

1 回答 1

3

main使用您的 ini 文件的内容调用。从 ini 加载应用程序的一种简单方法是:

from pyramid.paster import get_app

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

这期望“testing.ini”位于当前工作目录中,因此您可能需要对其进行调整。如果您希望它与树中的某个位置相关,您可以使用:

import os.path
import some_module

here = os.path.dirname(some_module.__file__)
app = get_app(os.path.join(here, 'testing.ini'))
于 2013-05-25T16:55:45.923 回答