14

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

David Coffin
  • 161
  • 1
  • 5

2 Answers2

13

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)
Steve
  • 684
  • 7
  • 8
  • Patching `webtest/__init__.py` is no longer necessary, as webbrowser is only imported by the `webtest.app:showbrowser` function if it's called. See https://github.com/Pylons/webtest/commit/78076424c219935ee556aab84d943d5949530531 and https://github.com/Pylons/webtest/commit/53889b57fe16c57fd7f532953d2e15bfaba7e5b3 – Tripp Lilley Jan 10 '13 at 17:34
2

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

import webbrowser

in webtest/__init__.py

David Coffin
  • 161
  • 1
  • 5
  • Patching `webtest/__init__.py` is no longer necessary, as webbrowser is only imported by the `webtest.app:showbrowser` function if it's called. See https://github.com/Pylons/webtest/commit/78076424c219935ee556aab84d943d5949530531 and https://github.com/Pylons/webtest/commit/53889b57fe16c57fd7f532953d2e15bfaba7e5b3 – Tripp Lilley Jan 10 '13 at 17:34