@@ -33,13 +33,11 @@ Basic usage example:
33
33
``` python
34
34
import os
35
35
import subprocess
36
- from time import time
36
+ import traceback
37
37
from mimetypes import guess_type
38
+ from time import time
38
39
39
- from reportportal_client import (ReportPortalService,
40
- FinishExecutionRQ,
41
- StartLaunchRQ, StartTestItemRQ,
42
- FinishTestItemRQ, SaveLogRQ)
40
+ from reportportal_client import ReportPortalServiceAsync
43
41
44
42
45
43
def timestamp ():
@@ -53,73 +51,75 @@ token = "1adf271d-505f-44a8-ad71-0afbdf8c83bd"
53
51
launch_name = " Test launch"
54
52
launch_doc = " Testing logging with attachment."
55
53
56
- service = ReportPortalService(endpoint = endpoint, project = project, token = token)
57
54
58
- # Create start launch request.
59
- sl_rq = StartLaunchRQ(name = launch_name,
60
- start_time = timestamp(),
61
- 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)
62
64
63
- # Start launch.
64
- launch = service.start_launch(sl_rq)
65
65
66
- # Create start test item request.
67
- sti_rq = StartTestItemRQ( name = " Test Case " ,
68
- description = " First Test Case " ,
69
- tags = [ " Image " , " Smoke " ],
70
- start_time = timestamp() ,
71
- launch_id = launch.id ,
72
- 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 )
73
73
74
74
# Start test item.
75
- 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" )
76
80
77
81
# Create text log message with INFO level.
78
- service.log(SaveLogRQ(item_id = test.id,
79
- time = timestamp(),
80
- message = " Hello World!" ,
81
- level = " INFO" ))
82
+ service.log(time = timestamp(),
83
+ message = " Hello World!" ,
84
+ level = " INFO" )
82
85
83
86
# Create log message with attached text output and WARN level.
84
- service.attach(SaveLogRQ(item_id = test.id,
85
- time = timestamp(),
86
- message = " Too high memory usage!" ,
87
- level = " WARN" ),
88
- name = " free_memory.txt" ,
89
- data = subprocess.check_output(" free -h" .split()))
90
-
91
- # 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.
92
97
image = " /tmp/image.png"
93
- sl_rq = SaveLogRQ(test.id, timestamp(), " Screen shot of issue." , " INFO" )
94
98
with open (image, " rb" ) as fh:
95
- service.attach(save_log_rq = sl_rq,
96
- name = os.path.basename(image),
97
- data = fh,
98
- mime = guess_type(image)[0 ] or " application/octet-stream" )
99
-
100
- # Create log message with binary data and INFO level.
101
- filebin = " /tmp/file.bin"
102
- with open (filebin, " rb" ) as fd:
103
- bindata = fd.read()
104
- # Note here that we pass binary data instead of file handle.
105
- service.attach(SaveLogRQ(item_id = test.id,
106
- time = timestamp(),
107
- message = " Binary data file." ,
108
- level = " INFO" ),
109
- name = " file.bin" ,
110
- data = bindata,
111
- mime = " application/octet-stream" )
112
-
113
- # Create finish test item request.
114
- 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()))
115
112
116
113
# Finish test item.
117
- service.finish_test_item(item_id = test.id, finish_test_item_rq = fti_rq )
114
+ service.finish_test_item(end_time = timestamp(), status = " PASSED " )
118
115
119
- # Create finish launch request.
120
- fl_rq = FinishExecutionRQ(end_time = timestamp(), status = " PASSED" )
121
116
# Finish launch.
122
- 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()
123
123
```
124
124
125
125
0 commit comments