Skip to content

Commit ab0235b

Browse files
authored
run behave selftest in separated thread (connects allure-framework#88 via allure-framework#122)
1 parent f2629c1 commit ab0235b

File tree

5 files changed

+43
-24
lines changed

5 files changed

+43
-24
lines changed

allure-behave/features/steps/behave_steps.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from behave.configuration import Configuration
77
from behave.formatter._registry import make_formatters
88
from behave.formatter.base import StreamOpener
9+
import threading
910

1011

1112
@given(u'feature definition')
@@ -18,15 +19,21 @@ def feature_definition(context, **kwargs):
1819
@when(u'I run behave with allure formatter')
1920
@when(u'I run behave with allure formatter with options "{args}"')
2021
def run_behave_with_allure(context, **kwargs):
21-
cmd_args = '-v -f allure_behave.formatter:AllureFormatter -f pretty'
22-
cmd = '{options} {cmd}'.format(cmd=cmd_args, options=kwargs.get('args', ''))
23-
config = Configuration(command_args=cmd)
2422

25-
result_tmp_dir = mkdtemp(dir=os.environ.get('TEST_TMP', None))
26-
stream_opener = StreamOpener(filename=result_tmp_dir)
23+
def run(context, **kwargs):
24+
cmd_args = '-v -f allure_behave.formatter:AllureFormatter -f pretty'
25+
cmd = '{options} {cmd}'.format(cmd=cmd_args, options=kwargs.get('args', ''))
26+
config = Configuration(command_args=cmd)
2727

28-
model_runner = ModelRunner(config, [context.feature_definition])
29-
model_runner.formatters = make_formatters(config, [stream_opener])
30-
model_runner.run()
28+
result_tmp_dir = mkdtemp(dir=os.environ.get('TEST_TMP', None))
29+
stream_opener = StreamOpener(filename=result_tmp_dir)
3130

32-
context.allure_report = AllureReport(result_tmp_dir)
31+
model_runner = ModelRunner(config, [context.feature_definition])
32+
model_runner.formatters = make_formatters(config, [stream_opener])
33+
model_runner.run()
34+
35+
context.allure_report = AllureReport(result_tmp_dir)
36+
37+
behave_tread = threading.Thread(target=run, args=(context,), kwargs=kwargs)
38+
behave_tread.start()
39+
behave_tread.join()

allure-behave/src/listener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from allure_commons import register
1+
from allure_commons import plugin_manager
22
from allure_commons.logger import AllureFileLogger
33
from allure_commons.reporter import AllureReporter
44
from allure_commons.utils import uuid4
@@ -23,7 +23,7 @@ class AllureListener(object):
2323
def __init__(self, result_dir):
2424
self.logger = AllureReporter()
2525
file_logger = AllureFileLogger(result_dir)
26-
register(file_logger)
26+
plugin_manager.register(file_logger)
2727

2828
self.current_group_uuid = None
2929
self.current_before_uuid = None

allure-pytest/src/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ def link_pattern(string):
9191
def pytest_configure(config):
9292
report_dir = config.option.allure_report_dir
9393
test_helper = AllureTestHelper(config)
94-
allure_commons.register(test_helper)
94+
allure_commons.plugin_manager.register(test_helper)
9595

9696
if report_dir:
9797
test_listener = AllureListener(config)
9898
config.pluginmanager.register(test_listener)
99-
allure_commons.register(test_listener)
99+
allure_commons.plugin_manager.register(test_listener)
100100

101101
file_logger = AllureFileLogger(report_dir)
102-
allure_commons.register(file_logger)
102+
allure_commons.plugin_manager.register(file_logger)
103103

104104

105105
def pytest_collection_modifyitems(items, config):
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from allure_commons._hooks import hookimpl # noqa: F401
2-
from allure_commons._core import register # noqa: F401
2+
from allure_commons._core import plugin_manager # noqa: F401
33
from allure_commons._allure import fixture # noqa: F401
44

55
__all__ = [
6-
'hookimpl'
7-
'register'
6+
'hookimpl',
7+
'plugin_manager',
88

9-
'fixture'
9+
'fixture',
1010
]

allure-python-commons/src/_core.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import threading
2+
from six import with_metaclass
23
from pluggy import PluginManager
34
from allure_commons import _hooks
45

56

6-
_storage = threading.local()
7-
_storage.plugin_manager = PluginManager('allure')
8-
_storage.plugin_manager.add_hookspecs(_hooks.AllureUserHooks)
9-
_storage.plugin_manager.add_hookspecs(_hooks.AllureDeveloperHooks)
7+
class MetaPluginManager(type):
8+
_storage = threading.local()
109

10+
@staticmethod
11+
def get_plugin_manager():
12+
if not hasattr(MetaPluginManager._storage, 'plugin_manager'):
13+
MetaPluginManager._storage.plugin_manager = PluginManager('allure')
14+
MetaPluginManager._storage.plugin_manager.add_hookspecs(_hooks.AllureUserHooks)
15+
MetaPluginManager._storage.plugin_manager.add_hookspecs(_hooks.AllureDeveloperHooks)
1116

12-
plugin_manager = _storage.plugin_manager
13-
register = plugin_manager.register
17+
return MetaPluginManager._storage.plugin_manager
18+
19+
def __getattr__(cls, attr):
20+
pm = MetaPluginManager.get_plugin_manager()
21+
return getattr(pm, attr)
22+
23+
24+
class plugin_manager(with_metaclass(MetaPluginManager)):
25+
pass

0 commit comments

Comments
 (0)