Skip to main content

A pytest plugin providing fixtures and markers to simplify testing of asynchronous tornado applications.

Project description

Caution: The original repo is eugeniy/pytest-tornado. The pytest plugin is very useful for me but seems not maintained now (2018/01). I just fork it and add some funcitons which are needed by me.

https://api.travis-ci.org/yen3/pytest-tornado.svg?branch=master

A pytest plugin providing fixtures and markers to simplify testing of asynchronous tornado applications.

Installation

# Install original plugin
pip install pytest-tornado

# Install the fork
pip install pytest-tornado-yen3

Example

import pytest
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

@pytest.fixture
def app():
    return application

@pytest.mark.gen_test
def test_hello_world(http_client, base_url):
    response = yield http_client.fetch(base_url)
    assert response.code == 200

Running tests

pytest

Fixtures

io_loop

creates an instance of the tornado.ioloop.IOLoop for each test case

http_port

get a port used by the test server

base_url

get an absolute base url for the test server, for example, http://localhost:59828

http_server

start a tornado HTTP server, you must create an app fixture, which returns the tornado.web.Application to be tested

http_client

get an asynchronous HTTP client

Show fixtures provided by the plugin:

pytest --fixtures

Markers

A gen_test marker lets you write a coroutine-style tests used with the tornado.gen module:

@pytest.mark.gen_test
def test_tornado(http_client):
    response = yield http_client.fetch('http://www.tornadoweb.org/')
    assert response.code == 200

Sometimes maybe you meet gen_test would hang with your test (ex: test with tornadis). You can use gen_test_current to use the current IOLoop rather then to create a new ioloop for each test

import tornadis

@pytest.mark.gen_test_current
def test_tornadis():
     client = tornadis.Client(host=REDIS_HOST, port=REDIS_PORT,
         autoconnect=True)
     msg = yield client.call('PING')
     assert msg.decode('utf8') == 'PONG'

Marked tests will time out after 5 seconds. The timeout can be modified by setting an ASYNC_TEST_TIMEOUT environment variable, --async-test-timeout command line argument or a marker argument.

@pytest.mark.gen_test(timeout=5)
def test_tornado(http_client):
    yield http_client.fetch('http://www.tornadoweb.org/')

The mark can also receive a run_sync flag, which if turned off will, instead of running the test synchronously, will add it as a coroutine and run the IOLoop (until the timeout). For instance, this allows to test things on both a client and a server at the same time.

@pytest.mark.gen_test(run_sync=False)
def test_tornado(http_server, http_client):
    response = yield http_client.fetch('http://localhost:5555/my_local_server_test/')
    assert response.body == 'Run on the same IOLoop!'

Show markers provided by the plugin:

pytest --markers

Project details


Download files

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

Source Distribution

pytest-tornado-yen3-0.4.6.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

pytest_tornado_yen3-0.4.6-py2.py3-none-any.whl (7.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pytest-tornado-yen3-0.4.6.tar.gz.

File metadata

File hashes

Hashes for pytest-tornado-yen3-0.4.6.tar.gz
Algorithm Hash digest
SHA256 dfdf8d57c63d6e6725e8e98227242cbae7e0b1536c368b44c2c489dae7d159be
MD5 31a2c0a5d91b9fe6df7b717444680a5f
BLAKE2b-256 27076c1f537534c5a610feb406adbdbe6b36a649274af3a732ef1c605b97bdf6

See more details on using hashes here.

File details

Details for the file pytest_tornado_yen3-0.4.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_tornado_yen3-0.4.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a02e13f91405a8e4023bea4b8a81dc460c7f72e50a1f02b1f73a55c6cefce7aa
MD5 4964ede227055eba43ca9467b142578b
BLAKE2b-256 89a672bc6a7e6ee5a907035f2ef7949531d680fcbad1ffc7e96735105f750dfb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page