1 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以使用unittest' 的功能來斷言您是否期望不需要模擬的異常:
import unittest
import sys
class ToTest:
def foo(self):
raise SystemExit(1)
def bar(self):
sys.exit(1)
def foo_bar(self):
print("This is okay")
return 0
class Test(unittest.TestCase):
def test_1(self):
with self.assertRaises(SystemExit) as cm:
ToTest().foo()
self.assertEqual(cm.exception.code, 1)
def test_2(self):
with self.assertRaises(SystemExit) as cm:
ToTest().bar()
self.assertEqual(cm.exception.code, 1)
def test_3(self):
self.assertEqual(ToTest().foo_bar(), 0)
添加回答
舉報(bào)