我是 asyncio 的新手,我有一个测试函数,我正在尝试使用 pytest 进行测试:这是我的测试结构:
MyClass()
async def myFunction(payload, headers):
session = aiohttp.ClientSession()
with session.post(url, json=payload, headers=headers) as resp:
response = await resp.json()
return response
Run Code Online (Sandbox Code Playgroud)
然后我的测试如下:
def my_test():
loop = asyncio.get_event_loop()
reponse = loop.run_until_complete(MyClass().myFunction(url, headers))
loop.close()
Run Code Online (Sandbox Code Playgroud)
当我运行测试时我得到:
pytest运行时错误线程“主线程”中没有当前事件循环
我该如何解决它,我觉得我需要以不同的方式处理线程。
我看到了一些相关的问题,但没有人使用 pytests 最后我使用的是 Python 3.8.6。
又是我,现在有一个棉花糖问题,我有以下结构:
路线.py
@models.response(ParentSchema())
def get(args, model_id):
return {"response": {"a": [{"data": "a"}], "b": [{"data": "a"}], "c": [{"data": "a"}], "d": [{"data": "a"}]}}
Run Code Online (Sandbox Code Playgroud)
那么这是我的模式文件 schema.py
class SubChildSchema(Schema):
data = fields.Str(description="Model's name")
class ChildSchema(Schema):
class Meta:
ordered = True
a = fields.Nested(SubChildSchema, many=True)
b = fields.Nested(SubChildSchema, many=True)
c = fields.Nested(SubChildSchema, many=True)
d = fields.Nested(SubChildSchema, many=True)
class ParentSchema(Schema):
response = fields.Nested(ChildSchema)
Run Code Online (Sandbox Code Playgroud)
它假设在响应中我应该得到一个排序的响应,例如:
{
"response": {
"a": [
{
"data": "a"
}
],
"b": [
{
"data": "a"
}
],
"c": [
{
"data": "a"
} …Run Code Online (Sandbox Code Playgroud) 由于错误的原因,我通过运行更新了我的 pyenv ,pyenv update此后每次打开新控制台时都会出现此错误
WARNING: `pyenv init -` no longer sets PATH.
Run `pyenv init` to see the necessary changes to make to your configuration.
Run Code Online (Sandbox Code Playgroud)
我尝试将其添加到我的 .zshrc 中:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init --path)"
fi
Run Code Online (Sandbox Code Playgroud)
这是在其他帖子和问题中推荐的,但在这种情况下,我收到了另一条错误消息:
Failed to initialize virtualenvwrapper.
Perhaps pyenv-virtualenvwrapper has not been loaded into your shell properly.
Please restart current shell and try again.
Run Code Online (Sandbox Code Playgroud)
更多信息:
系统:MacOS Catalina 10.15.7 Pyenv 2.0.2 版
谢谢你们