|
| 1 | +import logging |
| 2 | +from time import time |
| 3 | +from reportportal_client import ( |
| 4 | + ReportPortalService, FinishExecutionRQ, StartLaunchRQ, StartTestItemRQ, |
| 5 | + FinishTestItemRQ) |
| 6 | + |
| 7 | + |
| 8 | +def timestamp(): |
| 9 | + return str(int(time() * 1000)) |
| 10 | + |
| 11 | + |
| 12 | +class PyTestService(object): |
| 13 | + |
| 14 | + RP = None |
| 15 | + TEST_ITEM_STACK = [] |
| 16 | + launch_id = None |
| 17 | + # TODO: Implement and investigate the logging in PyTest |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + def init_service(endpoint, project, uuid): |
| 21 | + |
| 22 | + if PyTestService.RP is None: |
| 23 | + logging.debug( |
| 24 | + msg="ReportPortal - Init service: " |
| 25 | + "endpoint={0}, project={1}, uuid={2}". |
| 26 | + format(endpoint, project, uuid)) |
| 27 | + PyTestService.RP = ReportPortalService( |
| 28 | + endpoint=endpoint, |
| 29 | + project=project, |
| 30 | + token=uuid) |
| 31 | + else: |
| 32 | + raise Exception("PyTest is initialized") |
| 33 | + return PyTestService.RP |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def start_launch(launch_name=None, mode=None): |
| 37 | + # TODO: Tags, values could be moved to separated module like helper |
| 38 | + tags = ["PyTest"] |
| 39 | + sl_pt = StartLaunchRQ( |
| 40 | + name=launch_name, |
| 41 | + start_time=timestamp(), |
| 42 | + description="PyTest_Launch", |
| 43 | + mode=mode, |
| 44 | + tags=tags) |
| 45 | + logging.debug(msg="ReportPortal - Start launch: " |
| 46 | + "request_body={0}".format(sl_pt.data)) |
| 47 | + req_data = PyTestService.RP.start_launch(sl_pt) |
| 48 | + logging.debug(msg="ReportPortal - Launch started: " |
| 49 | + "response_body={0}".format(req_data.raw)) |
| 50 | + PyTestService.launch_id = req_data.id |
| 51 | + |
| 52 | + PyTestService.TEST_ITEM_STACK.append((None, "SUITE")) |
| 53 | + logging.debug( |
| 54 | + msg="ReportPortal - Stack: {0}". |
| 55 | + format(PyTestService.TEST_ITEM_STACK)) |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def start_pytest_item(test_item=None): |
| 59 | + try: |
| 60 | + # for common items |
| 61 | + item_description = test_item.function.__doc__ |
| 62 | + except AttributeError: |
| 63 | + # doctest has no `function` attribute |
| 64 | + item_description = test_item.reportinfo()[2] |
| 65 | + |
| 66 | + start_rq = StartTestItemRQ( |
| 67 | + name=test_item.name, |
| 68 | + description=item_description, |
| 69 | + tags=['PyTest Item Tag'], |
| 70 | + start_time=timestamp(), |
| 71 | + launch_id=PyTestService.launch_id, |
| 72 | + type="TEST") |
| 73 | + |
| 74 | + parent_item_id = PyTestService._get_top_id_from_stack() |
| 75 | + |
| 76 | + logging.debug( |
| 77 | + msg="ReportPortal - Start TestItem: " |
| 78 | + "request_body={0}, parent_item={1}".format( |
| 79 | + start_rq.data, parent_item_id)) |
| 80 | + |
| 81 | + req_data = PyTestService.RP.start_test_item( |
| 82 | + parent_item_id=parent_item_id, start_test_item_rq=start_rq) |
| 83 | + |
| 84 | + PyTestService.TEST_ITEM_STACK.append((req_data.id, "TEST")) |
| 85 | + logging.debug( |
| 86 | + msg="ReportPortal - Stack: {0}". |
| 87 | + format(PyTestService.TEST_ITEM_STACK)) |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def finish_pytest_item(status, issue=None): |
| 91 | + fta_rq = FinishTestItemRQ(end_time=timestamp(), |
| 92 | + status=status, |
| 93 | + issue=issue) |
| 94 | + |
| 95 | + test_item_id = PyTestService._get_top_id_from_stack() |
| 96 | + logging.debug( |
| 97 | + msg="ReportPortal - Finish TetsItem:" |
| 98 | + " request_body={0}, test_id={1}". |
| 99 | + format(fta_rq.data, test_item_id)) |
| 100 | + PyTestService.RP.finish_test_item( |
| 101 | + item_id=test_item_id, |
| 102 | + finish_test_item_rq=fta_rq) |
| 103 | + PyTestService.TEST_ITEM_STACK.pop() |
| 104 | + logging.debug( |
| 105 | + msg="ReportPortal - Stack: {0}". |
| 106 | + format(PyTestService.TEST_ITEM_STACK)) |
| 107 | + |
| 108 | + @staticmethod |
| 109 | + def finish_launch(status): |
| 110 | + fl_rq = FinishExecutionRQ( |
| 111 | + end_time=timestamp(), |
| 112 | + status=status) |
| 113 | + launch_id = PyTestService.launch_id |
| 114 | + logging.debug(msg="ReportPortal - Finish launch: " |
| 115 | + "request_body={0}, launch_id={1}".format(fl_rq.data, |
| 116 | + launch_id)) |
| 117 | + PyTestService.RP.finish_launch(launch_id, fl_rq) |
| 118 | + PyTestService.TEST_ITEM_STACK.pop() |
| 119 | + logging.debug( |
| 120 | + msg="ReportPortal - Stack: {0}". |
| 121 | + format(PyTestService.TEST_ITEM_STACK)) |
| 122 | + |
| 123 | + @staticmethod |
| 124 | + def _get_top_id_from_stack(): |
| 125 | + try: |
| 126 | + return PyTestService.TEST_ITEM_STACK[-1][0] |
| 127 | + except IndexError: |
| 128 | + return None |
0 commit comments