根据ini文件中的设置为金字塔Web应用程序编写doctest [英] Writing doctests for pyramid web app which depend on settings in ini file
问题描述
我想使用 webtest 模块为我的金字塔Web应用程序编写doctest.我这样尝试过:
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('/')
这会在我的代码到达以下行时引发KeyError
(因为some.url
未知):
This raises a KeyError
(because some.url
is not known) when my code reaches this line:
url = request.registry.settings['some.url']
some.url
的值在我的应用程序的粘贴ini文件中指定.运行测试代码时,有没有使用我的development.ini
的简单方法?我尚未完全了解在金字塔启动期间如何/何时加载ini文件,因此很难在测试时确定将文件加载到何处.
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.
推荐答案
main
与ini文件的内容一起调用.从ini加载应用的一种简单方法是:
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)
这希望"testing.ini"位于当前工作目录中,因此您可能需要对其进行调整.如果您希望它相对于树中的某个点,可以使用:
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'))
这篇关于根据ini文件中的设置为金字塔Web应用程序编写doctest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!