Skip to content

Commit

Permalink
Add support for launch and test attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
iivanou authored Aug 7, 2020
2 parents 6399511 + d9fc77c commit 3c8deaf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ NOT REQUIRED:
- ID of existing Report Portal launch
--variable RP_LAUNCH_DOC:"some_documentation_for_launch"
- Description for the launch
--variable RP_LAUNCH_TAGS:"RF Smoke"
- Space-separated list of tags for the launch
--variable RP_LAUNCH_ATTRIBUTES:"RF tag_name:tag_value"
- Space-separated list of tags/attributes for the launch
--variable RP_TEST_ATTRIBUTES:"Long"
- Space-separated list of tags/attributes for the tests
--variable RP_LOG_BATCH_SIZE:"10"
- Default value is "20", affects size of async batch log requests
```
Expand Down
20 changes: 15 additions & 5 deletions robotframework_reportportal/listener.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging

from reportportal_client.helpers import gen_attributes

from .variables import Variables
from .model import Keyword, Test, Suite, LogMessage
from .service import RobotService
Expand All @@ -15,8 +17,10 @@ def start_launch(launch):
launch.doc = Variables.launch_doc
logging.debug("ReportPortal - Start Launch: {0}".format(
launch.attributes))
RobotService.start_launch(launch_name=Variables.launch_name,
launch=launch)
RobotService.start_launch(
launch_name=Variables.launch_name,
attributes=gen_attributes(Variables.launch_attributes),
description=launch.doc)
else:
RobotService.rp.launch_id = Variables.launch_id

Expand All @@ -35,8 +39,10 @@ def start_suite(name, attributes):
else:
logging.debug("ReportPortal - Start Suite: {0}".format(attributes))
parent_id = items[-1][0] if items else None
item_id = RobotService.start_suite(name=name, suite=suite,
parent_item_id=parent_id)
item_id = RobotService.start_suite(
name=name,
suite=suite,
parent_item_id=parent_id)
items.append((item_id, parent_id))


Expand All @@ -56,7 +62,11 @@ def start_test(name, attributes):
logging.debug("ReportPortal - Start Test: {0}".format(attributes))
parent_item_id = items[-1][0]
items.append((
RobotService.start_test(test=test, parent_item_id=parent_item_id),
RobotService.start_test(
test=test,
parent_item_id=parent_item_id,
attributes=gen_attributes(Variables.test_attributes + test.tags),
),
parent_item_id))


Expand Down
39 changes: 34 additions & 5 deletions robotframework_reportportal/service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import logging
import traceback
from time import time
from reportportal_client import ReportPortalService
from reportportal_client.service import (
_dict_to_payload,
ReportPortalService
)

from .variables import Variables


def async_error_handler(exc_info):
Expand Down Expand Up @@ -32,6 +37,19 @@ class RobotService(object):
"ERROR": "ERROR"
}

@staticmethod
def _get_launch_attributes(cmd_attrs):
"""Generate launch attributes including both system and user ones.
:param list cmd_attrs: List for attributes from the pytest.ini file
"""
attributes = cmd_attrs or []
system_info = RobotService.rp.get_system_information(
Variables.agent_name)
system_info['system'] = True
system_attributes = _dict_to_payload(system_info)
return attributes + system_attributes

@staticmethod
def init_service(endpoint, project, uuid):
if RobotService.rp is None:
Expand All @@ -52,11 +70,20 @@ def terminate_service():
RobotService.rp.terminate()

@staticmethod
def start_launch(launch_name, mode=None, launch=None):
def start_launch(launch_name, attributes=None, description=None, mode=None):
"""Call start_launch method of the common client.
:param launch_name: Launch name
:param attributes: Launch attributes
:param description: Launch description
:param mode: Launch mode
:return: launch UUID
"""
sl_pt = {
"attributes": RobotService._get_launch_attributes(attributes),
"name": launch_name,
"start_time": timestamp(),
"description": launch.doc,
"description": description,
"mode": mode
}
logging.debug("ReportPortal - Start launch: "
Expand All @@ -74,9 +101,10 @@ def finish_launch(launch=None):
RobotService.rp.finish_launch(**fl_rq)

@staticmethod
def start_suite(name=None, suite=None, parent_item_id=None):
def start_suite(name=None, suite=None, parent_item_id=None, attributes=None):
start_rq = {
"name": name,
"attributes": attributes,
"description": suite.doc,
"start_time": timestamp(),
"item_type": "SUITE",
Expand All @@ -101,11 +129,12 @@ def finish_suite(item_id, issue=None, suite=None):
RobotService.rp.finish_test_item(**fta_rq)

@staticmethod
def start_test(test=None, parent_item_id=None):
def start_test(test=None, parent_item_id=None, attributes=None):
# Item type should be sent as "STEP" until we upgrade to RPv6.
# Details at: https://github.com/reportportal/agent-Python-RobotFramework/issues/56
start_rq = {
"name": test.name,
"attributes": attributes,
"description": test.doc,
"start_time": timestamp(),
"item_type": "STEP",
Expand Down
7 changes: 5 additions & 2 deletions robotframework_reportportal/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ def get_variable(name, default=None):


class Variables(object):
agent_name = 'robotframework-reportportal'
uuid = None
endpoint = None
launch_name = None
project = None
launch_doc = None
log_batch_size = None
launch_tags = None
launch_attributes = None
launch_id = None
test_attributes = None

@staticmethod
def check_variables():
Expand All @@ -39,7 +41,8 @@ def check_variables():
raise RobotServiceException(
"Missing parameter RP_PROJECT for robot run\n"
"You should pass -v RP_PROJECT:<project_name_value>")
Variables.launch_attributes = get_variable("RP_LAUNCH_ATTRIBUTES", default="").split()
Variables.launch_id = get_variable("RP_LAUNCH_UUID", default=None)
Variables.launch_doc = get_variable("RP_LAUNCH_DOC", default=None)
Variables.launch_tags = get_variable("RP_LAUNCH_TAGS", default="").split(" ")
Variables.log_batch_size = int(get_variable("RP_LOG_BATCH_SIZE", default="20"))
Variables.test_attributes = get_variable("RP_TEST_ATTRIBUTES", default="").split()
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from setuptools import setup, find_packages


__version__ = '5.0.2'
__version__ = '5.0.3'

requirements = [
"reportportal-client>=5.0.0",
"reportportal-client>=5.0.5",
"six",
]

Expand Down

0 comments on commit 3c8deaf

Please sign in to comment.