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

时间:2022-05-27 07:27:56

I'd like to unit test responses from the Google App Engine webapp.WSGIApplication, for example request the url '/' and test that the responses status code is 200, using GAEUnit. How can I do this?

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

I'd like to use the webapp framework and GAEUnit, which runs within the App Engine sandbox (unfortunately WebTest does not work within the sandbox).

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

2 个解决方案

#1


11  

I have added a sample application to the GAEUnit project which demonstrates how to write and execute a web test using GAEUnit. The sample includes a slightly modified version of the 'webtest' module ('import webbrowser' is commented out, as recommended by David Coffin).

我在GAEUnit项目中添加了一个示例应用程序,演示了如何使用GAEUnit编写和执行Web测试。该示例包含“webtest”模块的略微修改版本(“导入webbrowser”已被注释掉,如David Coffin所推荐)。

Here's the 'web_tests.py' file from the sample application 'test' directory:

这是示例应用程序'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


1  

Actually WebTest does work within the sandbox, as long as you comment out

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

import webbrowser

in webtest/__init__.py

#1


11  

I have added a sample application to the GAEUnit project which demonstrates how to write and execute a web test using GAEUnit. The sample includes a slightly modified version of the 'webtest' module ('import webbrowser' is commented out, as recommended by David Coffin).

我在GAEUnit项目中添加了一个示例应用程序,演示了如何使用GAEUnit编写和执行Web测试。该示例包含“webtest”模块的略微修改版本(“导入webbrowser”已被注释掉,如David Coffin所推荐)。

Here's the 'web_tests.py' file from the sample application 'test' directory:

这是示例应用程序'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


1  

Actually WebTest does work within the sandbox, as long as you comment out

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

import webbrowser

in webtest/__init__.py