Skip to content

If run_if throws exception (not intentionally), test will terminate with pass outcome #1203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 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.',
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please line up the start of this line with the opening parenthesis on the previous line. Make sure you're using spaces and not tabs to do the alignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return PhaseExecutionOutcome(phase_descriptor.PhaseResult.SKIP), None
# 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
20 changes: 20 additions & 0 deletions test/core/phase_executor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import openhtf
from openhtf.core import phase_descriptor
from openhtf.core import test_record


class PhaseExecutorTest(unittest.TestCase):
Expand Down Expand Up @@ -54,3 +55,22 @@ 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,
)

def test_execute_phase_when_run_if_throws_exception(self):

def run_if_with_exception():
raise Exception("run_if_with_exception")

@openhtf.PhaseOptions(run_if=run_if_with_exception)
def phase():
pass

_tr = None
def final(tr):
nonlocal _tr
_tr = tr

test = openhtf.Test(phase)
test.add_output_callbacks(final)
test.execute()
self.assertEqual(_tr.outcome, test_record.Outcome.ERROR)