我在一个使用子进程模块运行Photoshop脚本的类中有一个Python函数。
def runPhotoshop(self):
photoshop = r'C:\Program Files\Adobe\Adobe Photoshop CC 2014\Photoshop.exe'
extendscript = os.path.join(os.getcwd(), 'someScript.jsx')
returnCode = subprocess.call([photoshop, extendscript])
return returnCode
每次启动单元测试时启动Photoshop都很昂贵,所以我决定使用mock。 MyTool类是先前从mytool模块导入的。
@mock.patch('mytool.subprocess')
def test_run_tool(self, mock_subprocess):
# Need instance inside the test for patch to work
tool = MyTool()
# Patch in a mock subprocess module
mock_subprocess.call.return_value = 0
# Run the function and test the return value
returnCode = mytool.runPhotoshop()
self.assertEqual(returnCode, 0, "Javascript was not successful")
# Was the subprocess function called?
photoshop = r'C:\Program Files\Adobe\Adobe Photoshop CC 2014\Photoshop.exe'
extendscript = os.path.join(os.getcwd(), 'someScript.jsx')
mock_subprocess.call.assert_called_with([photoshop, extendscript])
当从命令行(Windows Powershell)运行时,此测试工作正常,而Photoshop永远不会启动,这是我想要的。
In method test_add_metadata_to_image_file
.
----------------------------------------------------------------------
Ran 1 test in 0.016s
OK
然而,当我在Eclipse Helios的PyDev插件中运行相同的单元测试时,不仅Photoshop启动,而且我得到一个断言错误:
AssertionError: Expected call: call(['C:\\Program Files\\Adobe\\Adobe Photoshop CC 2014\\Photoshop.exe', 'D:\\work_directory\\someScript.jsx'])
Not called
这不是我第一次注意到在PyDev中运行单元测试与命令行之间的区别。事实上,我正在考虑不再在PyDev中运行测试,只是坚持命令行(减少变量)。但是,我想知道发生了什么。