我想对来自 Google App Engine webapp.WSGIApplication 的响应进行单元测试,例如请求 url '/' 并测试响应状态代码是否为 200,使用 GAEUnit .我怎样才能做到这一点?
我想使用在 App Engine 沙箱中运行的 webapp 框架和 GAEUnit(不幸的是 WebTest 在沙箱中不起作用)。
最佳答案
我添加了一个 sample application到 GAEUnit 项目,该项目演示了如何使用 GAEUnit 编写和执行 Web 测试。该示例包括对“webtest”稍作修改的版本。 ' 模块('import webbrowser' 被注释掉,正如 David Coffin 所推荐的那样)。
这是示例应用程序“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)
关于python - 如何在 Google App Engine 中对来自 webapp WSGI 应用程序的响应进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/107675/