在 Python 中模拟子进程调用

Mocking a subprocess call in Python(在 Python 中模拟子进程调用)
本文介绍了在 Python 中模拟子进程调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个方法 (run_script) 想要测试.具体来说,我想测试对 subprocess.Popen 的调用是否发生.测试是否使用某些参数调用 subprocess.Popen 会更好.但是,当我运行测试时,我得到 TypeError: 'tuple' object is not callable.

I have a method (run_script) would like to test. Specifically I want to test that a call to subprocess.Popenoccurs. It would be even better to test that subprocess.Popen is called with certain parameters. When I run the test however I get TypeError: 'tuple' object is not callable.

如何测试我的方法以确保 subprocess 确实是使用模拟调用的?

How can I test my method to ensure that subprocess is actually being called using mocks?

@mock.patch('subprocess.Popen')
def run_script(file_path):
  process = subprocess.Popen(['myscript', -M, file_path], stdout=subprocess.PIPE)
  output,err = process.communicate()
  return process.returncode

def test_run_script(self, mock_subproc_popen):
  mock_subproc_popen.return_value = mock.Mock(communicate=('ouput','error'), returncode=0)
  am.account_manager("path")
  self.assertTrue(mock_subproc_popen.called)

推荐答案

你在 run_script 函数上使用补丁装饰器对我来说似乎很不寻常,因为你没有在那里传递模拟参数.

It seems unusual to me that you use the patch decorator over the run_script function, since you don't pass a mock argument there.

这个怎么样:

def run_script(file_path):
  process = subprocess.Popen(['myscript', -M, file_path], stdout=subprocess.PIPE)
  output,err = process.communicate()
  return process.returncode

@mock.patch('subprocess.Popen')
def test_run_script(self, mock_subproc_popen):
  process_mock = mock.Mock()
  attrs = {'communicate.return_value': ('output', 'error')}
  process_mock.configure_mock(**attrs)
  mock_subproc_popen.return_value = process_mock 
  am.account_manager("path") # this calls run_script somewhere, is that right?
  self.assertTrue(mock_subproc_popen.called)

现在,您模拟的 subprocess.Popen 似乎返回一个元组,导致 process.communicate() 引发 TypeError: 'tuple' object is not callable..所以最重要的是将mock_subproc_popen上的return_value弄的恰到好处.

Right now, your mocked subprocess.Popen seems to return a tuple, causeing process.communicate() to raise TypeError: 'tuple' object is not callable.. Therefore it's most important to get the return_value on mock_subproc_popen just right.

这篇关于在 Python 中模拟子进程调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)