-
Notifications
You must be signed in to change notification settings - Fork 20
/
environment.py
210 lines (179 loc) · 7.85 KB
/
environment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# -*- coding: utf-8 -*-
# pylint: disable=W0703# pylint: disable=W0703
"""
BehaveX - Agile test wrapper on top of Behave (BDD)
"""
import logging
import os
import shutil
import sys
from behave.contrib.scenario_autoretry import patch_scenario_with_autoretry
from behave.log_capture import capture
from behave.runner import Context, ModelRunner
from behavex import conf_mgr
from behavex.conf_mgr import get_env, get_param
from behavex.global_vars import global_vars
from behavex.outputs import report_json, report_xml
from behavex.outputs.report_utils import create_log_path
from behavex.utils import (
LOGGING_CFG,
create_custom_log_when_called,
get_autoretry_attempts,
get_logging_level,
)
Context.__getattribute__ = create_custom_log_when_called
hooks_already_set = False
def extend_behave_hooks():
"""
Extend Behave hooks with BehaveX hooks code.
"""
global hooks_already_set
behave_run_hook = ModelRunner.run_hook
behavex_env = sys.modules[__name__]
def run_hook(self, name, context, *args):
if name == 'before_all':
# noinspection PyUnresolvedReferences
behavex_env.before_all(context)
behave_run_hook(self, name, context, *args)
if name == 'before_feature':
# noinspection PyUnresolvedReferences
behavex_env.before_feature(context, *args)
behave_run_hook(self, name, context, *args)
if name == 'before_scenario':
# noinspection PyUnresolvedReferences
behavex_env.before_scenario(context, *args)
behave_run_hook(self, name, context, *args)
if name == 'before_step':
# noinspection PyUnresolvedReferences
behavex_env.before_step(context, *args)
behave_run_hook(self, name, context, *args)
if name == 'after_step':
# noinspection PyUnresolvedReferences
behave_run_hook(self, name, context, *args)
behavex_env.after_step(context, *args)
if name == 'after_scenario':
# noinspection PyUnresolvedReferences
behave_run_hook(self, name, context, *args)
behavex_env.after_scenario(context, *args)
if name == 'after_feature':
# noinspection PyUnresolvedReferences
behave_run_hook(self, name, context, *args)
behavex_env.after_feature(context, *args)
if name == 'after_all':
# noinspection PyUnresolvedReferences
behave_run_hook(self, name, context, *args)
behavex_env.after_all(context, *args)
if not hooks_already_set:
hooks_already_set = True
ModelRunner.run_hook = run_hook
def before_all(context):
"""Setup up initial tests configuration."""
try:
# Initialyzing log handler
context.bhx_log_handler = None
# Store framework settings to make them accessible from steps
context.bhx_config_framework = conf_mgr.get_config()
context.bhx_inside_scenario = False
except Exception as exception:
_log_exception_and_continue('before_all (behavex)', exception)
# noinspection PyUnusedLocal
def before_feature(context, feature):
try:
context.bhx_execution_attempts = {}
for scenario in feature.scenarios:
scenario.tags += feature.tags
if get_param('dry_run'):
if 'MANUAL' not in scenario.tags:
scenario.tags.append(u'BHX_MANUAL_DRY_RUN')
scenario.tags.append(u'MANUAL')
configured_attempts = get_autoretry_attempts(scenario.tags)
if configured_attempts > 0:
patch_scenario_with_autoretry(scenario, configured_attempts)
except Exception as exception:
_log_exception_and_continue('before_feature (behavex)', exception)
def before_scenario(context, scenario):
"""Initialize logs for current scenario."""
try:
context.bhx_inside_scenario = True
if scenario.name not in context.bhx_execution_attempts:
context.bhx_execution_attempts[scenario.name] = 0
execution_attempt = context.bhx_execution_attempts[scenario.name]
retrying_execution = True if execution_attempt > 0 else False
concat_feature_and_scenario_name = "{}-{}".format(str(context.feature.name), str(scenario.name))
context.log_path = create_log_path(concat_feature_and_scenario_name, retrying_execution)
context.bhx_log_handler = _add_log_handler(context.log_path)
if retrying_execution:
logging.info('Retrying scenario...\n'.format())
shutil.rmtree(context.evidence_path)
except Exception as exception:
_log_exception_and_continue('before_scenario (behavex)', exception)
def before_step(context, step):
if context.bhx_inside_scenario:
logging.info('STEP: {}'.format(step.name))
def after_step(context, step):
if step.exception:
step.error_message = step.error_message
logging.exception(step.exception)
if context.bhx_inside_scenario:
logging.debug('Execution time %.2f sec\n', step.duration)
@capture
def after_scenario(context, scenario):
try:
configured_attempts = get_autoretry_attempts(scenario.tags)
if scenario.status in ('failed', 'untested') and configured_attempts > 0:
feature_name = scenario.feature.name
if feature_name not in global_vars.retried_scenarios:
global_vars.retried_scenarios[feature_name] = [scenario.name]
else:
global_vars.retried_scenarios[feature_name].append(scenario.name)
context.bhx_execution_attempts[scenario.name] += 1
_close_log_handler(context.bhx_log_handler)
except Exception as exception:
_log_exception_and_continue('after_scenario (behavex)', exception)
# noinspection PyUnusedLocal
def after_feature(context, feature):
if get_env('multiprocessing') and get_param('parallel_scheme') == 'scenario':
return
report_xml.export_feature_to_xml(feature)
def after_all(context):
try:
# noinspection PyProtectedMember
report_json.generate_execution_info(context, context._runner.features)
except Exception as exception:
_log_exception_and_continue('after_all (json_report)', exception)
def _add_log_handler(log_path):
"""Adding a new log handler to logger"""
log_filename = os.path.join(log_path, 'scenario.log')
file_handler = logging.FileHandler(
log_filename, mode='+a', encoding=LOGGING_CFG['file_handler']['encoding']
)
log_level = get_logging_level()
logging.getLogger().setLevel(log_level)
file_handler.setFormatter(_get_log_formatter())
logging.getLogger().addHandler(file_handler)
return file_handler
def _close_log_handler(handler):
"""Closing current log handlers and removing them from logger"""
if handler:
if hasattr(handler, 'stream') and hasattr(handler.stream, 'close'):
handler.stream.close()
logging.getLogger().removeHandler(handler)
def _get_log_formatter():
"""Adding a new log handler to logger"""
default_log_format = '%(asctime)s - %(levelname)s - %(message)s'
default_date_format = '%H:%M:%S'
logging_format = os.environ.get('logging_format', default_log_format)
date_format = os.environ.get('logging_datefmt', default_date_format)
try:
formatter = logging.Formatter(logging_format, date_format)
except Exception as exception:
formatter = logging.Formatter(default_log_format, default_date_format)
_log_exception_and_continue('_get_log_formatter', exception)
return formatter
def _log_exception_and_continue(module, exception):
"""Logs any exception that occurs without raising it,
just to avoid the testing framework to stop executing
the following tests"""
error_message = "Unexpected error in '%s' function:" % module
logging.exception(error_message)
logging.exception(exception)