跳转到主要内容

一个pytest插件,提供 fixture 和 marker 以简化异步 tornado 应用程序的测试。

项目描述

https://travis-ci.org/vidartf/pytest-tornado.svg?branch=master

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

上传时间

构建分发

pytest_tornado5-2.0.0-py2.py3-none-any.whl (4.9 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下机构支持

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