You can use side_effect
patch
attribute to do it:
>>> 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'
>>>
I'm quite sure that you can find it little bit harder compared to flexmock
's syntax, but what you need is more a stub than a mock. If you can design your test to be isolated and configure your mock/stub in some setup stage maybe you will find this syntax good and all mock
asserts and patch
options very powerful.