A py.test plugin providing fixtures and markers to simplify testing of asynchronous tornado applications.
Project description
A py.test plugin providing fixtures and markers to simplify testing of asynchronous tornado applications.
Installation
pip install pytest-tornado
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
py.test
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:
py.test --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
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:
py.test --markers
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-tornado-0.4.5.tar.gz
.
File metadata
- Download URL: pytest-tornado-0.4.5.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 28b772d8dbea3d98da94d6697f739652a373b35123e0ed60af77557c6a2c392c |
|
MD5 | b6ccf5c7f18a9d1a9e399612ef4b0c03 |
|
BLAKE2b-256 | 18f454a40ea6b8c2a1ce803d3383294c1eaf7bd0546bff9d777d76bb824bd8c3 |
Provenance
File details
Details for the file pytest_tornado-0.4.5-py2.py3-none-any.whl
.
File metadata
- Download URL: pytest_tornado-0.4.5-py2.py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16aa582dc2638ba8e5a7e44ab0f6a6480a86c8b0aeda7b18f3aa0539d3256647 |
|
MD5 | 64a37862221c8b6daf1be101cd4de434 |
|
BLAKE2b-256 | a7ea7cd1f5b245364102b829e97ac9e73b5ff445e64d29c9c8c220f9f4c40380 |