Skip to content

Instantly share code, notes, and snippets.

@reinhrst
Last active February 26, 2016 15:18
Show Gist options
  • Save reinhrst/8775b63b1c369773e1f7 to your computer and use it in GitHub Desktop.
Save reinhrst/8775b63b1c369773e1f7 to your computer and use it in GitHub Desktop.
Tornado async testing framework with asyncio and using your own server options
import tornado.web
import tornado.httpserver
def make_app():
app = tornado.web.Application(
[(r"/.*", PageHandler)],
debug=tornado.options.options.serverdebug,
cookie_secret=config.COOKIE_SECRET,
compress_response=True)
return app
def make_server(app, io_loop=None):
server = tornado.httpserver.HTTPServer(
app, io_loop=io_loop, decompress_request=True)
return server
def start_server(port):
app = make_app()
server = make_server(app)
server.bind(port)
server.start()
import tornado.platform.asyncio
import asyncio
import tornado.testing
class MyTestCase(tornado.testing.AsyncHTTPTestCase):
def get_app(self):
return server.make_app()
def get_http_server(self):
return server.make_server(self._app, self.io_loop)
def get_new_ioloop(self):
"""
Needed to make sure that I can also run asyncio based callbacks in my tests
"""
io_loop = tornado.platform.asyncio.AsyncIOLoop()
asyncio.set_event_loop(io_loop.asyncio_loop)
return io_loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment