您需要在listdir中修补Popen的副本,而不是刚刚导入的副本。因此
@mock.patch.object(subprocess, 'Popen',autospec=True),请尝试
@mock.patch.object(listdir, 'Popen', autospec=True)
有关更多信息,请参见此文档:http : //www.voidspace.org.uk/python/mock/patch.html#where-to-
patch

您需要在listdir中修补Popen的副本,而不是刚刚导入的副本。因此
@mock.patch.object(subprocess, 'Popen',autospec=True),请尝试
@mock.patch.object(listdir, 'Popen', autospec=True)
有关更多信息,请参见此文档:http : //www.voidspace.org.uk/python/mock/patch.html#where-to-
patch