33module Allure
44 # Main class for creating and writing allure results
55 class AllureLifecycle
6+ def initialize
7+ @step_context = [ ]
8+ end
9+
610 # Start test result container
711 # @param [Allure::TestResultContainer] test_result_container
812 # @return [Allure::TestResultContainer]
@@ -42,6 +46,7 @@ def stop_test_container
4246 # @param [Allure::TestResult] test_result
4347 # @return [Allure::TestResult]
4448 def start_test_case ( test_result )
49+ clear_step_context
4550 unless @current_test_result_container
4651 return logger . error ( "Could not start test case, test container is not started" )
4752 end
@@ -75,6 +80,7 @@ def stop_test_case
7580 @current_test_case . stage = Stage ::FINISHED
7681 file_writer . write_test_result ( @current_test_case )
7782 clear_current_test_case
83+ clear_step_context
7884 end
7985
8086 # Start test step and add to current test case
@@ -85,8 +91,8 @@ def start_test_step(step_result)
8591
8692 step_result . start = ResultUtils . timestamp
8793 step_result . stage = Stage ::RUNNING
88- @current_test_case . steps . push ( step_result )
89- @current_test_step = step_result
94+ add_test_step ( step_result )
95+ step_result
9096 end
9197
9298 # @example Update current test step
@@ -97,19 +103,19 @@ def start_test_step(step_result)
97103 # @yieldreturn [void]
98104 # @return [void]
99105 def update_test_step
100- return logger . error ( "Could not update test step, no step is running" ) unless @ current_test_step
106+ return logger . error ( "Could not update test step, no step is running" ) unless current_test_step
101107
102- yield ( @ current_test_step)
108+ yield ( current_test_step )
103109 end
104110
105111 # Stop current test step
106112 # @return [void]
107113 def stop_test_step
108- return logger . error ( "Could not stop test step, no step is running" ) unless @ current_test_step
114+ return logger . error ( "Could not stop test step, no step is running" ) unless current_test_step
109115
110- @ current_test_step. stop = ResultUtils . timestamp
111- @ current_test_step. stage = Stage ::FINISHED
112- clear_current_test_step
116+ current_test_step . stop = ResultUtils . timestamp
117+ current_test_step . stage = Stage ::FINISHED
118+ clear_last_test_step
113119 end
114120
115121 # Start prepare fixture
@@ -134,6 +140,7 @@ def start_tear_down_fixture(fixture_result)
134140 # @param [Allure::FixtureResult] fixture_result
135141 # @return [Allure::FixtureResult]
136142 def start_fixture ( fixture_result )
143+ clear_step_context
137144 unless @current_test_result_container
138145 logger . error ( "Could not start fixture, test container is not started" )
139146 return false
@@ -162,6 +169,7 @@ def stop_fixture
162169 @current_fixture . stop = ResultUtils . timestamp
163170 @current_fixture . stage = Stage ::FINISHED
164171 clear_current_fixture
172+ clear_step_context
165173 end
166174
167175 # Add attachment to current test or step
@@ -199,6 +207,15 @@ def write_attachment(source, attachment)
199207 file_writer . write_attachment ( source , attachment )
200208 end
201209
210+ # Add step to current fixture|step|test case
211+ # @param [Allure::StepResult] step_result
212+ # @return [Allure::StepResult]
213+ def add_test_step ( step_result )
214+ current_executable . steps . push ( step_result )
215+ @step_context . push ( step_result )
216+ step_result
217+ end
218+
202219 private
203220
204221 def logger
@@ -210,7 +227,7 @@ def file_writer
210227 end
211228
212229 def current_executable
213- @ current_test_step || @current_fixture || @current_test_case
230+ current_test_step || @current_fixture || @current_test_case
214231 end
215232
216233 def clear_current_test_container
@@ -221,8 +238,16 @@ def clear_current_test_case
221238 @current_test_case = nil
222239 end
223240
224- def clear_current_test_step
225- @current_test_step = nil
241+ def current_test_step
242+ @step_context . last
243+ end
244+
245+ def clear_last_test_step
246+ @step_context . pop
247+ end
248+
249+ def clear_step_context
250+ @step_context . clear
226251 end
227252
228253 def clear_current_fixture
0 commit comments