0

I have a Python function inside of a class that runs a Photoshop script with the subprocess module.

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

Firing up Photoshop every time I fire off a unit test is expensive, so I decided to use mock. The MyTool class was imported from the mytool module earlier.

@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])

This test works perfectly when ran from the command line (a Windows Powershell), and Photoshop never launches which is what I want..

In method test_add_metadata_to_image_file
.
----------------------------------------------------------------------
Ran 1 test in 0.016s

OK

However, when I run the same unit test inside the PyDev plugin for Eclipse Helios, not only does Photoshop launches, but I get an assertion error:

AssertionError: Expected call: call(['C:\\Program Files\\Adobe\\Adobe Photoshop CC 2014\\Photoshop.exe', 'D:\\work_directory\\someScript.jsx'])
Not called

This is not the first time I noticed a difference between running a unit test in PyDev versus the command line. In fact, I am considering not running tests anymore in PyDev and just stick to the command line (less variables). However, I would like to have an idea of what is going on.

Christopher Spears
  • 1,105
  • 15
  • 32
  • When you use PyDev you run JUST this test or a more complex test suite? Maybe in `mytool` there is some kind of lazy initialization then when you patch `mytool.subprocess` the reference that will use `subprocess` to to do the call is already recorded and cannot be changed. Take care that either if both command line and PyDev run the same suite, you can have different results for different tests orders. IFAIK there is no know bugs on use `patch` in PyDev and I cannot imagine how can be possible to have it: PyDev simply create a runner and catch the exception to record. – Michele d'Amico Nov 09 '15 at 12:59
  • I am running the test as part of a suite. Good call. I'll go check the code. – Christopher Spears Nov 09 '15 at 17:12

0 Answers0