如何在Google App Engine中对来自Web应用程序WSGI应用程序的响应进行单元测试? [英] How can I unit test responses from the webapp WSGI application in Google App Engine?

查看:98
本文介绍了如何在Google App Engine中对来自Web应用程序WSGI应用程序的响应进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想使用webapp框架和GAEUnit,它运行在App Engine沙箱中(不幸的是 WebTest 在沙箱中不起作用)。 向GAEUnit项目示例应用程序,演示如何使用GAEUnit编写并执行Web测试。该示例包含一个稍微修改过的' webtest '模块('import webbrowser'已被注释掉,由David Coffin推荐)。

以下是示例应用程序test目录中的'web_tests.py'文件:

  import unittest $ b $ from webtest import TestApp 
from google.appengine.ext import webapp
导入索引

class TestTest(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)


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?

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).

解决方案

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).

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

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)

这篇关于如何在Google App Engine中对来自Web应用程序WSGI应用程序的响应进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆