Skip to main content

"Easy to mock external HTTP request in Tornado."

Project description PyPI


Easy to mock external HTTP request in Tornado.


$ pip install --upgrade flymock

Create mock data

Make a directory in your tests package:

$ mkdir __mock__

Use the hostname as the config filename, assume the url is, the config filename should be, the config see below:

- path: /demo    # path of the request to match
  method: GET    # method of the request to match
  headers:       # Response headers
    Content-Type: application/json
  body: Hello world # Response body
  code: 200      # Response status code

- path: /file
  body_type: file     # Use a file content as the response
  body: demo.json     # Filename(same path of the config file)
  code: 202

- path: /json
  body:               # If body is an object, that will response JSON content.
   code: 2


import os

from tornado import httpclient
from tornado import testing

from flymock import FlyPatcher

class DemoTestCase(testing.AsyncTestCase):
    def setUp(self):
        super(DemoTestCase, self).setUp()
        path = os.path.join(os.path.dirname(__file__), "__mock__")
        self.patcher = FlyPatcher(path)
        self.http_client = httpclient.AsyncHTTPClient()

    def tearDown(self):
        super(DemoTestCase, self).tearDown()

    def test_mocked(self):
        resp = yield self.http_client.fetch("")
        self.assertEqual(resp.code, 200)

Adjust response dynamic:

patcher = FlyPatcher("/path/to/__mock__")
def hook(response):
   response.patch_json({"a": 1})

with patcher.dynamic_hook(hook):
    # code goes here

# shortcut to adjust JSON
with patcher.patch_json({"a": 1}):
   # code goes here

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for flymock, version 0.0.6
Filename, size File type Python version Upload date Hashes
Filename, size flymock-0.0.6.tar.gz (11.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page