[英]Python mock patch a coroutine function within another function using pytest?
I have two functions in module_name/app.py 我在module_name / app.py中有两个函数
async def f1(arg):
# do something
return arg + '1'
async def f2(arg):
result = await f1(arg)
return result
I try to test f2 and mock f1 using pytest and asynctest. 我尝试使用pytest和asynctest测试f2并模拟f1。
It only works, if I do 它只会起作用,如果我这样做的话
def sf_f1(arg):
return 'some value'
@pytest.mark.asyncio
async def test_f2():
with asynctest.mock.patch('module_name.app.f1', side_effect=sf_f1):
assert 'some value' == await f2('test')
test PASSED 通过测试
But, I want do to something like this 但是,我想做到这样的事情
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')
I get 我明白了
assert 'some value' == await f2('test')
AssertionError: assert 'some value' == 'test1'
- some value
+ test1
Why doesn't the second way work? 为什么第二种方式不起作用?
In your second example, in mock_f1
fixture you create a CoroutineMock
object and return it. 在第二个示例中,在
mock_f1
fixture中创建一个CoroutineMock
对象并返回它。 But you don't overwrite module_name.app.f1
function: Mock
-like objects don't patch anything automatically. 但是你没有覆盖
module_name.app.f1
函数: Mock
类对象不会自动修补任何东西。
Here is an illustrating addition to your example: 以下是对您的示例的说明:
@pytest.mark.asyncio
async def test_f2_2(mock_f1):
print('fixture value:', mock_f1)
print('actual module_name.app.f1 function:', module_name.app.f1)
assert 'some value' == await f2('test')
Which will print somethink like this 哪个会打印出这样的想法
fixture value: <CoroutineMock spec='function' id='139756096130688'>
actual module_name.app.f1 function: <function f1 at 0x7f1b7e1139d8>
When you call f2
, it uses f1
function from the module, which is not overriden. 当你调用
f2
,它使用模块中的f1
函数,它不被覆盖。
So here is how this would work for you: 这就是这对你有用的方式:
@pytest.fixture
def mock_f1(monkeypatch):
fake_f1 = asynctest.CoroutineMock(module_name.app.f1, side_effect=sf_f1)
monkeypatch.setattr(module_name.app, 'f1', fake_f1)
return fake_f1
As you probably know, monkeypatch
will make sure that changes are applied only while the fixture is active. 您可能知道,
monkeypatch
将确保仅在灯具处于活动状态时应用更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.