一个pytest插件,提供固定装置和标记以简化异步tornado应用程序的测试。
项目描述
一个pytest插件,提供固定装置和标记以简化异步tornado应用程序的测试。
安装
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
固定装置
- io_loop
为每个测试案例创建一个tornado.ioloop.IOLoop实例
- http_port
获取测试服务器使用的端口号
- base_url
获取测试服务器的绝对基本URL,例如 http://localhost:59828。也可以与HTTPS固定装置一起使用,并将返回相应的URL,例如 http://localhost:48372。
- http_server
启动Tornado HTTP服务器,你必须创建一个app测试用例,该测试用例返回将被测试的tornado.web.Application
- http_client
获取一个异步HTTP客户端
还可以测试使用HTTPS的应用程序。要运行HTTPS服务器,你需要一个证书。
- https_port
获取测试服务器使用的端口号。
- https_server
启动Tornado HTTPS服务器。你必须创建一个app测试用例,该测试用例返回将被测试的tornado.web.Application,以及一个ssl_options测试用例,该测试用例返回tornado.httpserver.HTTPServer的SSL选项。
- https_client
获取一个异步HTTP客户端。如果你的测试使用自签名证书,可以在fetch方法中设置verify=False。
显示插件提供的测试用例
py.test --fixtures
标记
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
此标记还支持使用async/await语法编写测试
@pytest.mark.gen_test
async def test_tornado(http_client):
response = await 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-tornado-0.8.1.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b9fa1930c333b9dfe29bde90f9f42e6643278b5d0c95528a8302a7454fd3f1b1 |
|
MD5 | cbd3ba23ffca0e4ec774c219552d82ef |
|
BLAKE2b-256 | f785a7b43fb46dbd7d6c28e798491e5f7dc902e0fbbd1630b03307529faebf79 |
pytest_tornado-0.8.1-py2.py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 8d43f400c64fabc85af58891096d4f93f05e9f9baae2c751e039932a0f62d0b5 |
|
MD5 | 9b24d8bbc0800f1ba96db83d65c779af |
|
BLAKE2b-256 | e77f111eb35d4f4ea7a654294b76f3d98eee6c704282fd028ae37bfc259e2216 |