MagicMock根据输入返回值



我正在尝试将测试从flexmock重构到mock。给定flexmock的以下语法:

flexmock(subprocess).should_receive('check_output').with_args('ls /').and_return(output)

如何使用Mock重写此内容?特别是,如何使用Mock将返回值固定到特定输入?

您可以使用side_effect patch属性来执行此操作:

>>> from unittest.mock import *
>>> root = '''bin   cdrom  etc   initrd.img  lib32  libx32      media  opt   root  sbin  sys  usr  vmlinuz
... boot  dev    home  lib         lib64  lost+found  mnt    proc  run   srv   tmp  var'''
>>> answer = {'ls /': root}
>>> import subprocess
>>> with patch('subprocess.check_output', side_effect=lambda arg, *args, **kwargs: answer[arg]) as mock_check_output :
...     assert root == subprocess.check_output('ls /')
...     mock_check_output.assert_called_with('ls /')
...     subprocess.check_output('something else')
... 
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/usr/lib/python3.4/unittest/mock.py", line 896, in __call__
    return _mock_self._mock_call(*args, **kwargs)
  File "/usr/lib/python3.4/unittest/mock.py", line 962, in _mock_call
    ret_val = effect(*args, **kwargs)
  File "<stdin>", line 1, in <lambda>
KeyError: 'something else'
>>> 

我敢肯定,与flexmock的语法相比,您会发现它有点难,但您需要的更多的是一个stub而不是mock。如果您可以将测试设计为隔离的,并在某个设置阶段配置mock/stub,那么您可能会发现这种语法很好,并且所有mock断言和patch选项都非常强大。

相关内容

  • 没有找到相关文章

最新更新