Skip to content

Commit

Permalink
bug(QAWTO-212): fix action before the feature with background output …
Browse files Browse the repository at this point in the history
…with error
  • Loading branch information
ricardogarfe committed Jun 28, 2024
1 parent 63188db commit 6afb8b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 14 additions & 6 deletions toolium/behave/env_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import sys
import warnings

# Behave is an optional dependency in toolium, so it is imported here
from behave.model_core import Status

# Actions types defined in feature files
ACTIONS_BEFORE_FEATURE = 'actions before the feature'
ACTIONS_BEFORE_SCENARIO = 'actions before each scenario'
Expand Down Expand Up @@ -279,7 +282,7 @@ def execute_after_scenario_steps(self, context):
self.scenario_error = False
self.before_error_message = None
context.scenario.reset()
self.fail_first_step_precondition_exception(context.scenario)
self.fail_first_step_precondition_exception(context.scenario, error_message)
raise Exception(f'Before scenario steps have failed: {error_message}')

def execute_after_feature_steps(self, context):
Expand All @@ -298,18 +301,23 @@ def execute_after_feature_steps(self, context):
context.feature.reset()
for scenario in context.feature.walk_scenarios():
if scenario.should_run(context.config):
self.fail_first_step_precondition_exception(scenario)
self.fail_first_step_precondition_exception(scenario, error_message)
if len(scenario.background_steps) > 0:
context.logger.warn(f'Background from scenario status udpated to fail')
raise Exception(f'Before feature steps have failed: {error_message}')

def fail_first_step_precondition_exception(self, scenario):
def fail_first_step_precondition_exception(self, scenario, error_message):
"""
Fail first step in the given Scenario and add exception message for the output.
This is needed because xUnit exporter in Behave fails if there are not failed steps.
:param scenario: Behave's Scenario
:param error_message: Exception message
"""
if len(scenario.steps) > 0:
# Behave is an optional dependency in toolium, so it is imported here
from behave.model_core import Status
scenario.steps[0].status = Status.failed
scenario.steps[0].exception = Exception('Preconditions failed')
scenario.steps[0].error_message = str(self.before_error_message)
scenario.steps[0].error_message = str(error_message)
if len(scenario.background_steps) > 0:
scenario.background_steps[0].status = Status.failed
scenario.background_steps[0].exception = Exception('Preconditions failed')
scenario.background_steps[0].error_message = str(error_message)
8 changes: 4 additions & 4 deletions toolium/test/behave/test_env_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def test_execute_after_feature_steps_failed_before_feature(context, dyn_env):
assert dyn_env.before_error_message is None
context.execute_steps.assert_called_with('Given after feature step')
context.feature.reset.assert_called_once_with()
dyn_env.fail_first_step_precondition_exception.assert_called_once_with(context.scenario)
dyn_env.fail_first_step_precondition_exception.assert_called_once_with(context.scenario, 'Exception in before feature step')


def test_execute_after_feature_steps_failed_actions_failed_before_feature(context, dyn_env):
Expand All @@ -241,7 +241,7 @@ def test_execute_after_feature_steps_failed_actions_failed_before_feature(contex
assert dyn_env.before_error_message is None
context.execute_steps.assert_called_with('Given after feature step')
context.feature.reset.assert_called_once_with()
dyn_env.fail_first_step_precondition_exception.assert_called_once_with(context.scenario)
dyn_env.fail_first_step_precondition_exception.assert_called_once_with(context.scenario, 'Exception in before feature step')


def test_fail_first_step_precondition_exception(dyn_env):
Expand All @@ -251,7 +251,7 @@ def test_fail_first_step_precondition_exception(dyn_env):
scenario.steps = [step1, step2]
dyn_env.before_error_message = 'Exception in before feature step'

dyn_env.fail_first_step_precondition_exception(scenario)
dyn_env.fail_first_step_precondition_exception(scenario, dyn_env.before_error_message)
assert step1.status == 'failed'
assert str(step1.exception) == 'Preconditions failed'
assert step1.error_message == 'Exception in before feature step'
Expand All @@ -262,4 +262,4 @@ def test_fail_first_step_precondition_exception_without_steps(dyn_env):
scenario.steps = []
dyn_env.before_error_message = 'Exception in before feature step'

dyn_env.fail_first_step_precondition_exception(scenario)
dyn_env.fail_first_step_precondition_exception(scenario, dyn_env.before_error_message)

0 comments on commit 6afb8b3

Please sign in to comment.