Skip to content

Commit ad5b224

Browse files
authored
Add thread and host labels to pytest (fixes allure-framework#86) (via allure-framework#163)
add thread and host labels to pytest
1 parent 659fa95 commit ad5b224

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

allure-pytest/src/listener.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +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
89
from allure_commons.reporter import AllureReporter
910
from allure_commons.model2 import TestStepResult, TestResult, TestBeforeResult, TestAfterResult
1011
from allure_commons.model2 import TestResultContainer
@@ -24,6 +25,8 @@ class AllureListener(object):
2425
def __init__(self):
2526
self.allure_logger = AllureReporter()
2627
self._cache = ItemCache()
28+
self._host = host_tag()
29+
self._thread = thread_tag()
2730

2831
@allure_commons.hookimpl
2932
def start_step(self, uuid, title, params):
@@ -74,6 +77,8 @@ def pytest_runtest_protocol(self, item, nextitem):
7477

7578
test_case.labels.extend([Label(name=name, value=value) for name, value in allure_labels(item)])
7679
test_case.labels.extend([Label(name=LabelType.TAG, value=value) for value in pytest_markers(item)])
80+
test_case.labels.append(Label(name=LabelType.HOST, value=self._host))
81+
test_case.labels.append(Label(name=LabelType.THREAD, value=self._thread))
7782
test_case.labels.append(Label(name=LabelType.FRAMEWORK, value='pytest'))
7883
test_case.labels.append(Label(name=LabelType.LANGUAGE, value=platform_label()))
7984

allure-pytest/test/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import shlex
55
from inspect import getmembers, isfunction
66
from allure_commons_test.report import AllureReport
7+
from allure_commons.utils import thread_tag
78

89

910
@pytest.fixture(scope='function', autouse=True)
@@ -31,7 +32,7 @@ def allure_report_with_params(request, tmpdir_factory):
3132
tmpdir = tmpdir_factory.mktemp('data')
3233

3334
def run_with_params(*params):
34-
key = '{module}{param}'.format(module=module, param=''.join(params))
35+
key = '{thread}{module}{param}'.format(thread=thread_tag(), module=module, param=''.join(params))
3536
if not request.config.cache.get(key, False):
3637
_runner(tmpdir.strpath, module, *params)
3738
request.config.cache.set(key, True)

allure-pytest/tox.ini

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[tox]
22
envlist=
33
py{27,33,34,35}
4+
xdist
45
static_check
56

67

@@ -21,6 +22,24 @@ commands=
2122
py.test --doctest-module --alluredir={envtmpdir} {posargs: ./test/}
2223

2324

25+
[testenv:xdist]
26+
passenv =
27+
HOME
28+
29+
whitelist_externals = rm
30+
31+
deps=
32+
pyhamcrest
33+
pytest-xdist
34+
{distshare}/allure-python-commons-2*.zip
35+
{distshare}/allure-python-commons-test-2*.zip
36+
37+
commands=
38+
python setup.py develop
39+
rm -f {envtmpdir}/*.json
40+
py.test -n 4 --doctest-module --alluredir={envtmpdir} {posargs: ./test/}
41+
42+
2443
# Run tests without result checking. It is useful for:
2544
# 1. Getting demo report: `tox -e demo`
2645
# 2. Executing separate test:

allure-python-commons/src/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# -*- coding: utf-8 -*-
22

3+
import os
34
import sys
45
import six
56
import time
67
import uuid
8+
import socket
79
import inspect
810
import hashlib
911
import platform
12+
import threading
1013
import traceback
1114

1215
if sys.version_info.major > 2:
@@ -38,6 +41,14 @@ def platform_label():
3841
major_version=major_version)
3942

4043

44+
def thread_tag():
45+
return '{0}-{1}'.format(os.getpid(), threading.current_thread().name)
46+
47+
48+
def host_tag():
49+
return socket.gethostname()
50+
51+
4152
def represent(item):
4253
"""
4354
>>> represent(None)

0 commit comments

Comments
 (0)