如何使用 mock 停止执行 python 程序?
How to stop execution of python program using mock?
我正在使用 unittest 和 mock 来测试如下所示的脚本
class Hi:
def call_other(self):
perform some operation
sys.exit(1)
def f(self):
try:
res = self.do_something()
a = self.something_else(res)
except Exception as e:
print(e)
call_other()
print("hi after doing something") -----> (this_print)
def process(self)
self.f()
我的测试脚本是这样的
class Test_hi(unittest.TestCase)
def mock_call_other(self):
print("called during error")
def test_fail_scenario():
import Hi class here
h = Hi()
h.process()
h.do_something = mock.Mock(retrun_value="resource")
h.something_else = mock.Mock(side_effect=Exception('failing on purpose for testing'))
h.call_other(side_effect=self.mock_call_other) -----> (this_line)
如果我不模拟 call_other
方法,它将调用 sys.exit(1) 并且会在单元测试 运行 中引起一些问题,所以,
我不想在测试期间在 call_other
中调用 sys.exit(1)。
但是,如果我像上面那样模拟 call_other
方法(在 this_line
中),它只会打印一些内容并继续执行方法 f
。意思是,它将执行打印语句(在 this_print
中)
在实际程序中不应该是这种情况,当异常被捕获时它会执行 sys.exit(1) 并停止程序。
当捕获到异常时,如何使用 unittest 和 mock 实现相同的效果我想停止执行此测试用例并继续下一个测试用例。
如何实现?请帮助
您可以使用 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)
我正在使用 unittest 和 mock 来测试如下所示的脚本
class Hi:
def call_other(self):
perform some operation
sys.exit(1)
def f(self):
try:
res = self.do_something()
a = self.something_else(res)
except Exception as e:
print(e)
call_other()
print("hi after doing something") -----> (this_print)
def process(self)
self.f()
我的测试脚本是这样的
class Test_hi(unittest.TestCase)
def mock_call_other(self):
print("called during error")
def test_fail_scenario():
import Hi class here
h = Hi()
h.process()
h.do_something = mock.Mock(retrun_value="resource")
h.something_else = mock.Mock(side_effect=Exception('failing on purpose for testing'))
h.call_other(side_effect=self.mock_call_other) -----> (this_line)
如果我不模拟 call_other
方法,它将调用 sys.exit(1) 并且会在单元测试 运行 中引起一些问题,所以,
我不想在测试期间在 call_other
中调用 sys.exit(1)。
但是,如果我像上面那样模拟 call_other
方法(在 this_line
中),它只会打印一些内容并继续执行方法 f
。意思是,它将执行打印语句(在 this_print
中)
在实际程序中不应该是这种情况,当异常被捕获时它会执行 sys.exit(1) 并停止程序。
当捕获到异常时,如何使用 unittest 和 mock 实现相同的效果我想停止执行此测试用例并继续下一个测试用例。
如何实现?请帮助
您可以使用 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)