Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何模拟函数中的特定行?_Python_Pytest - Fatal编程技术网

Python 如何模拟函数中的特定行?

Python 如何模拟函数中的特定行?,python,pytest,Python,Pytest,我想为我的函数编写测试,但不想执行其中的特定语句。我正在尝试模拟,但失败。任何建议都会有帮助。我正在使用pytest mock进行模拟 def listDataSetZOWE(): """To list out all datasets in system""" try: process = Popen([zowe_path, 'zos-files','list','data-set','XYZ*'], stdout=PIPE, stderr=PIPE, universal_newli

我想为我的函数编写测试,但不想执行其中的特定语句。我正在尝试模拟,但失败。任何建议都会有帮助。我正在使用pytest mock进行模拟

def listDataSetZOWE():
"""To list out all datasets in system"""
try:

    process = Popen([zowe_path, 'zos-files','list','data-set','XYZ*'], stdout=PIPE, stderr=PIPE, universal_newlines=True)
    stdout, stderr = process.communicate()
    print(stdout)
    process.kill()

except Exception as e:
    print(e)
    stderr=e

return stderr
我不希望下面两行在测试时被执行

 process = Popen([zowe_path, 'zos-files','list','data-set','XYZ*'], stdout=PIPE, stderr=PIPE, universal_newlines=True)
    stdout, stderr = process.communicate()
我的测试:

def test_listDataSetZOWE(mocker):
    mocker.patch("process = Popen([zowe_path, 'zos-files','list','data-set','MACK00*'], stdout=PIPE, stderr=PIPE, universal_newlines=True)","datasetobj")
    mocker.patch("stdout, stderr = process.communicate()","stderr")
    zowe.listDataSetZOWE()
其失败原因如下:

 TypeError: Need a valid target to patch. You supplied: "process = Popen([zowe_path, 'zos-files','list','data-set','MACK00*'], stdout=PIPE, stderr=PIPE, universal_newlines=True)"

只需修补
subprocess.Popen
maybe?在最坏的情况下,您可以将这些行提取到一个私有函数中,该函数返回(stdout,stderr)元组。然后可以模拟新函数。或者你可以嘲笑波本。@Thathappingdude不工作。同样的错误。