一个pytest插件,提供 fixture 和 marker 以简化异步 tornado 应用程序的测试。
项目描述
一个提供 fixture 和 marker 以简化异步 tornado 应用程序测试的 pytest 插件。
安装
pip install pytest-tornado
示例
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
运行测试
py.test
Fixture
- io_loop
为每个测试用例创建一个 tornado.ioloop.IOLoop 实例
- http_port
获取测试服务器使用的端口
- base_url
获取测试服务器的绝对基本 URL,例如,http://localhost:59828
- http_server
启动一个 tornado HTTP 服务器,你必须创建一个 app fixture,它返回要测试的 tornado.web.Application
- http_client
获取一个异步 HTTP 客户端
显示插件提供的 fixture
py.test --fixtures
Marker
一个 gen_test 标记允许您使用 tornado.gen 模块编写协程风格的测试
@pytest.mark.gen_test
def test_tornado(http_client):
response = yield http_client.fetch('https://tornado.pythonlang.cn/')
assert response.code == 200
标记的测试将在5秒后超时。可以通过设置 ASYNC_TEST_TIMEOUT 环境变量、--async-test-timeout 命令行参数或标记参数来修改超时时间。
@pytest.mark.gen_test(timeout=5)
def test_tornado(http_client):
yield http_client.fetch('https://tornado.pythonlang.cn/')
标记还可以接收一个 run_sync 标志,如果关闭,则不会同步运行测试,而是将其作为协程添加并运行 IOLoop(直到超时)。例如,这允许同时在一个客户端和一个服务器上测试事物。
@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!'
显示插件提供的标记
py.test --markers
项目详情
关闭
pytest-tornado5-2.0.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0de5eaaff0af802a6557dd5e27ab8eab94e363c7e07a419237b9032eb8146661 |
|
MD5 | 7d3c4c67b8b0fcde4ec9ca84f04e9688 |
|
BLAKE2b-256 | d0dc3c22d68bd33ceed02cafd32b69f4501963465e11f559d335af8781e0a03f |
关闭
pytest_tornado5-2.0.0-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a61164e1f34603be446c17f778b1e6d53e3153c0e68f47e632fe6184357c2f45 |
|
MD5 | 988caac954f818044bd1c63f6b30266e |
|
BLAKE2b-256 | 5b8618862ee76649dd69416c1ff3228ca0872826cbc361edc6fb5dfa3dc3c17e |