Skip to main content

py.test plugin for testing Python 3.5+ Tornado code

Project description

https://travis-ci.org/eukaryote/pytest-tornasync.svg?branch=master

A simple pytest plugin that provides some helpful fixtures for testing Tornado (version 5.0 or newer) apps and easy handling of plain (undecoratored) native coroutine tests (Python 3.5+).

Why another Tornado pytest plugin when the excellent pytest-tornado already exists? The main reason is that I didn’t want to have to decorate every test coroutine with @pytest.mark.gen_test. This plugin doesn’t have anything like gen_test. Defining a test with async def and a name that begins with test_ is all that is required.

Installation

Install using pip, which must be run with Python 3.5+:

pip install pytest-tornasync

Usage

Define an app fixture:

import pytest


@pytest.fixture
def app():
    import yourapp
    return yourapp.make_app()  # a tornado.web.Application

Create tests as native coroutines using Python 3.5+ async def:

async def test_app(http_server_client):
    resp = await http_server_client.fetch('/')
    assert resp.code == 200
    # ...

Fixtures

When the plugin is installed, then pytest --fixtures will show the fixtures that are available:

http_server_port

Port used by http_server.

http_server

Start a tornado HTTP server that listens on all available interfaces.

You must create an app fixture, which returns the tornado.web.Application to be tested.

Raises: FixtureLookupError: tornado application fixture not found

http_server_client

Create an asynchronous HTTP client that can fetch from http_server.

http_client

Create an asynchronous HTTP client that can fetch from anywhere.

io_loop

Create a new tornado.ioloop.IOLoop for each test case.

Examples

import time

import tornado.web
import tornado.gen

import pytest


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


@pytest.fixture
def app():
    return tornado.web.Application([(r"/", MainHandler)])


async def test_http_server_client(http_server_client):
    # http_server_client fetches from the `app` fixture and takes path
    resp = await http_server_client.fetch('/')
    assert resp.code == 200
    assert resp.body == b"Hello, world!"


async def test_http_client(http_client):
    # http_client fetches from anywhere and takes full URL
    resp = await http_client.fetch('http://httpbin.org/status/204')
    assert resp.code == 204


async def example_coroutine(period):
    await tornado.gen.sleep(period)


async def test_example():
    # no fixtures needed
    period = 1.0
    start = time.time()
    await example_coroutine(period)
    elapsed = time.time() - start
    assert elapsed >= period

Changes

0.6.0 (2018-11-19)

  • minor updates to avoid a pytest warning under pytest 4

  • repo switch to using a ‘src’ dir

0.5.0 (2018-05-28)

  • updated to work with Tornado 5, which is now the minimum required version

  • require pytest >= 3.0

  • the io_loop fixture always refers to a tornado.ioloop.IOLoop instance now

  • the io_loop_asyncio and io_loop_tornado fixtures have been removed, since now that Tornado 5 always uses asyncio under Python 3, there would be no difference between the two fixtures, so io_loop is all that is needed

  • tox tests now test more versions of Tornado (5.0.* and latest 5.*), Pytest (3.0.* and latest 3.*), and Python (3.5, 3.6, 3.7, and pypy3).

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-tornasync-0.6.0.post2.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

pytest_tornasync-0.6.0.post2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file pytest-tornasync-0.6.0.post2.tar.gz.

File metadata

  • Download URL: pytest-tornasync-0.6.0.post2.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for pytest-tornasync-0.6.0.post2.tar.gz
Algorithm Hash digest
SHA256 d781b6d951a2e7c08843141d3ff583610b4ea86bfa847714c76edefb576bbe5d
MD5 c81f6f5975666e9bfaaa31a349ae3e17
BLAKE2b-256 54a0e096d3609793ea1c3bbf255f923da453b83728cfc9f10bcbab98d6932d74

See more details on using hashes here.

File details

Details for the file pytest_tornasync-0.6.0.post2-py3-none-any.whl.

File metadata

  • Download URL: pytest_tornasync-0.6.0.post2-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3

File hashes

Hashes for pytest_tornasync-0.6.0.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 4b165b6ba76b5b228933598f456b71ba233f127991a52889788db0a950ad04ba
MD5 c9fa07eb02c044f5a1c943d9e723fc61
BLAKE2b-256 50b93615ebfc3120bb949c3725b50793f42c3230d0175d6cfd358ea8bb6928ff

See more details on using hashes here.

Supported by

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