Python pytest asyncio monkey修补程序请求.post不起作用

Python pytest asyncio monkey修补程序请求.post不起作用,python,pytest,monkeypatching,python-asyncio,pytest-asyncio,Python,Pytest,Monkeypatching,Python Asyncio,Pytest Asyncio,我正在尝试对requests.post内部的test_应用程序进行monkeypatch,我正在使用quart(restapi)的test_客户端,它是异步的。 我从反应中得到的结果是好的,但是测试失败了 导入我的应用程序 导入请求 导入异步 导入pytest app=my_app.app 打印(应用程序) 类响应: @静力学方法 def json(): 数据={“状态”:“确定”} 返回数据 @静力学方法 def状态_代码(): 返回200 #@asyncio.coroutine @pytes

我正在尝试对requests.post内部的test_应用程序进行monkeypatch,我正在使用quart(restapi)的test_客户端,它是异步的。 我从反应中得到的结果是好的,但是测试失败了

导入我的应用程序
导入请求
导入异步
导入pytest
app=my_app.app
打印(应用程序)
类响应:
@静力学方法
def json():
数据={“状态”:“确定”}
返回数据
@静力学方法
def状态_代码():
返回200
#@asyncio.coroutine
@pytest.fixture(autouse=True)
def模拟_响应(monkeypatch):
def模拟邮政(*args,**kwargs):
返回MockResponse()
setattr(请求,“post”,mock_post,risting=True)
@pytest.mark.asyncio
异步def测试应用程序(模拟响应):
尝试:
client=app.test\u client()
response=wait client.post(“/func”)
res_json=wait response.get_json()
打印(“响应:+str(响应))
打印(“响应:+str(响应状态代码))
打印(“响应:+str(res_json))
assert response.status_code==200
assert res_json=={“status”:“ok”}
例外情况除外,如e:
打印(e)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
pytest.main()
结果是:

pyfuncitem = <Function test_app>

    @pytest.mark.tryfirst
    def pytest_pyfunc_call(pyfuncitem):
        """
        Run asyncio marked test functions in an event loop instead of a normal
        function call.
        """
        for marker_name, fixture_name in _markers_2_fixtures.items():
            if marker_name in pyfuncitem.keywords \
                    and not getattr(pyfuncitem.obj, 'is_hypothesis_test', False):
                event_loop = pyfuncitem.funcargs[fixture_name]

                funcargs = pyfuncitem.funcargs
                testargs = {arg: funcargs[arg]
                            for arg in pyfuncitem._fixtureinfo.argnames}

                event_loop.run_until_complete(
                    asyncio.ensure_future(
>                       pyfuncitem.obj(**testargs), loop=event_loop))

/usr/local/lib/python3.7/dist-packages/pytest_asyncio/plugin.py:158: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

coro_or_future = None

    def ensure_future(coro_or_future, *, loop=None):
        """Wrap a coroutine or an awaitable in a future.

        If the argument is a Future, it is returned directly.
        """
        if coroutines.iscoroutine(coro_or_future):
            if loop is None:
                loop = events.get_event_loop()
            task = loop.create_task(coro_or_future)
            if task._source_traceback:
                del task._source_traceback[-1]
            return task
        elif futures.isfuture(coro_or_future):
            if loop is not None and loop is not futures._get_loop(coro_or_future):
                raise ValueError('loop argument must agree with Future')
            return coro_or_future
        elif inspect.isawaitable(coro_or_future):
            return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)
        else:
>           raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
                            'required')
E           TypeError: An asyncio.Future, a coroutine or an awaitable is required

/usr/lib/python3.7/asyncio/tasks.py:592: TypeError
----------------------------- Captured stdout call -----------------------------


ressssss <my_test.MockResponse object at 0x7f08c6466dd8>
response : Response(200)
response : 200
response : {'status': 'ok'}
pyfuncitem=
@pytest.mark.tryfirst
def pytest_pyfunc_调用(pyfuncitem):
"""
在事件循环而不是普通循环中运行标记为asyncio的测试函数
函数调用。
"""
对于标记_名称,_markers_2_fixtures.items()中的fixture_名称:
如果pyfuncitem.keywords中有标记_名称\
而不是getattr(pyfuncitem.obj,'是假设检验',错误):
event\u loop=pyfuncitem.funcargs[夹具名称]
funcargs=pyfuncitem.funcargs
testargs={arg:funcargs[arg]
用于pyfuncitem中的arg。\u fixtureinfo.argnames}
事件\u循环。运行\u直到\u完成(
asyncio.u确保未来(
>pyfuncitem.obj(**testargs),循环=事件\循环)
/usr/local/lib/python3.7/dist-packages/pytest\u-asyncio/plugin.py:158:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
coro_或_future=无
def sure_future(coro_或_future,*,loop=None):
“包装一个共同计划或一个在未来等待的计划。
如果参数是未来的,则直接返回。
"""
如果coroutines.iscoroutine(coro_或未来):
如果循环为无:
loop=events.get\u event\u loop()
任务=循环。创建任务(coro或未来)
如果任务。\u源\u回溯:
删除任务。\u源\u回溯[-1]
返回任务
elif futures.isfuture(coro_或_future):
如果循环不是None,循环不是futures.\u get\u循环(coro\u或\u future):
raise VALUERROR('循环参数必须与Future'一致')
返回coro_或_未来
elif检查可等待(coro_或未来):
返回确保未来(_wrap_waitiable(coro_或_future),loop=loop)
其他:
>raise TypeError('异步IO.Future、协同程序或可等待的is'
“必需的”)
E TypeError:需要一个asyncio.Future、一个协程或一个可等待的
/usr/lib/python3.7/asyncio/tasks.py:592:TypeError
-----------------------------捕获的stdout调用-----------------------------
RESSSSS
答复:答复(200)
答复:200
响应:{'status':'ok'}
即使来自模拟的值是好值,它也会失败
这意味着函数将正确的值返回到assert

非常确定问题在于
请求
模块。它是一个阻塞模块(同步)。但是我很好奇你是否已经解决了最后的问题,我很确定问题是
请求
模块。它是一个阻塞模块(同步)。但我很好奇你是否最终解决了你的问题