Skip to content

Commit

Permalink
If run_if throws exception (not intentionally), test will terminate w…
Browse files Browse the repository at this point in the history
…ith pass outcome (#1203)

* Allow graceful termination if run_if throws exception

* Returning exception to make sure test is stopped on error

* Fixed indentation

* Using htf_test for unit testing

* using assertTestError
  • Loading branch information
eli-zr authored Dec 11, 2024
1 parent f8e3885 commit 85f5d72
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
18 changes: 14 additions & 4 deletions openhtf/core/phase_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 18 additions & 1 deletion test/core/phase_executor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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)

0 comments on commit 85f5d72

Please sign in to comment.