Skip to content

Commit

Permalink
QE-16610 Log errors for failing before_scenario_hooks (#527)
Browse files Browse the repository at this point in the history
## PR Type
What kind of change does this PR introduce?

<!-- Please check the one that applies to this PR using "x". -->
- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting)
- [ ] Refactoring (no functional changes)
- [ ] CI related changes
- [ ] Other... Please describe:

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying, or
link to a relevant issue. -->

Issue Number: https://dominodatalab.atlassian.net/browse/QE-16610

## What is the new behavior?

## Does this PR introduce a breaking change?
- [ ] Yes
- [ ] No

<!-- If this PR contains a breaking change, please describe the impact
and migration path for existing applications below. -->

## Other information
  • Loading branch information
ddl-kgarton authored Sep 9, 2024
1 parent 8f30f4d commit 1c14e60
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project closely adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.205.0
- change - increase information logged for failing before_scenario_hooks

## 0.204.0
- chore - add gh workflows for publishing
- chore - fix project metadata
Expand Down
40 changes: 39 additions & 1 deletion features/cli/run_with_hooks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Feature: Run with hooks
.* DEBUG just logging some stuff from my before all hook
Feature: Feature that simply echo's "Hello World"
.* DEBUG just logging some stuff from my before scenario hook
.* DEBUG HOOK before_scenario_log: passed ✅
Scenario: This is a scenario that simply echo's hello world
.* DEBUG just logging some stuff from my before step hook
Expand Down Expand Up @@ -203,7 +204,7 @@ Feature: Run with hooks
When I run the command "cucu run {CUCU_RESULTS_DIR}/failing_before_hooks/echo.feature --results {CUCU_RESULTS_DIR}/failing_before_hooks_results/ --generate-report --report {CUCU_RESULTS_DIR}/failing_before_hooks_report/" and save stdout to "STDOUT", stderr to "STDERR" and expect exit code "1"
Then I should see "{STDOUT}" contains the following
"""
HOOK-ERROR in before_scenario: RuntimeError: boom
HOOK-ERROR in before_scenario_fail: RuntimeError: boom
"""
And I should see "{STDOUT}" contains the following
"""
Expand Down Expand Up @@ -303,3 +304,40 @@ Feature: Run with hooks
And I click the link "Feature that has failing and passing after scenario hooks"
And I click the link "Hello world scenario"
Then I should see the text "Hello World"

@QE-16733
Scenario: When a before_scenario hook fails, the Scenario is skipped, but reports as errored
Given I create a file at "{CUCU_RESULTS_DIR}/custom_hooks/environment.py" with the following:
"""
from cucu.environment import *
from cucu import register_before_scenario_hook
def before_scenario_error(ctx):
raise RuntimeError("This error should be logged and reported.")
register_before_scenario_hook(before_scenario_error)
"""
And I create a file at "{CUCU_RESULTS_DIR}/custom_hooks/steps/__init__.py" with the following:
"""
from cucu.steps import *
"""
And I create a file at "{CUCU_RESULTS_DIR}/custom_hooks/echo.feature" with the following:
"""
Feature: Feature that simply echoes "Hello World"
Scenario: This is a scenario that simply echoes hello world
Given I echo "Hello"
And I echo "World"
"""
* # The --show-skips argument is necessary for both the `cucu run` command and the `cucu report` command
When I run the command "cucu run {CUCU_RESULTS_DIR}/custom_hooks/echo.feature --show-skips --results {CUCU_RESULTS_DIR}/custom_hooks_results/ -l debug --no-color-output" and save stdout to "STDOUT", stderr to "STDERR" and expect exit code "1"
When I run the command "cucu report {CUCU_RESULTS_DIR}/custom_hooks_results --show-skips --output {CUCU_RESULTS_DIR}/browser-report" and expect exit code "0"
And I start a webserver at directory "{CUCU_RESULTS_DIR}/browser-report/" and save the port to the variable "PORT"
And I open a browser at the url "http://{HOST_ADDRESS}:{PORT}/flat.html"
Then I should see a table that contains rows matching the following
| .* | Feature that simply echoes "Hello World" | This is a scenario that simply echoes hello world | .* | errored | .* |
And I wait to click the link "This is a scenario that simply echoes hello world"
* # Additional check of the log file itself here, when QE-16733 is resolved
# When I wait to click the link "Logs"
# And I wait to click the link "cucu.debug.console.log"
# Then I wait to see the text "This error should be logged and reported."
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cucu"
version = "0.204.0"
version = "0.205.0"
description = "Easy BDD web testing"
readme = "README.md"
license = "The Clear BSD License"
Expand Down
17 changes: 15 additions & 2 deletions src/cucu/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,28 @@ def before_scenario(ctx, scenario):

# run before all scenario hooks
for hook in CONFIG["__CUCU_BEFORE_SCENARIO_HOOKS"]:
hook(ctx)
try:
hook(ctx)
logger.debug(f"HOOK {hook.__name__}: passed ✅")
except Exception as e:
error_message = (
f"HOOK-ERROR in {hook.__name__}: {e.__class__.__name__}: {e}\n"
)
error_message += traceback.format_exc()
logger.error(error_message)
ctx.scenario.mark_skipped()
# Set 'hook_failed' status to 'True' so that the test gets marked
# as 'errored', even though no steps ran
ctx.scenario.hook_failed = True


def run_after_scenario_hook(ctx, scenario, hook):
try:
hook(ctx)
logger.debug(f"HOOK {hook.__name__}: passed ✅")
except Exception as e:
# For any after scenario hooks,'hook_failed' status will be 'False' but will attach the error message to scenario.
# For any after scenario hooks,'hook_failed' status will be 'False'
# but will attach the error message to scenario.
error_message = (
f"HOOK-ERROR in {hook.__name__}: {e.__class__.__name__}: {e}\n"
)
Expand Down
2 changes: 1 addition & 1 deletion src/cucu/reporter/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def generate(results, basepath, only_failures=False):
f"{scenario_filepath}/"
)

if ".console." in log_filepath:
if ".console." in log_filepath and scenario_started_at:
log_filepath += ".html"

log_files.append(
Expand Down

0 comments on commit 1c14e60

Please sign in to comment.