python try-except 子句中的单元测试未捕获 AssertError
AssertError not catched in unittest in python try-except clause
我在测试用例中创建了一个对象,想在它的方法内部进行测试。
但是异常被 try-except 子句吞没了。
我知道我可以更改 run
中的异常,但这不是我想要的。有没有任何单元测试工具可以处理这个问题的方法?
看来unittest.TestCase
的assertTrue
方法只是一个普通的断言子句。
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
没有正确传播到测试函数。
class TestProcess(unittest.TestCase);
@pytest.mark.asyncio
async def test_process_stack_multiple(self):
"""
Run multiple and nested processes to make sure the process stack is always correct
"""
expect_true = []
def test_nested(process):
expect_true.append(process == Process.current())
class StackTest(plumpy.Process):
def run(self):
# TODO: unexpected behaviour here
# if assert error happend here not raise
# it will be handled by try except clause in process
# is there better way to handle this?
expect_true.append(self == Process.current())
test_nested(self)
class ParentProcess(plumpy.Process):
def run(self):
expect_true.append(self == Process.current())
proc = StackTest()
# launch the inner process
asyncio.ensure_future(proc.step_until_terminated())
to_run = []
for _ in range(100):
proc = ParentProcess()
to_run.append(proc)
await asyncio.gather(*[p.step_until_terminated() for p in to_run])
for proc in to_run:
self.assertEqual(plumpy.ProcessState.FINISHED, proc.state)
for res in expect_true:
self.assertTrue(res)
任何 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()
我在测试用例中创建了一个对象,想在它的方法内部进行测试。
但是异常被 try-except 子句吞没了。
我知道我可以更改 run
中的异常,但这不是我想要的。有没有任何单元测试工具可以处理这个问题的方法?
看来unittest.TestCase
的assertTrue
方法只是一个普通的断言子句。
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
没有正确传播到测试函数。
class TestProcess(unittest.TestCase);
@pytest.mark.asyncio
async def test_process_stack_multiple(self):
"""
Run multiple and nested processes to make sure the process stack is always correct
"""
expect_true = []
def test_nested(process):
expect_true.append(process == Process.current())
class StackTest(plumpy.Process):
def run(self):
# TODO: unexpected behaviour here
# if assert error happend here not raise
# it will be handled by try except clause in process
# is there better way to handle this?
expect_true.append(self == Process.current())
test_nested(self)
class ParentProcess(plumpy.Process):
def run(self):
expect_true.append(self == Process.current())
proc = StackTest()
# launch the inner process
asyncio.ensure_future(proc.step_until_terminated())
to_run = []
for _ in range(100):
proc = ParentProcess()
to_run.append(proc)
await asyncio.gather(*[p.step_until_terminated() for p in to_run])
for proc in to_run:
self.assertEqual(plumpy.ProcessState.FINISHED, proc.state)
for res in expect_true:
self.assertTrue(res)
任何 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()