mocking subprocess.Popen dependant on import style

后端 未结 1 323
滥情空心
滥情空心 2021-01-13 01:57

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

相关标签:
1条回答
  • 2021-01-13 02:25

    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

    0 讨论(0)
提交回复
热议问题