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