11import logging
22from time import time
3+ from six import with_metaclass
34from reportportal_client import (
45 ReportPortalService , FinishExecutionRQ , StartLaunchRQ , StartTestItemRQ ,
56 FinishTestItemRQ , SaveLogRQ )
@@ -9,127 +10,141 @@ def timestamp():
910 return str (int (time () * 1000 ))
1011
1112
12- class PyTestService (object ):
13+ class Singleton (type ):
14+ _instances = {}
1315
14- RP = None
15- TEST_ITEM_STACK = []
16- launch_id = None
16+ def __call__ (cls , * args , ** kwargs ):
17+ if cls not in cls ._instances :
18+ cls ._instances [cls ] = super (Singleton , cls ).__call__ (
19+ * args , ** kwargs )
20+ return cls ._instances [cls ]
1721
18- @staticmethod
19- def init_service (endpoint , project , uuid ):
2022
21- if PyTestService .RP is None :
23+ class PyTestServiceClass (with_metaclass (Singleton , object )):
24+
25+ def __init__ (self ):
26+
27+ self .RP = None
28+ self .TEST_ITEM_STACK = []
29+ self .launch_id = None
30+ self .loglevel_map = {
31+ 0 : "TRACE" ,
32+ 10 : "DEBUG" ,
33+ 20 : "INFO" ,
34+ 30 : "WARNING" ,
35+ 40 : "ERROR" }
36+
37+ def init_service (self , endpoint , project , uuid ):
38+
39+ if self .RP is None :
2240 logging .debug (
2341 msg = "ReportPortal - Init service: "
2442 "endpoint={0}, project={1}, uuid={2}" .
2543 format (endpoint , project , uuid ))
26- PyTestService .RP = ReportPortalService (
44+ self .RP = ReportPortalService (
2745 endpoint = endpoint ,
2846 project = project ,
2947 token = uuid )
3048 else :
31- raise Exception ( "PyTest is initialized" )
32- return PyTestService .RP
49+ logging . debug ( "The pytest is already initialized" )
50+ return self .RP
3351
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" ]
52+ def start_launch (
53+ self , launch_name = None , mode = None , tags = None , launch = None ):
54+ # In next versions launch object(suite, testcase)
55+ # could be set as parameter
3856 sl_pt = StartLaunchRQ (
3957 name = launch_name ,
4058 start_time = timestamp (),
41- description = "PyTest_Launch" ,
59+ description = 'Pytest Launch' ,
4260 mode = mode ,
4361 tags = tags )
4462 logging .debug (msg = "ReportPortal - Start launch: "
4563 "request_body={0}" .format (sl_pt .data ))
46- req_data = PyTestService .RP .start_launch (sl_pt )
64+ req_data = self .RP .start_launch (sl_pt )
4765 logging .debug (msg = "ReportPortal - Launch started: "
4866 "response_body={0}" .format (req_data .raw ))
49- PyTestService .launch_id = req_data .id
67+ self .launch_id = req_data .id
5068
51- PyTestService .TEST_ITEM_STACK .append ((None , "SUITE" ))
69+ self .TEST_ITEM_STACK .append ((None , "SUITE" ))
5270 logging .debug (
5371 msg = "ReportPortal - Stack: {0}" .
54- format (PyTestService .TEST_ITEM_STACK ))
72+ format (self .TEST_ITEM_STACK ))
5573
56- @staticmethod
57- def start_pytest_item (test_item = None ):
74+ def start_pytest_item (self , test_item = None ):
5875 try :
5976 # for common items
6077 item_description = test_item .function .__doc__
6178 except AttributeError :
6279 # doctest has no `function` attribute
6380 item_description = test_item .reportinfo ()[2 ]
64-
6581 start_rq = StartTestItemRQ (
6682 name = test_item .name ,
6783 description = item_description ,
6884 tags = ['PyTest Item Tag' ],
6985 start_time = timestamp (),
70- launch_id = PyTestService .launch_id ,
86+ launch_id = self .launch_id ,
7187 type = "TEST" )
7288
73- parent_item_id = PyTestService ._get_top_id_from_stack ()
89+ parent_item_id = self ._get_top_id_from_stack ()
7490
7591 logging .debug (
7692 msg = "ReportPortal - Start TestItem: "
7793 "request_body={0}, parent_item={1}" .format (
7894 start_rq .data , parent_item_id ))
7995
80- req_data = PyTestService .RP .start_test_item (
96+ req_data = self .RP .start_test_item (
8197 parent_item_id = parent_item_id , start_test_item_rq = start_rq )
8298
83- PyTestService .TEST_ITEM_STACK .append ((req_data .id , "TEST" ))
99+ self .TEST_ITEM_STACK .append ((req_data .id , "TEST" ))
84100 logging .debug (
85101 msg = "ReportPortal - Stack: {0}" .
86- format (PyTestService .TEST_ITEM_STACK ))
102+ format (self .TEST_ITEM_STACK ))
87103
88- @staticmethod
89- def finish_pytest_item (status , issue = None ):
104+ def finish_pytest_item (self , status , issue = None ):
90105 fta_rq = FinishTestItemRQ (end_time = timestamp (),
91106 status = status ,
92107 issue = issue )
93108
94- test_item_id = PyTestService ._get_top_id_from_stack ()
109+ test_item_id = self ._get_top_id_from_stack ()
95110 logging .debug (
96111 msg = "ReportPortal - Finish TetsItem:"
97112 " request_body={0}, test_id={1}" .
98113 format (fta_rq .data , test_item_id ))
99- PyTestService .RP .finish_test_item (
114+ self .RP .finish_test_item (
100115 item_id = test_item_id ,
101116 finish_test_item_rq = fta_rq )
102- PyTestService .TEST_ITEM_STACK .pop ()
117+ self .TEST_ITEM_STACK .pop ()
103118 logging .debug (
104119 msg = "ReportPortal - Stack: {0}" .
105- format (PyTestService .TEST_ITEM_STACK ))
120+ format (self .TEST_ITEM_STACK ))
106121
107- @ staticmethod
108- def finish_launch ( status ):
122+ def finish_launch ( self , launch = None , status = "rp_launch" ):
123+ # TO finish launch session str parameter is needed
109124 fl_rq = FinishExecutionRQ (
110125 end_time = timestamp (),
111126 status = status )
112- launch_id = PyTestService .launch_id
127+ launch_id = self .launch_id
113128 logging .debug (msg = "ReportPortal - Finish launch: "
114129 "request_body={0}, launch_id={1}" .format (fl_rq .data ,
115130 launch_id ))
116- PyTestService .RP .finish_launch (launch_id , fl_rq )
117- PyTestService .TEST_ITEM_STACK .pop ()
131+ self .RP .finish_launch (launch_id , fl_rq )
132+ self .TEST_ITEM_STACK .pop ()
118133 logging .debug (
119134 msg = "ReportPortal - Stack: {0}" .
120- format (PyTestService .TEST_ITEM_STACK ))
135+ format (self .TEST_ITEM_STACK ))
121136
122- @staticmethod
123- def _get_top_id_from_stack ():
137+ def _get_top_id_from_stack (self ):
124138 try :
125- return PyTestService .TEST_ITEM_STACK [- 1 ][0 ]
139+ return self .TEST_ITEM_STACK [- 1 ][0 ]
126140 except IndexError :
127141 return None
128142
129- @staticmethod
130- def post_log (message , log_level = "DEBUG" ):
131- sl_rq = SaveLogRQ (item_id = PyTestService ._get_top_id_from_stack (),
143+ def post_log (self , message , log_level = "INFO" ):
144+ sl_rq = SaveLogRQ (item_id = self ._get_top_id_from_stack (),
132145 time = timestamp (), message = message ,
133- level = log_level )
134- PyTestService .RP .log (sl_rq )
146+ level = self .loglevel_map [log_level ])
147+ self .RP .log (sl_rq )
148+
135149
150+ PyTestService = PyTestServiceClass ()
0 commit comments