diff --git a/openhtf/core/phase_executor.py b/openhtf/core/phase_executor.py index d0eca616..f68924cb 100644 --- a/openhtf/core/phase_executor.py +++ b/openhtf/core/phase_executor.py @@ -303,10 +303,20 @@ def _execute_phase_once( ) -> Tuple[PhaseExecutionOutcome, Optional[pstats.Stats]]: """Executes the given phase, returning a PhaseExecutionOutcome.""" # Check this before we create a PhaseState and PhaseRecord. - if phase_desc.options.run_if and not phase_desc.options.run_if(): - self.logger.debug('Phase %s skipped due to run_if returning falsey.', - phase_desc.name) - return PhaseExecutionOutcome(phase_descriptor.PhaseResult.SKIP), None + if phase_desc.options.run_if: + try: + run_phase = phase_desc.options.run_if() + except Exception: # pylint: disable=broad-except + self.logger.debug('Phase %s stopped due to a fault in run_if function.', + phase_desc.name) + # Allow graceful termination + return PhaseExecutionOutcome(ExceptionInfo(*sys.exc_info())), None + + if not run_phase: + self.logger.debug('Phase %s skipped due to run_if returning falsey.', + phase_desc.name) + return PhaseExecutionOutcome(phase_descriptor.PhaseResult.SKIP), None + override_result = None with self.test_state.running_phase_context(phase_desc) as phase_state: diff --git a/test/core/phase_executor_test.py b/test/core/phase_executor_test.py index 86379568..9f2b2c35 100644 --- a/test/core/phase_executor_test.py +++ b/test/core/phase_executor_test.py @@ -18,7 +18,8 @@ import openhtf from openhtf.core import phase_descriptor - +from openhtf.core import test_record +from openhtf.util import test as htf_test class PhaseExecutorTest(unittest.TestCase): @@ -54,3 +55,19 @@ def test_execute_phase_with_repeat_limit_max_exceeds_default_limit(self): openhtf.PhaseOptions(repeat_limit=phase_descriptor.MAX_REPEAT_LIMIT), expected_call_count=phase_descriptor.DEFAULT_REPEAT_LIMIT + 1, ) + + +class PhaseExecuterRunIfTest(htf_test.TestCase): + + def test_execute_phase_when_run_if_throws_exception(self): + + def run_if_with_exception(): + raise Exception("run_if_with_exception") + + def phase_excp_run_if(): + pass + + phase = openhtf.PhaseOptions(run_if=run_if_with_exception)( + phase_excp_run_if) + record = self.execute_phase_or_test(openhtf.Test(phase)) + self.assertTestError(record)