Python 2.x:如何模拟子流程。如果stdin = PIPE],请打开

问题描述 投票:0回答:1

我正在尝试模拟以下功能

def run_query():
    sql_cmd = "Some Query"
    process = Popen(["sqlplus", "-S", "/", "as", "sysdba"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
    process.stdin.write(sql_cmd)
    (process_stdout, process_stderr) = process.communicate()

下面是我编写的测试函数:

@patch('subprocess.Popen')
def test_run_query(Popen):
    Popen.return_value.communicate.return_value = (2, 1)

但是,我遇到了错误

Error occured while running sql command
Error output:
[Errno 2] No such file or directory
F

我尝试了其他stackoverflow帖子,但是这种示例不存在。请帮忙。

python python-2.x python-mock
1个回答
0
投票

您在错误的地方打补丁。

您需要在使用它的名称空间中修补Popen

@patch('mypackage.mymodule.Popen')
def test_run_query(Popen):
    Popen.return_value.communicate.return_value = "2", "1"

其中“ mypackage / mymodule.py”

© www.soinside.com 2019 - 2024. All rights reserved.