У меня есть объект, созданный в тестовом примере, и я хочу сделать тест внутри его метода. Но исключение является проглоченным предложением try-Кроме. Я знаю, что могу изменить повышение исключения в run
, но это не то, что я хочу. Есть ли способ, которым любой инструмент unittest может справиться с этим?
Кажется, что assertTrue
метод unittest.TestCase
- это просто тривиальное утверждение assert.
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)