Skip to content

Commit dc22c0c

Browse files
OpenHTF Ownerscopybara-github
authored andcommitted
Make Phase name (or any other executable unit name) known in finalize_from_phase_outcome so that we can mention it on the finalization message.
I also updated all the other error messages in finalize_from_test_outcome to use the phase name. PiperOrigin-RevId: 508689999
1 parent e1052fe commit dc22c0c

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

openhtf/core/test_executor.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def __init__(self, test_descriptor: 'test_descriptor.TestDescriptor',
108108
self._phase_exec = None # type: Optional[phase_executor.PhaseExecutor]
109109
self.uid = execution_uid
110110
self._last_outcome = None # type: Optional[phase_executor.PhaseExecutionOutcome]
111+
self._last_execution_unit: str = None
111112
self._abort = threading.Event()
112113
self._full_abort = threading.Event()
113114
# This is a reentrant lock so that the teardown logic that prevents aborts
@@ -235,6 +236,7 @@ def _initialize_plugs(
235236
# Record the equivalent failure outcome and exit early.
236237
self._last_outcome = phase_executor.PhaseExecutionOutcome(
237238
phase_executor.ExceptionInfo(*sys.exc_info()))
239+
self._last_execution_unit = 'Plugs Initialization'
238240
return True
239241

240242
def _execute_test_start(self) -> bool:
@@ -264,6 +266,7 @@ def _execute_test_start(self) -> bool:
264266

265267
if outcome.is_terminal:
266268
self._last_outcome = outcome
269+
self._last_execution_unit = 'TestStart'
267270
return True
268271

269272
if self.test_state.test_record.dut_id is None:
@@ -296,7 +299,9 @@ def _execute_test_teardown(self) -> None:
296299
self.logger.debug('Finishing test with outcome ABORTED.')
297300
self.test_state.abort()
298301
elif self._last_outcome and self._last_outcome.is_terminal:
299-
self.test_state.finalize_from_phase_outcome(self._last_outcome)
302+
self.test_state.finalize_from_phase_outcome(
303+
self._last_outcome, self._last_execution_unit
304+
)
300305
else:
301306
self.test_state.finalize_normally()
302307

@@ -333,6 +338,7 @@ def _execute_phase(self, phase: phase_descriptor.PhaseDescriptor,
333338
if outcome.is_terminal:
334339
if not self._last_outcome:
335340
self._last_outcome = outcome
341+
self._last_execution_unit = phase.name
336342
return _ExecutorReturn.TERMINAL
337343

338344
if outcome.is_fail_subtest:
@@ -354,6 +360,7 @@ def _execute_checkpoint(self, checkpoint: phase_branches.Checkpoint,
354360
if outcome.is_terminal:
355361
if not self._last_outcome:
356362
self._last_outcome = outcome
363+
self._last_execution_unit = checkpoint.name
357364
return _ExecutorReturn.TERMINAL
358365

359366
if outcome.is_fail_subtest:
@@ -600,6 +607,7 @@ def _execute_test_diagnoser(
600607
# Record the equivalent failure outcome and exit early.
601608
self._last_outcome = phase_executor.PhaseExecutionOutcome(
602609
phase_executor.ExceptionInfo(*sys.exc_info()))
610+
self._last_execution_unit = diagnoser.name
603611

604612
def _execute_test_diagnosers(self) -> None:
605613
for diagnoser in self._test_options.diagnosers:

openhtf/core/test_state.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,9 @@ def set_status_running(self) -> None:
382382

383383
def finalize_from_phase_outcome(
384384
self,
385-
phase_execution_outcome: phase_executor.PhaseExecutionOutcome) -> None:
385+
phase_execution_outcome: phase_executor.PhaseExecutionOutcome,
386+
phase_name: str,
387+
) -> None:
386388
"""Finalize due to the given phase outcome."""
387389
if self._is_aborted():
388390
return
@@ -400,39 +402,49 @@ def finalize_from_phase_outcome(
400402
self.test_record.add_outcome_details(code, description)
401403
if self._outcome_is_failure_exception(phase_execution_outcome):
402404
self.state_logger.error(
403-
'Outcome will be FAIL since exception was of type %s',
404-
phase_execution_outcome.phase_result.exc_val)
405+
f'Outcome of {phase_name} will be FAIL since exception was of type'
406+
' {phase_execution_outcome.phase_result.exc_val}'
407+
)
405408
self._finalize(test_record.Outcome.FAIL)
406409
else:
407410
self.state_logger.critical(
408-
'Finishing test execution early due to an exception raised during '
409-
'phase execution; outcome ERROR.')
411+
f'Finishing test execution of {phase_name} early due to an '
412+
'exception raised during phase execution; outcome ERROR.'
413+
)
410414
# Enable CLI printing of the full traceback with the -v flag.
411415
if isinstance(result, phase_executor.ExceptionInfo):
412416
self.state_logger.critical(
413-
'Traceback:%s%s%s%s',
417+
'Traceback:%s%s%s%s\n in executing %s',
414418
os.linesep,
415419
phase_execution_outcome.phase_result.get_traceback_string(),
416420
os.linesep,
417421
description,
422+
phase_name,
418423
)
419424
else:
420425
self.state_logger.critical(
421-
'Description:%s',
422-
description,
426+
f'Description:{description}, PhaseName:{phase_name}'
423427
)
424428
self._finalize(test_record.Outcome.ERROR)
425429
elif phase_execution_outcome.is_timeout:
426-
self.state_logger.error('Finishing test execution early due to '
427-
'phase timeout, outcome TIMEOUT.')
428-
self.test_record.add_outcome_details('TIMEOUT',
429-
'A phase hit its timeout.')
430+
self.state_logger.error(
431+
'Finishing test execution early due to '
432+
f'phase {phase_name} experiencing timeout, '
433+
'outcome TIMEOUT.'
434+
)
435+
self.test_record.add_outcome_details(
436+
'TIMEOUT', f'Phase {phase_name} hit its timeout.'
437+
)
430438
self._finalize(test_record.Outcome.TIMEOUT)
431439
elif phase_execution_outcome.phase_result == openhtf.PhaseResult.STOP:
432-
self.state_logger.error('Finishing test execution early due to '
433-
'PhaseResult.STOP, outcome FAIL.')
434-
self.test_record.add_outcome_details('STOP',
435-
'A phase stopped the test run.')
440+
self.state_logger.error(
441+
'Finishing test execution early due to '
442+
f'{phase_name} causing PhaseResult.STOP, '
443+
'outcome FAIL.'
444+
)
445+
self.test_record.add_outcome_details(
446+
'STOP', f'Phase {phase_name} stopped the test run.'
447+
)
436448
self._finalize(test_record.Outcome.FAIL)
437449

438450
def finalize_normally(self) -> None:

test/test_state_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def test_test_state_cache(self):
243243
def test_test_state_finalize_from_phase_outcome(
244244
self, phase_exe_outcome: phase_executor.PhaseExecutionOutcome,
245245
test_record_outcome: test_record.Outcome):
246-
self.test_state.finalize_from_phase_outcome(phase_exe_outcome)
246+
self.test_state.finalize_from_phase_outcome(phase_exe_outcome, 'MyPhase')
247247
self.assertEqual(self.test_state.test_record.outcome, test_record_outcome)
248248

249249
def test_test_state_finalize_from_phase_outcome_exception_info(self):
@@ -252,6 +252,6 @@ def test_test_state_finalize_from_phase_outcome_exception_info(self):
252252
except ValueError:
253253
phase_exe_outcome = phase_executor.PhaseExecutionOutcome(
254254
phase_executor.ExceptionInfo(*sys.exc_info()))
255-
self.test_state.finalize_from_phase_outcome(phase_exe_outcome)
255+
self.test_state.finalize_from_phase_outcome(phase_exe_outcome, 'MyPhase')
256256
self.assertEqual(self.test_state.test_record.outcome,
257257
test_record.Outcome.ERROR)

0 commit comments

Comments
 (0)