python-3.x - 使用多台服务器进行 aiohttp 测试
问题描述
尝试进行与 Web 服务器的多个实例(也在它们之间进行通信)进行通信的测试。但是第二个似乎覆盖了第一个,但是我尝试了。有关如何解决此问题的任何建议。
到目前为止,我有这个:
import os
from aiohttp.test_utils import TestClient, TestServer, loop_context
import pytest
from http import HTTPStatus as hs
from mycode import main
@pytest.fixture
def cli(loop):
app = main(["-t", "--location", "global", "-g"])
srv = TestServer(app, port=40080)
client = TestClient(srv, loop=loop)
loop.run_until_complete(client.start_server())
return client
@pytest.fixture
def cli_edge(loop):
app = main(["-t", "--location", "edge", "-j", "http://127.0.0.1:40080"])
srv = TestServer(app, port=40081)
client = TestClient(srv, loop=loop)
loop.run_until_complete(client.start_server())
return client
async def test_edge_namespace(cli, cli_edge):
resp = await cli.put('/do/something', json={})
assert resp.status in [hs.OK, hs.CREATED, hs.NO_CONTENT]
resp = await cli_edge.get('/do/something')
assert resp.status in [hs.OK, hs.CREATED, hs.NO_CONTENT]
上面的调用cli.put
转到用于cli_edge
. 我将有更多的测试应该与服务器通信。
使用带有 asyncio 和 aiohttp 扩展的 Python 3.7 和 pytest。
解决方案
建议的代码有效,错误出现在服务器实现的其他地方。
你可以加:
def fin():
loop.run_until_complete(srv.close())
loop.run_until_complete(client.close())
request.addfinalizer(fin)
和request
pytest 固定装置中的参数可以很好地关闭连接。
推荐阅读
- selenium - Chrome WebDriver - C#.NET 如何加载现有的 chrome cookie/配置文件,这样我就可以绕过身份验证
- python - Python Mysql 连接器。哪个更好 connection.close() 或 connection.disconnect() 或 connection.shutdown()
- google-cloud-platform - 如何使用 Cloud Function 执行服务帐户管理操作(创建、列出、删除)?
- java - 对象为空时无法进入if语句
- javascript - Next.js _app.js:模块解析失败:新项目上出现意外令牌
- laravel - 如何修复 DELETE 方法在 Laravel 中不起作用
- mongodb - Kubernetes mongo DB 用户创建
- node.js - 如何从命令行将自定义变量传递给 pm2
- playwright - 如何更改启动“npx folio”的目录
- docker - 通过 docker 镜像访问 SMB 共享还是通过 docker 主机连接到 SMB 共享?