我正在尝试在以下业务代码中模拟Popen实例:
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
output = process.communicate()[0]
if process.returncode != 0:
这是我的测试代码设置:
@mock.patch('subprocess.Popen.returncode')
@mock.patch('subprocess.Popen.communicate')
def testCommandExecutesCommunicate(self, mock_popen_communicate, mock_popen_returncode):
我也尝试过:
@mock.patch('subprocess.Popen.returncode')
@mock.patch('subprocess.Popen.communicate')
def testCommandExecutesCommunicate(self, mock_popen_communicate, mock_popen_returncode):
在两种情况下,我都无法使用returncode
模拟
Error
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mock/mock.py", line 1297, in patched
arg = patching.__enter__()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mock/mock.py", line 1369, in __enter__
original, local = self.get_original()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mock/mock.py", line 1343, in get_original
"%s does not have the attribute %r" % (target, name)
AttributeError: <class 'subprocess.Popen'> does not have the attribute 'returncode'
我应该如何模拟Popen?我应该以某种方式嘲笑Popen类吗?还是我通过模拟方法和属性走上了正确的轨道?
答案 0 :(得分:1)
`AttributeError:没有属性'returncode'
查看位于subprocess.py
的代码
class Popen():
def __init__(self, ....):
self.returncode = None
属性returncode
由__init__
设置,后来由communicate()
等更新,类属性returncode
不友好,会导致错误
为什么mockp.communicate()mockp.communicate.return_value ='123'
mockp.communicate
或与()
相同,与mockp.communicate = mock.Mock()
相同,这是一种为函数communicate
创建新的模拟对象的方法
mock.communicate.return_value = list([2, 3])
用于设置模拟函数的return_value。
解决方案:我对解决方案并不完全满意,但是我倾向于认为模拟整个类'Popen()`是进行单元测试的方法。
对于单元测试,只需模拟整个类,模拟subprocess.Popen
并设置returncode
和communicate()
并设置return_value
,例如
remove_file.py
def do_cmd(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
exit_code = process.communicate()[0]
return exit_code
test_remove_file.py
class TestDoCmd(unittest.TestCase):
@mock.patch('remove_file.subprocess.Popen')
def test_do_cmd(self, mockp):
# add function
mockp.returncode.return_value = 'abc'
mockp.communicate()
mockp.communicate.return_value = '123'
file = '/tmp/none.txt'
remove_file.do_cmd('rm -f {}'.format(file))
mockp.assert_called_with('rm -f /tmp/none.txt', shell=True, stdout=-1)