[英]how to unit test subprocess.call() to check is it called in python
Hey I am trying to write unittest to check if subprocess.call()
called in the function flash_it that does not return anything.嘿,我正在尝试编写 unittest 来检查 subprocess.call
subprocess.call()
是否在 function flash_it 中调用,它不返回任何内容。
def flash_it('flash.bat'):
file = open('file.txt', 'w')
subprocess.call(['flash.bat', '-d'], stdout = file, stderr = subprocess.STDOUT)
@mock.patch('subprocess.call')
def test_subprocess_call_passed(self, mock_subproc_call):
main.flash_it('flash.bat')
self.assertTrue(mock_subproc_call.called)
You probably need something like this:你可能需要这样的东西:
import subprocess
import mock
def flash_it(filename):
with open(filename, 'w') as f:
subprocess.call(['cmd to flash', '-d'], stdout=f, stderr=subprocess.STDOUT)
@mock.patch('subprocess.call')
def test_subprocess_call_passed(mock_subproc_call):
flash_it('flash.bat')
mock_subproc_call.assert_called_once()
Note that I didn't actually run that.请注意,我实际上并没有运行它。
Of course there is also assert_called_once_with
that lets you assert what parameters were passed to function.当然还有
assert_called_once_with
可以让你断言哪些参数被传递给了 function。 Check documentation: https://docs.python.org/3/library/unittest.mock.html检查文档: https://docs.python.org/3/library/unittest.mock.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.