我想單元測試來自Google App Engine webapp.WSGIApplication的響應,例如請求url'/'並使用GAEUnit測試響應狀態代碼爲200。我怎樣才能做到這一點?如何在Google App Engine中測試Web應用程序WSGI應用程序的響應?
我想使用在App Engine沙箱中運行的webapp框架和GAEUnit(不幸的是,WebTest在沙箱內不起作用)。
我想單元測試來自Google App Engine webapp.WSGIApplication的響應,例如請求url'/'並使用GAEUnit測試響應狀態代碼爲200。我怎樣才能做到這一點?如何在Google App Engine中測試Web應用程序WSGI應用程序的響應?
我想使用在App Engine沙箱中運行的webapp框架和GAEUnit(不幸的是,WebTest在沙箱內不起作用)。
其實WebTest的並在沙箱中工作,只要你註釋掉
import webbrowser
在WebTest的
/__ init__.py
我添加了一個sample application到演示如何編寫和GAEUnit項目使用GAEUnit執行網絡測試。該示例包含「webtest」模塊的稍微修改版本(根據David Coffin的建議,「import webbrowser」已被註釋掉)。
下面是示例應用程序 '測試' 目錄下的 '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)
修補`webtest/__ init __。py`不再是必需的,因爲webbrowser只是如果被調用,則由`webtest.app:showbrowser`函數導入。請參閱https://github.com/Pylons/webtest/commit/78076424c219935ee556aab84d943d5949530531和https://github.com/Pylons/webtest/commit/53889b57fe16c57fd7f532953d2e15bfaba7e5b3 – 2013-01-10 17:34:48
修補`WebTest的/ __的init __ py`不再是必要的,如網頁瀏覽器僅由`WebTest的進口。 app:showbrowser`函數是否被調用。請參閱https://github.com/Pylons/webtest/commit/78076424c219935ee556aab84d943d5949530531和https://github.com/Pylons/webtest/commit/53889b57fe16c57fd7f532953d2e15bfaba7e5b3 – 2013-01-10 17:34:08