【问题标题】:How can I unit test responses from the webapp WSGI application in Google App Engine?如何在 Google App Engine 中对来自 webapp WSGI 应用程序的响应进行单元测试?
【发布时间】:2010-09-11 13:59:10
【问题描述】:
我想对来自 Google App Engine webapp.WSGIApplication 的响应进行单元测试,例如请求 url '/' 并使用 GAEUnit 测试响应状态代码是否为 200。我怎样才能做到这一点?
我想使用在 App Engine 沙箱中运行的 webapp 框架和 GAEUnit(很遗憾,WebTest 在沙箱中不起作用)。
【问题讨论】:
标签:
python
unit-testing
google-app-engine
【解决方案1】:
我在 GAEUnit 项目中添加了一个sample application,它演示了如何使用 GAEUnit 编写和执行 Web 测试。该示例包含“webtest”模块的略微修改版本(根据 David Coffin 的建议,“import webbrowser”已被注释掉)。
这是示例应用程序“test”目录中的“web_tests.py”文件:
import unittest
from webtest import TestApp
from google.appengine.ext import webapp
import index
class IndexTest(unittest.TestCase):
def setUp(self):
self.application = webapp.WSGIApplication([('/', index.IndexHandler)], debug=True)
def test_default_page(self):
app = TestApp(self.application)
response = app.get('/')
self.assertEqual('200 OK', response.status)
self.assertTrue('Hello, World!' in response)
def test_page_with_param(self):
app = TestApp(self.application)
response = app.get('/?name=Bob')
self.assertEqual('200 OK', response.status)
self.assertTrue('Hello, Bob!' in response)
【解决方案2】:
其实 WebTest 确实可以在沙箱中工作,只要你注释掉
import webbrowser
在 webtest/__init__.py 中