您可以使用Exception
asside_effect
来模拟sys.exit
行为而无需退出测试。
side_effect
文件说:
这可以是调用模拟时要调用的函数、可迭代的或要引发的异常(类或实例)。
因此,您不能使用or之类的语句,但您想做的是退出运行周期,这可以通过引发异常来获得……我希望您不要在线程的主周期中使用 wild - 。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())