When attempting to mock Popen I can only get it to succeed if the importing of subprocess matches in both unit test code and main module code.
Given following module
You need to patch the copy of Popen in listdir, not the one you just imported. So, instead of @mock.patch.object(subprocess, 'Popen', autospec=True)
, try @mock.patch.object(listdir, 'Popen', autospec=True)
See this doc for more info: http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch