我如何为下面的run zip方法编写一个unittest?我正在努力
正确使用mock。我读过一些stackoverflow解决方案和
文档。我通过中的pydev模块在Linux上使用python3.6
日蚀py.测试作为测试者。在
请在下面找到我的最佳猜测,以及输出。我的测试是基于
stackoverflow答案:import unittest.mock as mock
SEVENZIPPATH =
def run_7zip(input_file, output_file):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
arglist = [SEVENZIPPATH, "a", "-t7z", "-mx9","-aoa",output_file,input_file]
process = subprocess.Popen(args = arglist, shell = False, startupinfo = startupinfo)
output,err = process.communicate()
return process.returncode
@mock.patch('subprocess.Popen')
def test_run_7zip(mock_subproc_popen):
process_mock = mock.Mock()
attrs = {'communicate.return_value': ('output', 'error')}
process_mock.configure_mock(**attrs)
mock_subproc_popen.return_value = process_mock
assert mock_subproc_popen.called is True
输出(截断):
^{pr2}$
在de1的有益评论之后,我将传递以下内容:@mock.patch('subprocess.Popen')
def test_run_7zip(mock_subproc_popen):
process_mock = mock.Mock()
attrs = {'communicate.return_value': ('output', 'error')}
process_mock.configure_mock(**attrs)
mock_subproc_popen.return_value = process_mock
run_7zip('path1', 'path2')
assert mock_subproc_popen.called is True