简体   繁体   English

如何从pytest夹具运行aiohttp应用

[英]How to run aiohttp app from pytest fixture

I have system under test(SUT) that requires socketio-server. 我有需要套接字服务器的被测系统(SUT)。 This server would response to SUT in some functionality. 该服务器将在某些功能上响应SUT。 So socketio-server is necessary enviroment for my SUT. 因此,socketio服务器对于我的SUT是必不可少的环境。

For socketio-server i chose aiohttp with python-socketio. 对于socketio-server,我选择了带python-socketio的aiohttp。

Example of functionality my socketio-server: 我的socketio服务器功能示例:

from aiohttp import web
import socketio


sio = socketio.AsyncServer()
app = web.Application()
sio.attach(app)


@sio.on('commands')
async def message_handler(sid, msg):
    if msg['data']['command'] == 'terminal_settings_info':
        response_msg = {'handler': msg['handler'],
                        'data': {'result': 1,
                                 'is_success': True,
                                 'TerminalID': 5,
                                 'request_id': msg['data']['request_id']}}
        await sio.emit('commands', response_msg)

    elif msg['data']['command'] == 'get_agent_info':
        response_msg = {'handler': msg['handler'],
                        'data': {'result': 1,
                                 'is_success': True,
                                 'terminal_address': 'Zelenograd',
                                 'agent_support_phone': '8-888-88-88-88',
                                 'mf_retail': True,
                                 'request_id': msg['data']['request_id']}}
        await sio.emit('commands', response_msg)

    else:
        raise ValueError('Unknown request.\nMessage: ' + msg.__repr__())


web.run_app(app, host='127.0.0.1', port=1234)

I want to SetUp this socketio-server from pytest fixture before run tests and TearDown after tests complete. 我想在运行测试之前从pytest夹具设置此socketio服务器,并在测试完成后从TearDown设置。 So, this server must non-blocks pytest thread. 因此,该服务器必须不阻塞pytest线程。 This is my purpose. 这是我的目的 How can i do this? 我怎样才能做到这一点?

I'm pretty good in pytest and syncrhonous code execution in Python, but newbi for async programming(asyncio, aiohttp) and newbi for using threading, subprocess or multiprocessing. 我在python的pytest和同步代码执行方面相当出色,但是newbi用于异步编程(asyncio,aiohttp),而newbi用于使用线程,子进程或多处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM