Skip to content
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

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

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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.',
glados-verma marked this conversation as resolved.
Show resolved Hide resolved
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)
Loading