我有一个多处理应用程序,它的工作程序在“while True”循环中运行。为了进行测试,我希望能够以如下方式模拟 sys.exit():
with mock.patch('sys.exit') as sys_mock:
sys_mock.side_effect = break
或
with mock.patch('sys.exit') as sys_mock:
sys_mock.side_effect = return
这样我就可以跳出循环并完成我的测试。这些都不起作用,但有没有不同的方法来完成我想要完成的事情?
请您参考如下方法:
您可以使用 Exception 作为 side_effect 来模拟 sys.exit 行为而不退出测试。
side_effect文档说:
This can either be a function to be called when the mock is called, an iterable or an exception (class or instance) to be raised.
所以你不能使用像break 或return 这样的语句,但你想要做的是退出你的运行周期,这样可以通过引发异常获得...我希望您不要在线程的主循环中使用疯狂的 try-except。
我写了一个简单的例子来测试它,我使用装饰器patch语法并放置内联side_effect=Exception,使测试更具可读性:
import sys
import threading
import unittest
from unittest.mock import patch
class T(threading.Thread):
def __init__(self, *args, **kwargs):
super(T, self).__init__(*args, **kwargs)
self._interrupt = threading.Event()
self.started = threading.Event() #Used to be sure that we test run() behavior
self.started.clear()
self.terminated = False
def interrupt(self):
self._interrupt.set()
def run(self, *args, **kwargs):
self._interrupt.clear()
self.started.set()
while not self._interrupt.is_set():
self._interrupt.wait(timeout=1)
self.terminated = True
sys.exit()
class TestInterrupt(unittest.TestCase):
@patch("sys.exit", side_effect=Exception("Ignore it... just close thread"))
def test_interrupt(self, mock_sys_exit):
t = T()
t.start()
if not t.started.is_set():
t.started.wait(timeout=0.2)
self.assertTrue(t.started.is_set(), "t not started!")
#Ok t is in run() main cycle: we can test interrupt
t.interrupt()
t.join(0.1)
self.assertTrue(t.terminated)
self.assertFalse(t.isAlive())
