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


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

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


9299
2017-09-20 08:56


起源



答案:


我添加了一个 样品申请 到GAEUnit项目,该项目演示了如何使用GAEUnit编写和执行Web测试。该示例包含一个稍微修改过的版本'WebTest的按照David Coffin的建议,'module('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)

11
2017-09-22 12:02



修补 webtest/__init__.py 不再需要,因为webbrowser只是由 webtest.app:showbrowser 函数,如果它被调用。看到 github.com/Pylons/webtest/commit/... 和 github.com/Pylons/webtest/commit/... - Tripp Lilley


答案:


我添加了一个 样品申请 到GAEUnit项目,该项目演示了如何使用GAEUnit编写和执行Web测试。该示例包含一个稍微修改过的版本'WebTest的按照David Coffin的建议,'module('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)

11
2017-09-22 12:02



修补 webtest/__init__.py 不再需要,因为webbrowser只是由 webtest.app:showbrowser 函数,如果它被调用。看到 github.com/Pylons/webtest/commit/... 和 github.com/Pylons/webtest/commit/... - Tripp Lilley


实际上,只要你注释掉,WebTest就可以在沙盒中工作

import webbrowser

在webtest / __ init__.py中


1
2017-09-20 09:44



修补 webtest/__init__.py 不再需要,因为webbrowser只是由 webtest.app:showbrowser 函数,如果它被调用。看到 github.com/Pylons/webtest/commit/... 和 github.com/Pylons/webtest/commit/... - Tripp Lilley