Skip to content

Commit 8a6c2fd

Browse files
authored
Make it possible to add item attributes as key/value pairs, not just values
1 parent 1dfbad9 commit 8a6c2fd

File tree

6 files changed

+63
-43
lines changed

6 files changed

+63
-43
lines changed

pytest_reportportal/helpers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""This module includes help functions for both plugin and service modules."""
2+
import logging
3+
4+
log = logging.getLogger(__name__)
5+
6+
7+
def get_attributes(rp_attributes):
8+
"""Generate list of attributes for RP.
9+
10+
:param list rp_attributes: rp_attributes option value
11+
:return list: List of dictionaries to be passed to the
12+
RP Python client
13+
"""
14+
attrs = []
15+
for rp_attr in rp_attributes:
16+
try:
17+
key, value = rp_attr.split(':')
18+
attr_dict = {'key': key, 'value': value}
19+
except ValueError:
20+
attr_dict = {'value': rp_attr}
21+
22+
if all(value for value in attr_dict.values()):
23+
attrs.append(attr_dict)
24+
continue
25+
log.debug('Failed to process "{0}" attribute, attribute value'
26+
' should not be empty.'.format(rp_attr))
27+
return attrs

pytest_reportportal/plugin.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from reportportal_client.errors import ResponseError
1515
from .service import PyTestServiceClass
1616
from .listener import RPReportListener
17+
from .helpers import get_attributes
1718

1819
try:
1920
# This try/except can go away once we support pytest >= 3.3
@@ -41,29 +42,6 @@ def is_master(config):
4142
return not hasattr(config, 'slaveinput')
4243

4344

44-
def get_launch_attributes(rp_launch_attributes):
45-
"""Generate list of launch attributes for RP.
46-
47-
:param list rp_launch_attributes: rp_launch_attributes option value
48-
:return list: List of dictionaries to be passed to the
49-
RP Python client
50-
"""
51-
launch_attrs = []
52-
for rp_attr in rp_launch_attributes:
53-
try:
54-
key, value = rp_attr.split(':')
55-
attr_dict = {'key': key, 'value': value}
56-
except ValueError:
57-
attr_dict = {'value': rp_attr}
58-
59-
if all(value for value in attr_dict.values()):
60-
launch_attrs.append(attr_dict)
61-
continue
62-
log.debug('Failed to process "{0}" attribute, attribute value'
63-
' should not be empty.'.format(rp_attr))
64-
return launch_attrs
65-
66-
6745
@pytest.mark.optionalhook
6846
def pytest_configure_node(node):
6947
"""
@@ -110,7 +88,7 @@ def pytest_sessionstart(session):
11088
session.config.py_test_service.rp = None
11189
return
11290

113-
attributes = get_launch_attributes(
91+
attributes = get_attributes(
11492
session.config.getini('rp_launch_attributes'))
11593
session.config.py_test_service.start_launch(
11694
session.config.option.rp_launch,

pytest_reportportal/service.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
from six import with_metaclass
3232
from six.moves import queue
3333

34+
from .helpers import get_attributes
35+
3436
log = logging.getLogger(__name__)
3537

3638

@@ -630,13 +632,12 @@ def get_marker_value(item, keyword):
630632
get_marker = getattr(item, "get_closest_marker")
631633
except AttributeError:
632634
get_marker = getattr(item, "get_marker")
633-
attributes = [{"value": get_marker_value(item, k)}
634-
for k in item.keywords if get_marker(k) is not None
635-
and k not in self.ignored_attributes]
636635

637-
attributes.extend([{"value": tag} for tag in
638-
item.session.config.getini('rp_tests_attributes')])
639-
return attributes
636+
raw_attrs = [get_marker_value(item, k)
637+
for k in item.keywords if get_marker(k) is not None
638+
and k not in self.ignored_attributes]
639+
raw_attrs.extend(item.session.config.getini('rp_tests_attributes'))
640+
return get_attributes(raw_attrs)
640641

641642
def _get_parameters(self, item):
642643
"""

tests/test_helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""This modules includes unit tests for helpers."""
2+
3+
from pytest_reportportal.helpers import get_attributes
4+
5+
6+
def test_get__attributes():
7+
"""Test get_attributes functionality."""
8+
expected_out = [{'value': 'Tag'}, {'key': 'Key', 'value': 'Value'}]
9+
out = get_attributes(['Tag', 'Key:Value', ''])
10+
assert expected_out == out

tests/test_plugin.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from reportportal_client.errors import ResponseError
1111
from pytest_reportportal.plugin import (
12-
get_launch_attributes,
1312
pytest_configure,
1413
pytest_sessionstart
1514
)
@@ -78,13 +77,6 @@ def test_uuid_env_var_override(mocked_session):
7877
assert kwargs.get('uuid') == 'foobar'
7978

8079

81-
def test_get_launch_attributes():
82-
"""Test get_launch_attributes functionality."""
83-
expected_out = [{'value': 'Tag'}, {'key': 'Key', 'value': 'Value'}]
84-
out = get_launch_attributes(['Tag', 'Key:Value', ''])
85-
assert expected_out == out
86-
87-
8880
def test_portal_on_maintenance(mocked_session):
8981
"""
9082
Test if portal on maintenance.

tests/test_service.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ def test_item_attributes(mocked_item, rp_service):
1212

1313
def getini(option):
1414
if option == 'rp_tests_attributes':
15-
return ['ini_marker']
15+
return ['ini_marker', 'test_ini_key:test_ini_value']
1616

1717
def get_closest_marker(name):
18-
return {'test_marker': pytest.mark.test_marker}.get(name)
18+
return {'test_marker': pytest.mark.test_marker,
19+
'test_decorator_key':
20+
pytest.mark.test_decorator_key('test_decorator_value')
21+
}.get(name)
1922

2023
class NodeKeywords(object):
21-
_keywords = ['pytestmark', 'ini_marker', 'test_marker']
24+
_keywords = ['pytestmark',
25+
'ini_marker',
26+
'test_marker',
27+
'test_decorator_key',
28+
'test_ini_key']
2229

2330
def __iter__(self):
2431
return iter(self._keywords)
@@ -27,7 +34,12 @@ def __iter__(self):
2734
mocked_item.keywords = NodeKeywords()
2835
mocked_item.get_closest_marker = get_closest_marker
2936
markers = rp_service._get_item_markers(mocked_item)
30-
assert markers == [{'value': 'test_marker'}, {'value': 'ini_marker'}]
37+
assert markers == [{'value': 'test_marker'},
38+
{'key': 'test_decorator_key',
39+
'value': 'test_decorator_value'},
40+
{'value': 'ini_marker'},
41+
{'key': 'test_ini_key',
42+
'value': 'test_ini_value'}]
3143

3244

3345
def test_get_item_parameters(mocked_item, rp_service):
@@ -45,7 +57,7 @@ def test_get_item_parameters(mocked_item, rp_service):
4557
@mock.patch('reportportal_client.service.ReportPortalService.start_test_item')
4658
def test_code_ref_bypass(mocked_item_start, mocked_item, mocked_session,
4759
rp_service):
48-
""" Test that a test code reference constructed and bypassed to a client
60+
""" Test that a test code reference constructed and bypassed to a client.
4961
5062
:param mocked_item_start: mocked start_test_item method reference
5163
:param mocked_item: a mocked test item

0 commit comments

Comments
 (0)