| 1 |
import time |
| 2 |
|
| 3 |
import tornado.gen |
| 4 |
import tornado.ioloop |
| 5 |
import tornado.web |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from test import MESSAGE, PAUSE_TIME |
| 10 |
|
| 11 |
|
| 12 |
class ExpectedError(RuntimeError): |
| 13 |
|
| 14 |
""" |
| 15 |
A dedicated error type for raising from tests to verify a fixture was run. |
| 16 |
""" |
| 17 |
|
| 18 |
|
| 19 |
@pytest.fixture |
| 20 |
def mynumber(): |
| 21 |
return 42 |
| 22 |
|
| 23 |
|
| 24 |
class MainHandler(tornado.web.RequestHandler): |
| 25 |
def get(self): |
| 26 |
self.write(MESSAGE) |
| 27 |
|
| 28 |
|
| 29 |
def _pause_coro(period): |
| 30 |
return tornado.gen.sleep(period) |
| 31 |
|
| 32 |
|
| 33 |
async def pause(): |
| 34 |
await _pause_coro(PAUSE_TIME) |
| 35 |
|
| 36 |
|
| 37 |
def test_plain_function(): |
| 38 |
|
| 39 |
assert True |
| 40 |
|
| 41 |
|
| 42 |
def test_plain_function_with_fixture(mynumber): |
| 43 |
|
| 44 |
assert mynumber == 42 |
| 45 |
|
| 46 |
|
| 47 |
async def nontest_coroutine(io_loop): |
| 48 |
|
| 49 |
assert False |
| 50 |
|
| 51 |
|
| 52 |
def nontest_function(io_loop): |
| 53 |
|
| 54 |
assert False |
| 55 |
|
| 56 |
|
| 57 |
async def test_pause(io_loop): |
| 58 |
start = time.time() |
| 59 |
await pause() |
| 60 |
elapsed = time.time() - start |
| 61 |
assert elapsed >= PAUSE_TIME |
| 62 |
|
| 63 |
|
| 64 |
async def test_http_client_fetch(http_client, http_server, http_server_port): |
| 65 |
url = "http://localhost:%s/" % http_server_port[1] |
| 66 |
resp = await http_client.fetch(url) |
| 67 |
assert resp.code == 200 |
| 68 |
assert resp.body.decode("utf8") == MESSAGE |
| 69 |
|
| 70 |
|
| 71 |
async def test_http_server_client_fetch(http_server_client): |
| 72 |
resp = await http_server_client.fetch("/") |
| 73 |
assert resp.code == 200 |
| 74 |
assert resp.body.decode("utf8") == MESSAGE |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.xfail(raises=ExpectedError) |
| 78 |
def test_expected_noncoroutine_fail(): |
| 79 |
raise ExpectedError() |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.xfail(raises=ExpectedError) |
| 83 |
async def test_expected_coroutine_fail_no_ioloop(): |
| 84 |
"""A coroutine test without an io_loop param.""" |
| 85 |
raise ExpectedError() |
| 86 |
|
| 87 |
|
| 88 |
@pytest.mark.xfail(raises=ExpectedError) |
| 89 |
async def test_expected_coroutine_fail_io_loop(io_loop): |
| 90 |
"""A coroutine test with an io_loop param.""" |
| 91 |
raise ExpectedError() |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.xfail(strict=True, raises=tornado.ioloop.TimeoutError) |
| 95 |
@pytest.mark.timeout(seconds=0.1) |
| 96 |
async def test_timeout(io_loop): |
| 97 |
await _pause_coro(0.15) |