Skip to content
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
67 changes: 67 additions & 0 deletions allure-behave/src/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import inspect
import threading
import allure_commons
from allure_commons.logger import AllureFileLogger
from allure_behave.listener import AllureListener

HOOKS = [
"before_feature",
"after_feature",
"before_scenario",
"after_scenario",
"before_step",
"after_step"
]

_storage = threading.local()


def wrapper(original, allured):
def hook(*args, **kwargs):
allured(*args, **kwargs)
return original(*args, **kwargs)

return hook


def allure_report(result_dir="allure_results"):
allure_hooks = AllureHooks(result_dir)
frame = inspect.currentframe()
try:
for hook_name in HOOKS:
if hook_name in frame.f_back.f_locals:
frame.f_back.f_locals[hook_name] = wrapper(frame.f_back.f_locals[hook_name],
getattr(allure_hooks, hook_name))
else:
frame.f_back.f_locals[hook_name] = getattr(allure_hooks, hook_name)
finally:
del frame


class AllureHooks(object):
def __init__(self, result_dir):
self.listener = AllureListener({})

if not hasattr(_storage, 'file_logger'):
_storage.file_logger = AllureFileLogger(result_dir)
allure_commons.plugin_manager.register(_storage.file_logger)

allure_commons.plugin_manager.register(self.listener)

def before_feature(self, context, feature):
self.listener.start_feature()

def after_feature(self, context, feature):
self.listener.stop_feature()

def before_scenario(self, context, scenario):
self.listener.start_scenario(scenario)

def after_scenario(self, context, scenario):
self.listener.stop_scenario(scenario)

def before_step(self, context, step):
self.listener.start_behave_step(step)

def after_step(self, context, step):
self.listener.stop_behave_step(step)
27 changes: 17 additions & 10 deletions allure-behave/src/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, behave_config):
self.behave_config = behave_config
self.logger = AllureReporter()
self.current_step_uuid = None
self.current_scenario_uuid = None
self.execution_context = Context()
self.fixture_context = Context()
self.steps = deque()
Expand Down Expand Up @@ -82,12 +83,15 @@ def stop_feature(self):

@allure_commons.hookimpl
def start_test(self, parent_uuid, uuid, name, parameters, context):
scenario = context['scenario']
self.start_scenario(context['scenario'])

def start_scenario(self, scenario):
self.current_scenario_uuid = uuid4()
self.fixture_context.enter()
self.execution_context.enter()
self.execution_context.append(uuid)
self.execution_context.append(self.current_scenario_uuid)

test_case = TestResult(uuid=uuid, start=now())
test_case = TestResult(uuid=self.current_scenario_uuid, start=now())
test_case.name = scenario_name(scenario)
test_case.historyId = scenario_history_id(scenario)
test_case.description = '\n'.join(scenario.description)
Expand All @@ -98,31 +102,34 @@ def start_test(self, parent_uuid, uuid, name, parameters, context):
test_case.labels.append(Label(name=LabelType.FRAMEWORK, value='behave'))
test_case.labels.append(Label(name=LabelType.LANGUAGE, value=platform_label()))

self.logger.schedule_test(uuid, test_case)
self.logger.schedule_test(self.current_scenario_uuid, test_case)

@allure_commons.hookimpl
def stop_test(self, parent_uuid, uuid, name, context, exc_type, exc_val, exc_tb):
scenario = context['scenario']
self.stop_scenario(context['scenario'])

def stop_scenario(self, scenario):
if scenario.status == 'skipped' and not self.behave_config.show_skipped:
self.logger.drop_test(uuid)
self.logger.drop_test(self.current_scenario_uuid)
else:
status = scenario_status(scenario)
status_details = scenario_status_details(scenario)

self.flush_steps()
test_result = self.logger.get_test(uuid)
test_result = self.logger.get_test(self.current_scenario_uuid)
test_result.stop = now()
test_result.status = status
test_result.statusDetails = status_details
self.logger.close_test(uuid)
self.logger.close_test(self.current_scenario_uuid)
self.current_step_uuid = None

for group in self.fixture_context.exit():
group.children.append(uuid)
group.children.append(self.current_scenario_uuid)
self.logger.stop_group(group.uuid)

self.execution_context.exit()
self.execution_context.append(uuid)
self.execution_context.append(self.current_scenario_uuid)
self.current_scenario_uuid = None

def schedule_step(self, step):
self.steps.append(step)
Expand Down
3 changes: 2 additions & 1 deletion allure-behave/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ def get_status(exception):
def step_status_details(result):
if result.exception:
# workaround for https://github.com/behave/behave/pull/616
trace = result.exc_traceback if type(result.exc_traceback) == list else format_traceback(result.exc_traceback)
trace = "\n".join(result.exc_traceback) if type(result.exc_traceback) == list else format_traceback(
result.exc_traceback)
return StatusDetails(message=format_exception(type(result.exception), result.exception), trace=trace)

elif result.status == 'undefined':
Expand Down
10 changes: 5 additions & 5 deletions allure-behave/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ commands=
pip uninstall -y behave
pip install git+git://github.com/behave/behave.git@refs/pull/616/head
rm -rf {envtmpdir}/*
behave --processes 3 \
--parallel-element feature \
-f allure_behave.formatter:AllureFormatter \
-o {envtmpdir}/allrue-result \
-f pretty {posargs: ./features}
behave --processes 4 \
--parallel-element feature {posargs: ./features}
; -f allure_behave.formatter:AllureFormatter \
; -o {envtmpdir}/allrue-result \
; -f pretty {posargs: ./features}

[testenv:behave-master]
passenv = HOME
Expand Down