Metadata-Version: 2.0
Name: pytest-tornado-yen3
Version: 0.4.6
Summary: A pytest plugin providing fixtures and markers to simplify testing of asynchronous tornado applications.
Home-page: https://github.com/eugeniy/pytest-tornado
Author: Eugeniy Kalinin, Yen3
Author-email: burump@gmail.com, yen3@gmail.com
License: Apache License, Version 2.0
Description-Content-Type: UNKNOWN
Keywords: pytest pytest tornado async asynchronous testing unit tests plugin
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: Plugins
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Testing
Requires-Dist: pytest
Requires-Dist: tornado

pytest-tornado
==============

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.

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

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
-------

.. code-block:: python

    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:

.. code-block:: python

    @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


.. code-block:: python

    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.

.. code-block:: python

    @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.

.. code-block:: python

    @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


.. _eugeniy/pytest-tornado: https://github.com/eugeniy/pytest-tornado
.. _pytest: http://pytest.org/
.. _`tornado.ioloop.IOLoop`: http://tornado.readthedocs.org/en/latest/ioloop.html#ioloop-objects
.. _`tornado.web.Application`: http://tornado.readthedocs.org/en/latest/web.html#application-configuration
.. _`tornado.gen`: http://tornado.readthedocs.org/en/latest/gen.html
.. _tornadis: https://github.com/thefab/tornadis


