这是我的代码,我编写了一个decorater,它会引发一个RuntimeError,在testcase中,我使用assertRaises来确保引发了异常。但是当我运行程序时,程序仍然会被异常中断。我认为它应该告诉我测试用例通过了!
import unittest
import decorators
def not_completed(func):
"""
Prevent user from calling not fully implemented functions.
"""
raise RuntimeError("This function is not fully implemented: {}".format(func.__name__))
@not_completed
def test_not_completed(a, b):
print("invoke test", a+b)
class TestDecorators(unittest.TestCase):
def test_not_completed(self):
with self.assertRaises(RuntimeError):
decorators.test_not_completed(1, 3)
这是结果输出:
File "E:\nutstore\Temp\hhw_dev_tools\decorators.py", line 8, in <module> @not_completed File "E:\nutstore\Temp\hhw_dev_tools\decorators.py", line 5, in not_completed raise RuntimeError("This function is not fully implemented: {}".format(func.__name__)) RuntimeError: This function is not fully implemented: test_not_completed
答案 0 :(得分:1)
因为not_completed
在导入时间内运行
@not_completed
def test_not_completed(a, b):
print("invoke test", a+b)
是
def test_not_completed(a, b):
print("invoke test", a+b)
test_not_completed = not_completed(test_not_completed)
因此not_completed没有在资源管理器中运行