@@ -26,24 +26,18 @@ pip install reportportal-client
26
26
Main classes are:
27
27
28
28
- reportportal_client.ReportPortalService
29
- - reportportal_client.StartLaunchRQ
30
- - reportportal_client.StartTestItemRQ
31
- - reportportal_client.FinishTestItemRQ
32
- - reportportal_client.FinishExecutionRQ
33
- - reportportal_client.SaveLogRQ
29
+ - reportportal_client.ReportPortalServiceAsync
34
30
35
31
Basic usage example:
36
32
37
33
``` python
38
34
import os
39
35
import subprocess
40
- from time import time
36
+ import traceback
41
37
from mimetypes import guess_type
38
+ from time import time
42
39
43
- from reportportal_client import (ReportPortalService,
44
- FinishExecutionRQ,
45
- StartLaunchRQ, StartTestItemRQ,
46
- FinishTestItemRQ, SaveLogRQ)
40
+ from reportportal_client import ReportPortalServiceAsync
47
41
48
42
49
43
def timestamp ():
@@ -57,73 +51,75 @@ token = "1adf271d-505f-44a8-ad71-0afbdf8c83bd"
57
51
launch_name = " Test launch"
58
52
launch_doc = " Testing logging with attachment."
59
53
60
- service = ReportPortalService(endpoint = endpoint, project = project, token = token)
61
54
62
- # Create start launch request.
63
- sl_rq = StartLaunchRQ(name = launch_name,
64
- start_time = timestamp(),
65
- description = launch_doc)
55
+ def my_error_handler (exc_info ):
56
+ """
57
+ This callback function will be called by async service client when error occurs.
58
+ Return True if error is not critical and you want to continue work.
59
+ :param exc_info: result of sys.exc_info() -> (type, value, traceback)
60
+ :return:
61
+ """
62
+ print (" Error occured: {} " .format(exc_info[1 ]))
63
+ traceback.print_exception(* exc_info)
66
64
67
- # Start launch.
68
- launch = service.start_launch(sl_rq)
69
65
70
- # Create start test item request.
71
- sti_rq = StartTestItemRQ( name = " Test Case " ,
72
- description = " First Test Case " ,
73
- tags = [ " Image " , " Smoke " ],
74
- start_time = timestamp() ,
75
- launch_id = launch.id ,
76
- type = " TEST " )
66
+ service = ReportPortalServiceAsync( endpoint = endpoint, project = project,
67
+ token = token, error_handler = my_error_handler)
68
+
69
+ # Start launch.
70
+ launch = service.start_launch( name = launch_name ,
71
+ start_time = timestamp() ,
72
+ description = launch_doc )
77
73
78
74
# Start test item.
79
- test = service.start_test_item(parent_item_id = None , start_test_item_rq = sti_rq)
75
+ test = service.start_test_item(name = " Test Case" ,
76
+ description = " First Test Case" ,
77
+ tags = [" Image" , " Smoke" ],
78
+ start_time = timestamp(),
79
+ item_type = " TEST" )
80
80
81
81
# Create text log message with INFO level.
82
- service.log(SaveLogRQ(item_id = test.id,
83
- time = timestamp(),
84
- message = " Hello World!" ,
85
- level = " INFO" ))
82
+ service.log(time = timestamp(),
83
+ message = " Hello World!" ,
84
+ level = " INFO" )
86
85
87
86
# Create log message with attached text output and WARN level.
88
- service.attach(SaveLogRQ(item_id = test.id,
89
- time = timestamp(),
90
- message = " Too high memory usage!" ,
91
- level = " WARN" ),
92
- name = " free_memory.txt" ,
93
- data = subprocess.check_output(" free -h" .split()))
94
-
95
- # Create log message with piped binary file, INFO level and custom mimetype.
87
+ service.log(time = timestamp(),
88
+ message = " Too high memory usage!" ,
89
+ level = " WARN" ,
90
+ attachment = {
91
+ " name" : " free_memory.txt" ,
92
+ " data" : subprocess.check_output(" ps" .split()),
93
+ " mime" : " text/plain"
94
+ })
95
+
96
+ # Create log message with binary file, INFO level and custom mimetype.
96
97
image = " /tmp/image.png"
97
- sl_rq = SaveLogRQ(test.id, timestamp(), " Screen shot of issue." , " INFO" )
98
98
with open (image, " rb" ) as fh:
99
- service.attach(save_log_rq = sl_rq,
100
- name = os.path.basename(image),
101
- data = fh,
102
- mime = guess_type(image)[0 ] or " application/octet-stream" )
103
-
104
- # Create log message with binary data and INFO level.
105
- filebin = " /tmp/file.bin"
106
- with open (filebin, " rb" ) as fd:
107
- bindata = fd.read()
108
- # Note here that we pass binary data instead of file handle.
109
- service.attach(SaveLogRQ(item_id = test.id,
110
- time = timestamp(),
111
- message = " Binary data file." ,
112
- level = " INFO" ),
113
- name = " file.bin" ,
114
- data = bindata,
115
- mime = " application/octet-stream" )
116
-
117
- # Create finish test item request.
118
- fti_rq = FinishTestItemRQ(end_time = timestamp(), status = " PASSED" )
99
+ attachment = {
100
+ " name" : os.path.basename(image),
101
+ " data" : fh.read(),
102
+ " mime" : guess_type(image)[0 ] or " application/octet-stream"
103
+ }
104
+ service.log(timestamp(), " Screen shot of issue." , " INFO" , attachment)
105
+
106
+ # Create log message supplying only contents
107
+ service.log(
108
+ timestamp(),
109
+ " running processes" ,
110
+ " INFO" ,
111
+ attachment = subprocess.check_output(" ps aux" .split()))
119
112
120
113
# Finish test item.
121
- service.finish_test_item(item_id = test.id, finish_test_item_rq = fti_rq )
114
+ service.finish_test_item(end_time = timestamp(), status = " PASSED " )
122
115
123
- # Create finish launch request.
124
- fl_rq = FinishExecutionRQ(end_time = timestamp(), status = " PASSED" )
125
116
# Finish launch.
126
- service.finish_launch(launch.id, fl_rq)
117
+ service.finish_launch(end_time = timestamp())
118
+
119
+ # Due to async nature of the service we need to call terminate() method which
120
+ # ensures all pending requests to server are proccessed.
121
+ # Failure to call terminate() may result in lost data.
122
+ service.terminate()
127
123
```
128
124
129
125
0 commit comments