如何解决patch.multiple是否可以与pytest一起用作装饰器
我有一个https://docs.python.org/3/library/unittest.mock.html#patch-multiple采用的test_tmp.py
from unittest.mock import DEFAULT,MagicMock,patch
thing = object()
other = object()
@patch.multiple('__main__',thing=DEFAULT,other=DEFAULT)
def test_function(thing,other):
print(f'thing={thing}')
print(f'other={other}')
assert isinstance(thing,MagicMock)
assert isinstance(other,MagicMock)
test_function()
它与python一起运行
python test_tmp.py
thing=<MagicMock name='thing' id='4355085552'>
other=<MagicMock name='other' id='4355243312'>
但它不适用于pytest并显示类似错误
pytest test_tmp.py
============================================================================================================= test session starts =============================================================================================================
platform darwin -- Python 3.8.2,pytest-5.4.3,py-1.9.0,pluggy-0.13.1
rootdir: /private/tmp
collected 0 items / 1 error
=================================================================================================================== ERRORS ====================================================================================================================
________________________________________________________________________________________________________ ERROR collecting test_tmp.py _________________________________________________________________________________________________________
test_tmp.py:13: in <module>
test_function()
/Users/user/.pyenv/versions/3.8.2/lib/python3.8/unittest/mock.py:1345: in patched
with self.decoration_helper(patched,/Users/user/.pyenv/versions/3.8.2/lib/python3.8/contextlib.py:113: in __enter__
return next(self.gen)
/Users/user/.pyenv/versions/3.8.2/lib/python3.8/unittest/mock.py:1313: in decoration_helper
arg = patching.__enter__()
/Users/user/.pyenv/versions/3.8.2/lib/python3.8/unittest/mock.py:1416: in __enter__
original,local = self.get_original()
/Users/user/.pyenv/versions/3.8.2/lib/python3.8/unittest/mock.py:1389: in get_original
raise AttributeError(
E AttributeError: <module '__main__' from '/path/to/bin/pytest'> does not have the attribute 'thing'
=========================================================================================================== short test summary info ===========================================================================================================
ERROR test_tmp.py - AttributeError: <module '__main__' from '/path/to/bin/pytest'> does not have the attribute 'thing'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================================================================================================== 1 error in 0.31s ===============================================================================================================
想知道为什么吗?
我正在使用pytest =“ ^ 5.4.3”
解决方法
有些事情在pytest中不起作用:
- 您无法直接调用测试,因此无法调用该功能
- 在这种情况下装饰器版本不起作用,我想
pytest
不能实现这一点(pytest默认将测试函数参数理解为fixture;它处理patch
和{ {1}}正确,但似乎无法处理patch.object
注入的关键字参数) - 使用
patch.multiple
可能不起作用,您可以改为使用'__main__'
。
这是一个有效的版本:
sys.modules[__name__]
此版本也应与def test_function1():
with patch.multiple(sys.modules[__name__],thing=DEFAULT,other=DEFAULT) as mocks:
print(f'thing = {mocks["thing"]}')
print(f'other = {mocks["other"]}')
assert isinstance(thing,MagicMock)
assert isinstance(other,MagicMock)
一起使用。
在pytest中,您通常会将这种模拟转移到固定装置中,因此,这可能更符合pytest的精神:
unittest
注意:
这仅回答标题中的问题,而不回答“为什么”-我看过@pytest.fixture
def multiple():
with patch.multiple(sys.modules[__name__],other=DEFAULT) as mocks:
yield mocks
def test_function(multiple):
print(f'thing = {multiple["thing"]}')
print(f'other = {multiple["other"]}')
assert isinstance(thing,MagicMock)
assert isinstance(other,MagicMock)
的源代码,但不了解它如何与pytest交互。也许更有见识的人可以回答。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。