我有一个在测试用例中创建的对象,并且想要在其方法内部进行测试。
但是,异常被try-except子句吞没了。
我知道我可以更改run
中的异常,但这不是我想要的。任何单元测试工具都可以解决此问题吗?
似乎assertTrue
的{{1}}方法只是一个简单的assert子句。
unittest.TestCase
编辑
为清楚起见,我将真实的测试用例粘贴到此处。这里最棘手的事情是class TestDemo(unittest.TestCase):
def test_a(self):
test_case = self
class NestedProc:
def method1(self):
print("flag show the method is running")
test_case.assertTrue(False)
def run(self):
try:
self.method1()
except:
pass # can raise here to give the exception but not what I want.
NestedProc().run() # no exception raised
# NestedProc().method1() # exception raised
总是会成功,导致ParentProcess
无法正确传播到测试函数。
AssertError
答案 0 :(得分:2)
任何assert*
方法甚至fail()
都将引发异常。最简单的方法可能是手动设置一个标志,然后再手动设置fail()
:
def test_a(self):
success = True
class NestedProc:
def method1(self):
nonlocal success
success = False
raise Exception()
...
NestedProc().run()
if not success:
self.fail()