Skip to content

Commit 3e46662

Browse files
author
Glazov Nikolay
committed
Change in core Thread to Process
Now step correct showing in Thread
1 parent ec5c9e8 commit 3e46662

File tree

9 files changed

+51
-38
lines changed

9 files changed

+51
-38
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
*.egg-info
99

1010
*/build
11-
*/dist
11+
*/dist
12+
.eggs

allure-pytest/src/listener.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from allure_commons.utils import uuid4
66
from allure_commons.utils import represent
77
from allure_commons.utils import platform_label
8-
from allure_commons.utils import host_tag, thread_tag
8+
from allure_commons.utils import host_tag, thread_tag, thread_tag_detail
99
from allure_commons.reporter import AllureReporter
1010
from allure_commons.model2 import TestStepResult, TestResult, TestBeforeResult, TestAfterResult
1111
from allure_commons.model2 import TestResultContainer
@@ -22,7 +22,6 @@
2222
from allure_pytest.utils import get_outcome_status, get_outcome_status_details
2323
from allure_pytest.utils import get_pytest_report_status
2424
from allure_commons.utils import md5
25-
import threading
2625

2726

2827
class AllureListener(object):
@@ -37,13 +36,8 @@ def __init__(self, config):
3736
@allure_commons.hookimpl
3837
def start_step(self, uuid, title, params):
3938
parameters = [Parameter(name=name, value=value) for name, value in params.items()]
40-
step = TestStepResult(name=title, start=now(), parameters=parameters, thrd=threading.current_thread().name)
41-
if thread_tag() != self._thread:
42-
self.allure_logger.start_step(self.allure_logger.last_with_thread(), uuid, step)
43-
else:
44-
self.allure_logger.start_step(None, uuid, step)
45-
# step = TestStepResult(name=title, start=now(), parameters=parameters)
46-
# self.allure_logger.start_step(None, uuid, step)
39+
step = TestStepResult(name=title, start=now(), parameters=parameters, thrd=thread_tag_detail())
40+
self.allure_logger.start_step(None, uuid, step)
4741

4842
@allure_commons.hookimpl
4943
def stop_step(self, uuid, exc_type, exc_val, exc_tb):
@@ -54,7 +48,7 @@ def stop_step(self, uuid, exc_type, exc_val, exc_tb):
5448

5549
@allure_commons.hookimpl
5650
def start_fixture(self, parent_uuid, uuid, name):
57-
after_fixture = TestAfterResult(name=name, start=now())
51+
after_fixture = TestAfterResult(name=name, start=now(), thrd=thread_tag_detail())
5852
self.allure_logger.start_after_fixture(parent_uuid, uuid, after_fixture)
5953

6054
@allure_commons.hookimpl
@@ -67,15 +61,15 @@ def stop_fixture(self, parent_uuid, uuid, name, exc_type, exc_val, exc_tb):
6761
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
6862
def pytest_runtest_protocol(self, item, nextitem):
6963
uuid = self._cache.push(item.nodeid)
70-
test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now())
64+
test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now(), thrd=thread_tag_detail())
7165
self.allure_logger.schedule_test(uuid, test_result)
7266
yield
7367

7468
@pytest.hookimpl(hookwrapper=True)
7569
def pytest_runtest_setup(self, item):
7670
if not self._cache.get(item.nodeid):
7771
uuid = self._cache.push(item.nodeid)
78-
test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now())
72+
test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now(), thrd=thread_tag_detail())
7973
self.allure_logger.schedule_test(uuid, test_result)
8074

8175
yield
@@ -86,7 +80,7 @@ def pytest_runtest_setup(self, item):
8680
group_uuid = self._cache.get(fixturedef)
8781
if not group_uuid:
8882
group_uuid = self._cache.push(fixturedef)
89-
group = TestResultContainer(uuid=group_uuid)
83+
group = TestResultContainer(uuid=group_uuid, thrd=thread_tag_detail())
9084
self.allure_logger.start_group(group_uuid, group)
9185
self.allure_logger.update_group(group_uuid, children=uuid)
9286
params = item.callspec.params if hasattr(item, 'callspec') else {}
@@ -134,13 +128,13 @@ def pytest_fixture_setup(self, fixturedef, request):
134128

135129
if not container_uuid:
136130
container_uuid = self._cache.push(fixturedef)
137-
container = TestResultContainer(uuid=container_uuid)
131+
container = TestResultContainer(uuid=container_uuid, thrd=thread_tag_detail())
138132
self.allure_logger.start_group(container_uuid, container)
139133

140134
self.allure_logger.update_group(container_uuid, start=now())
141135

142136
before_fixture_uuid = uuid4()
143-
before_fixture = TestBeforeResult(name=fixture_name, start=now())
137+
before_fixture = TestBeforeResult(name=fixture_name, start=now(), thrd=thread_tag_detail())
144138
self.allure_logger.start_before_fixture(container_uuid, before_fixture_uuid, before_fixture)
145139

146140
outcome = yield
@@ -247,12 +241,6 @@ def add_description_html(self, test_description_html):
247241
if test_result:
248242
test_result.descriptionHtml = test_description_html
249243

250-
@allure_commons.hookimpl
251-
def add_thrd(self, test_description_html):
252-
test_result = self.allure_logger.get_test(None)
253-
if test_result:
254-
test_result.descriptionHtml = test_description_html
255-
256244
@allure_commons.hookimpl
257245
def add_link(self, url, link_type, name):
258246
test_result = self.allure_logger.get_test(None)

allure-python-commons/src/_allure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from functools import wraps
22
from typing import Any, Callable, TypeVar
3+
import threading
34

45
from allure_commons._core import plugin_manager
56
from allure_commons.types import LabelType, LinkType

allure-python-commons/src/_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
class MetaPluginManager(type):
9-
_storage = threading.local()
9+
# _storage = threading.local()
1010
_storage = multiprocessing.Process
1111

1212
@staticmethod

allure-python-commons/src/lifecycle.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from allure_commons.utils import uuid4
1212
from allure_commons.utils import now
1313
from allure_commons.types import AttachmentType
14+
import threading
1415

1516

1617
class AllureLifecycle(object):
@@ -52,7 +53,8 @@ def write_test_case(self, uuid=None):
5253
@contextmanager
5354
def start_step(self, parent_uuid=None, uuid=None):
5455
parent = self._get_item(uuid=parent_uuid, item_type=ExecutableItem)
55-
step = TestStepResult()
56+
# step = TestStepResult()
57+
step = TestStepResult(thrd=threading.current_thread().name)
5658
step.start = now()
5759
parent.steps.append(step)
5860
self._items[uuid or uuid4()] = step

allure-python-commons/src/logger.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313

1414

1515
def delete_step_thrd(data):
16-
if "steps" in data.keys():
17-
for step in data['steps']:
18-
step.pop("thrd", None)
19-
step = delete_step_thrd(step)
16+
if isinstance(data, list):
17+
for field in data:
18+
field = delete_step_thrd(field)
19+
elif isinstance(data, dict):
20+
for field in data.keys():
21+
if field == 'thrd':
22+
data.pop('thrd')
23+
break
24+
field = delete_step_thrd(data[field])
2025
return data
2126

22-
2327
class AllureFileLogger(object):
2428

2529
def __init__(self, report_dir, clean=False):

allure-python-commons/src/model2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class TestResultContainer(object):
2323
links = attrib(default=Factory(list))
2424
start = attrib(default=None)
2525
stop = attrib(default=None)
26+
thrd = attrib(default=None)
2627

2728

2829
@attrs

allure-python-commons/src/reporter.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
from collections import OrderedDict
2-
import os
3-
import shutil
2+
# import os
3+
# import shutil
4+
# import threading
5+
# from termcolor import cprint
6+
# from pygments import highlight
7+
# from pygments.lexers import PythonLexer
8+
# from pygments.formatters import Terminal256Formatter
9+
# from pprint import pformat
10+
from allure_commons.utils import thread_tag, thread_tag_detail
11+
12+
# def pprint_color(obj):
13+
# print(highlight(pformat(obj), PythonLexer(), Terminal256Formatter()))
414

515
from allure_commons.types import AttachmentType
616
from allure_commons.model2 import ExecutableItem
@@ -14,6 +24,7 @@ class AllureReporter(object):
1424
def __init__(self):
1525
self._items = OrderedDict()
1626
self._orphan_items = []
27+
self._thread = thread_tag_detail()
1728

1829
def _update_item(self, uuid, **kwargs):
1930
item = self._items[uuid] if uuid else self._items[next(reversed(self._items))]
@@ -25,16 +36,20 @@ def _update_item(self, uuid, **kwargs):
2536
setattr(item, name, value)
2637

2738
def _last_executable(self):
28-
for _uuid in reversed(self._items):
39+
copy_items = self._items.copy()
40+
for _uuid in reversed(copy_items):
41+
if hasattr(self._items[_uuid], "thrd") and self._items[_uuid].thrd != thread_tag_detail():
42+
continue
2943
if isinstance(self._items[_uuid], ExecutableItem):
3044
return _uuid
31-
32-
def last_with_thread(self):
33-
for _uuid in reversed(self._items):
34-
if self._items[_uuid].thrd != "MainThread":
45+
for _uuid in reversed(copy_items):
46+
if hasattr(self._items[_uuid], "thrd") and self._items[_uuid].thrd != self._thread:
3547
continue
3648
if isinstance(self._items[_uuid], ExecutableItem):
3749
return _uuid
50+
for _uuid in reversed(copy_items):
51+
if isinstance(self._items[_uuid], ExecutableItem):
52+
return _uuid
3853

3954
def get_item(self, uuid):
4055
return self._items.get(uuid)
@@ -120,8 +135,6 @@ def _attach(self, uuid, name=None, attachment_type=None, extension=None):
120135
def attach_file(self, uuid, source, name=None, attachment_type=None, extension=None):
121136
file_name = self._attach(uuid, name=name, attachment_type=attachment_type, extension=extension)
122137
plugin_manager.hook.report_attached_file(source=source, file_name=file_name)
123-
destination = os.path.join('./allure-results', file_name)
124-
shutil.copy2(source, destination)
125138

126139
def attach_data(self, uuid, body, name=None, attachment_type=None, extension=None):
127140
file_name = self._attach(uuid, name=name, attachment_type=attachment_type, extension=extension)

allure-python-commons/src/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def thread_tag():
9696
return '{0}-{1}'.format(os.getpid(), threading.current_thread().name)
9797

9898

99+
def thread_tag_detail():
100+
return '{0}-{1}'.format(threading.get_native_id(), threading.current_thread().name)
101+
99102
def host_tag():
100103
return socket.gethostname()
101104

0 commit comments

Comments
 (0)