模拟subprocess.Popen依赖于导入样式 [英] mocking subprocess.Popen dependant on import style

查看:161
本文介绍了模拟subprocess.Popen依赖于导入样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试模拟Popen时,如果子过程的导入在单元测试代码和主模块代码中都匹配,我只能使它成功.

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.

给出以下模块listdir.py:

Given following module listdir.py:

from subprocess import Popen, PIPE

def listdir(dir):
    cmd = ['ls', dir]
    pc = Popen(cmd, stdout=PIPE, stderr=PIPE)
    out, err = pc.communicate()
    if pc.returncode != 0:
        raise Exception
    return out

以及以下单元测试代码test_listdir.py

and following unit test code test_listdir.py

import subprocess
import listdir
import mock

@mock.patch.object(subprocess, 'Popen', autospec=True)
def test_listdir(mock_popen):
    mock_popen.return_value.returncode = 0
    mock_popen.return_value.communicate.return_value = ("output", "Error")
    listdir.listdir("/fake_dir")

由于两个python模块之间的导入样式不同,由于某些原因未对Popen进行模拟,因此运行测试始终会引发异常.

For some reason Popen is not being mocked, due to the import style being different between the two python modules, and running the test always raises an exception.

如果我将listdir.py更改为导入所有子过程,例如

If I change listdir.py to import all of subproces e.g.

import subprocess

def listdir(dir):
    cmd = ['ls', dir]
    pc = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
                          stderr=subprocess.PIPE)
    out, err = pc.communicate()
    if pc.returncode != 0:
        raise ListingErrorException
    return out

然后在测试中返回输出".

Then "output" is returned in the test.

任何人都想弄清楚为什么,我的偏好是 从子流程导入Popen,在两个模块中都使用Pipe,但是我只是无法模拟它.

Anyone care to shed some light on why, my preference would be to have from subprocess import Popen, Pipe in both modules, but I just can't get that to mock.

推荐答案

您需要在listdir中修补Popen的副本,而不是刚刚导入的副本.因此,请尝试使用@mock.patch.object(listdir, 'Popen', autospec=True)

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)

有关更多信息,请参阅此文档: http://www.voidspace. org.uk/python/mock/patch.html#where-to-patch

See this doc for more info: http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch

这篇关于模拟subprocess.Popen依赖于导入样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆