Debian Codegraph @

README.rst

raw
1 ================
2 pytest-tornasync
3 ================
4
5 .. image:: https://travis-ci.org/eukaryote/pytest-tornasync.svg?branch=master
6 :target: https://travis-ci.org/eukaryote/pytest-tornasync
7
8 A simple pytest plugin that provides some helpful fixtures for testing
9 Tornado (version 5.0 or newer) apps and easy handling of plain
10 (undecoratored) native coroutine tests (Python 3.5+).
11
12 Why another Tornado pytest plugin when the excellent ``pytest-tornado`` already
13 exists? The main reason is that I didn't want to have to decorate every test
14 coroutine with ``@pytest.mark.gen_test``. This plugin doesn't have anything
15 like ``gen_test``. Defining a test with ``async def`` and a name that
16 begins with ``test_`` is all that is required.
17
18
19 Installation
20 ------------
21
22 Install using pip, which must be run with Python 3.5+:
23
24 .. code-block:: sh
25
26 pip install pytest-tornasync
27
28
29 Usage
30 -----
31
32 Define an ``app`` fixture:
33
34 .. code-block:: python
35
36 import pytest
37
38
39 @pytest.fixture
40 def app():
41 import yourapp
42 return yourapp.make_app() # a tornado.web.Application
43
44
45 Create tests as native coroutines using Python 3.5+ ``async def``:
46
47 .. code-block:: python
48
49 async def test_app(http_server_client):
50 resp = await http_server_client.fetch('/')
51 assert resp.code == 200
52 # ...
53
54
55 Fixtures
56 --------
57
58 When the plugin is installed, then ``pytest --fixtures`` will show
59 the fixtures that are available:
60
61 http_server_port
62 Port used by `http_server`.
63 http_server
64 Start a tornado HTTP server that listens on all available interfaces.
65
66 You must create an `app` fixture, which returns
67 the `tornado.web.Application` to be tested.
68
69 Raises:
70 FixtureLookupError: tornado application fixture not found
71 http_server_client
72 Create an asynchronous HTTP client that can fetch from `http_server`.
73 http_client
74 Create an asynchronous HTTP client that can fetch from anywhere.
75 io_loop
76 Create a new `tornado.ioloop.IOLoop` for each test case.
77
78
79
80 Examples
81 --------
82
83 .. code-block:: python
84
85 import time
86
87 import tornado.web
88 import tornado.gen
89
90 import pytest
91
92
93 class MainHandler(tornado.web.RequestHandler):
94 def get(self):
95 self.write("Hello, world!")
96
97
98 @pytest.fixture
99 def app():
100 return tornado.web.Application([(r"/", MainHandler)])
101
102
103 async def test_http_server_client(http_server_client):
104 # http_server_client fetches from the `app` fixture and takes path
105 resp = await http_server_client.fetch('/')
106 assert resp.code == 200
107 assert resp.body == b"Hello, world!"
108
109
110 async def test_http_client(http_client):
111 # http_client fetches from anywhere and takes full URL
112 resp = await http_client.fetch('http://httpbin.org/status/204')
113 assert resp.code == 204
114
115
116 async def example_coroutine(period):
117 await tornado.gen.sleep(period)
118
119
120 async def test_example():
121 # no fixtures needed
122 period = 1.0
123 start = time.time()
124 await example_coroutine(period)
125 elapsed = time.time() - start
126 assert elapsed >= period