Aar*_*ron 10 google-app-engine unit-testing webapp2
我正在尝试使用webapp2对处理程序进行单元测试,并且遇到了一个只是一个愚蠢的小错误.
我希望能够在测试中使用webapp2.uri_for,但我似乎无法做到这一点:
def test_returns_200_on_home_page(self):
response = main.app.get_response(webapp2.uri_for('index'))
self.assertEqual(200, response.status_int)
Run Code Online (Sandbox Code Playgroud)
如果我这样做main.app.get_response('/')
就行了.
收到的例外是:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 318, in run
testMethod()
File "tests.py", line 27, in test_returns_200_on_home_page
webapp2.uri_for('index')
File "/Users/.../webapp2_example/lib/webapp2.py", line 1671, in uri_for
return request.app.router.build(request, _name, args, kwargs)
File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 173, in __getattr__
return getattr(self._get_current_object(), name)
File "/Users/.../webapp2_example/lib/webapp2_extras/local.py", line 136, in _get_current_object
raise RuntimeError('no object bound to %s' % self.__name__)
RuntimeError: no object bound to request
Run Code Online (Sandbox Code Playgroud)
我有什么愚蠢的设置吗?
mor*_*aes 14
我认为唯一的选择是设置一个虚拟请求,只是为了能够为测试创建URI:
def test_returns_200_on_home_page(self):
// Set a dummy request just to be able to use uri_for().
req = webapp2.Request.blank('/')
req.app = main.app
main.app.set_globals(app=main.app, request=req)
response = main.app.get_response(webapp2.uri_for('index'))
self.assertEqual(200, response.status_int)
Run Code Online (Sandbox Code Playgroud)
切勿set_globals()
在测试之外使用.是由WSGI应用程序调用以线程安全的方式设置活动应用程序和请求.
归档时间: |
|
查看次数: |
1706 次 |
最近记录: |