我在模块名中有两个函数/应用程序副本在
async def f1(arg):
# do something
return arg + '1'
async def f2(arg):
result = await f1(arg)
return result
我尝试使用pytest和asynctest测试f2和模拟f1。
只有我这样做才管用
测试通过
但是,我想做这样的事情
import module_name
@pytest.fixture()
def mock_f1():
return asynctest.CoroutineMock(module_name.app.f1, side_effect=sf_f1)
@pytest.mark.asyncio
async def test_f2_2(mock_f1):
assert 'some value' == await f2('test')
我明白了
assert 'some value' == await f2('test')
AssertionError: assert 'some value' == 'test1'
- some value
+ test1
为什么第二条路不行?在
在第二个示例中,在
mock_f1
fixture中,创建一个CoroutineMock
对象并返回它。但是你不能重写module_name.app.f1
函数:Mock
类对象不会自动修补任何内容。在以下是对示例的补充说明:
会印出这样的想法
^{pr2}$当您调用
f2
时,它使用模块中的f1
函数,该函数不会被重写。在以下是如何为您工作:
您可能知道,
monkeypatch
将确保只有在fixture处于活动状态时才应用更改。在相关问题 更多 >
编程相关推荐