跳转到主要内容

一个pytest插件,提供固定装置和标记以简化异步tornado应用程序的测试。

项目描述

https://travis-ci.org/eugeniy/pytest-tornado.svg?branch=master https://coveralls.io/repos/eugeniy/pytest-tornado/badge.svg

一个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 (9.1 kB 查看哈希值)

上传时间 源代码

构建分发

pytest_tornado-0.8.1-py2.py3-none-any.whl (9.6 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面