Skip to content

Instantly share code, notes, and snippets.

@emmettbutler
Created June 14, 2013 20:29
Show Gist options
  • Save emmettbutler/5785018 to your computer and use it in GitHub Desktop.
Save emmettbutler/5785018 to your computer and use it in GitHub Desktop.
Tornado TestCase subclass that caches responses in memory. Useful when you need multiple test methods testing a single response and that response takes a long time to generate.
import datetime as dt
from tornado.testing import AsyncHTTPTestCase
class CachedTestCase(AsyncHTTPTestCase):
responses = {}
def cache_response(self, key, response):
self.responses[key] = {}
self.responses[key]['response'] = response
self.responses[key]['time'] = dt.datetime.now()
def get_cached_url(self, path):
if path not in self.responses.keys():
self.http_client.fetch(self.get_url(path), self.stop)
self.cache_response(path, self.wait(timeout=30))
return self.responses[path]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment