如何在Google App Engine中对来自Webapp WSGI应用程序的响应进行单元测试?

时间:2020-03-06 14:28:50  来源:igfitidea点击:

我想对来自Google App Engine webapp.WSGIApplication的响应进行单元测试,例如,使用GAEUnit请求url'/'并测试响应状态代码为200。我怎样才能做到这一点?

我想使用在App Engine沙箱中运行的webapp框架和GAEUnit(不幸的是,WebTest在沙箱中不起作用)。

解决方案

实际上,只要我们注释掉,WebTest就可以在沙箱中运行

import webbrowser

在webtest / __ init__.py中

我在GAEUnit项目中添加了一个示例应用程序,该应用程序演示了如何使用GAEUnit编写和执行网络测试。该示例包括对" 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)