Skip to content

Instantly share code, notes, and snippets.

@wvega
Created December 11, 2013 00:51
Show Gist options
  • Save wvega/7903256 to your computer and use it in GitHub Desktop.
Save wvega/7903256 to your computer and use it in GitHub Desktop.
# a fully-functional Python unittest example where we have a
# Tornado web application that is being tested:
#
# taken from https://groups.google.com/forum/#!topic/python-tornado/hnz7JmXqEKk
# originally posted by Jeremy Whitlock
import tornado.httpserver
import tornado.httpclient
import tornado.ioloop
import tornado.web
import unittest
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write('Hello, world')
class TestTornadoWeb(unittest.TestCase):
response = None
def setUp(self):
application = tornado.web.Application([
(r'/', MainHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
def handle_request(self, response):
self.response = response
tornado.ioloop.IOLoop.instance().stop()
def testHelloWorldHandler(self):
http_client = tornado.httpclient.AsyncHTTPClient()
http_client.fetch('http://localhost:8888/',
self.handle_request)
tornado.ioloop.IOLoop.instance().start()
self.failIf(self.response.error)
self.assertEqual(self.response.body, 'Hello, world')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment