Skip to content

Commit

Permalink
bug(QAWTO-212): fix result for action before the feature error with b…
Browse files Browse the repository at this point in the history
…ackground (#397)

* bug(QAWTO-212): fix action before the feature with background output with error

* fix(QAWTO-212): fix flake8 linter messages

* fix(QAWTO-212): move behave import to selected function

* docs(QAWTO-212): update changelog
  • Loading branch information
ricardogarfe authored Jul 4, 2024
1 parent 63188db commit 3f08314
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ v3.1.5
- Fix `export_poeditor_project` method allowing empty export response
- Add `key=value` expressions for selecting elements in the context storage
- Upgrade Faker version to 25.9.*
- Fix result for action before the feature with error and background to fail scenarios

v3.1.4
------
Expand Down
19 changes: 13 additions & 6 deletions toolium/behave/env_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,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 +298,25 @@ 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('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
"""
# Behave is an optional dependency in toolium, so it is imported here
from behave.model_core import Status
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)
10 changes: 6 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,8 @@ 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 +242,8 @@ 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 +253,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 +264,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 3f08314

Please sign in to comment.