-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathallure-ruby-commons.rb
248 lines (217 loc) · 6.68 KB
/
allure-ruby-commons.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# rubocop:disable Naming/FileName
# frozen_string_literal: true
require "require_all"
require "securerandom"
require_rel "allure_ruby_commons/**/*rb"
# Namespace for classes that handle allure report generation and different framework adaptors
module Allure
# Set lifecycle object
# @param [AllureLifecycle] lifecycle
# @return [void]
def self.lifecycle=(lifecycle)
Thread.current[:lifecycle] = lifecycle
end
extend self # rubocop:disable Style/ModuleFunction
# Get thread specific allure lifecycle object
# @return [AllureLifecycle]
def lifecycle
Thread.current[:lifecycle] ||= AllureLifecycle.new
end
# Get allure configuration
# @return [Config]
def configuration
Config.instance
end
# Set allure configuration
# @yieldparam [Config]
# @yieldreturn [void]
# @return [void]
def configure
yield(configuration)
end
# Add epic to current test case
# @param [String] value
# @return [void]
def epic(value)
replace_label(ResultUtils::EPIC_LABEL_NAME, value)
end
# Add feature to current test case
# @param [String] value
# @return [void]
def feature(value)
replace_label(ResultUtils::FEATURE_LABEL_NAME, value)
end
# Add story to current test case
# @param [String] value
# @return [void]
def story(value)
replace_label(ResultUtils::STORY_LABEL_NAME, value)
end
# Add suite to current test case
# @param [String] value
# @return [void]
def suite(value)
replace_label(ResultUtils::SUITE_LABEL_NAME, value)
end
# Add tag to current test case
# @param [String] value
# @return [void]
def tag(value)
label(ResultUtils::TAG_LABEL_NAME, value)
end
# Add label to current test case
# @param [String] name
# @param [String] value
# @return [void]
def label(name, value)
lifecycle.update_test_case do |test_case|
test_case.labels.push(Label.new(name, value))
end
end
# Replace label in current test case
#
# @param [String] name
# @param [String] value
# @return [void]
def replace_label(name, value)
lifecycle.update_test_case do |test_case|
present = test_case.labels.detect { |l| l.name == name }
return label(name, value) unless present
test_case.labels.map! { |l| l.name == name ? Label.new(name, value) : l }
end
end
# Add description to current test case
# @param [String] description
# @return [void]
def add_description(description)
lifecycle.update_test_case do |test_case|
test_case.description = description
end
end
# Add html description to current test case
# @param [String] description_html
# @return [void]
def description_html(description_html)
lifecycle.update_test_case do |test_case|
test_case.description_html = description_html
end
end
# Add parameter to current test case
# @param [String] name
# @param [String] value
# @return [void]
def parameter(name, value)
lifecycle.update_test_case do |test_case|
test_case.parameters.push(Parameter.new(name, value))
end
end
# Add tms link to current test case
# @param [String] name
# @param [String] url
# @return [void]
def tms(name, url)
add_link(name: name, url: url, type: ResultUtils::TMS_LINK_TYPE)
end
# Add issue linkt to current test case
# @param [String] name
# @param [String] url
# @return [void]
def issue(name, url)
add_link(name: name, url: url, type: ResultUtils::ISSUE_LINK_TYPE)
end
# Add link to current test case
# @param [String ] url
# @param [String] name
# @param [String] type type of the link used to display link icon
# @return [void]
def add_link(url:, name: nil, type: "custom")
lifecycle.update_test_case do |test_case|
test_case.links.push(Link.new(type, name || url, url))
end
end
# Add attachment to current test case or step
# @param [String] name Attachment name
# @param [File, String] source File or string to save as attachment
# @param [String] type attachment type defined in {ContentType} or any other valid mime type
# @param [Boolean] test_case add attachment to current test case instead of test step
# @return [void]
def add_attachment(name:, source:, type:, test_case: false)
lifecycle.add_attachment(name: name, source: source, type: type, test_case: test_case)
end
# Manually create environment.properties file
# if this method is called before test run started and
# option clean_results_directory is enabled, the file will be deleted
# @param [Hash<Symbol, String>, Proc] environment
# @return [void]
def add_environment(environment)
lifecycle.write_environment(environment)
end
# Manually create categories.json file
# if this method is called before test run started and
# option clean_results_directory is enabled, the file will be deleted
# @param [File, Array<Category>] categories
# @return [void]
def add_categories(categories)
lifecycle.write_categories(categories)
end
# Set test case status detail to flaky
#
# @return [void]
def set_flaky
lifecycle.update_test_case do |test_case|
test_case.status_details.flaky = true
end
end
# Set test case status detail to muted
#
# @return [void]
def set_muted
lifecycle.update_test_case do |test_case|
test_case.status_details.muted = true
end
end
# Set test case status detail to known
#
# @return [void]
def set_known
lifecycle.update_test_case do |test_case|
test_case.status_details.known = true
end
end
# Add step with provided name and optional status to current test step, fixture or test case
# @param [String] name
# @param [Symbol] status {Status}, {Status::PASSED} by default
# @return [void]
def step(name:, status: nil)
lifecycle.add_test_step(StepResult.new(name: name, status: status || Status::PASSED, stage: Stage::FINISHED))
lifecycle.stop_test_step
end
# Run passed block as step with given name and return result of yield
# @param [String] name
# @yield [] step block
# @return [Object]
def run_step(name)
lifecycle.start_test_step(StepResult.new(name: name, stage: Stage::RUNNING))
result = yield
lifecycle.update_test_step { |step| step.status = Status::PASSED }
result
rescue StandardError, configuration.failure_exception => e
lifecycle.update_test_step do |step|
step.status = ResultUtils.status(e)
step.status_details = ResultUtils.status_details(e)
end
raise(e)
ensure
lifecycle.stop_test_step
end
# Add parameter to current test step
# @param [String] name
# @param [String] value
# @return [void]
def step_parameter(name, value)
lifecycle.update_test_step do |step|
step.parameters.push(Parameter.new(name, value))
end
end
end
# rubocop:enable Naming/FileName