File: runner.context_cleanup.feature

package info (click to toggle)
behave 1.2.6-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,160 kB
  • sloc: python: 19,857; makefile: 137; sh: 18
file content (372 lines) | stat: -rw-r--r-- 11,828 bytes parent folder | download | duplicates (3)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
Feature: Perform Context.cleanups at the end of a test-run, feature or scenario (scope guards)

  As a test writer
  I want to perform cleanups (tear-downs) of functionality at the end of a test-run, feature or scenario
  So that I can easily implement the "scope guard" design pattern (Python: contextmanager)

  . SPECIFICATION:
  .   * Context.add_cleanup(func) is provided to register cleanup functions
  .   * Cleanup functions are registered in the current layer of the context object stack.
  .   * Cleanup functions are executed before the current layer of the context object is removed/popped.
  .   * Cleanup functions are executed in reverse order of registration
  .     (LIFO: first registered cleanup function will be executed last, etc.)
  .   * Cleanup functions are executed in best-effort mode: If one fails, the others are still executed
  .   * Behaviour when the execution of cleanup function fails should be adjustable
  .
  .   | Scope/Layer | Cleanup Registration Point  | Cleanup Execution Point |
  .   | test-run    | In before_all() hook        | After after_all() hook is executed.      |
  .   | feature     | In before_feature() hook    | After after_feature() hook is executed.  |
  .   | feature     | In before_tag() in Feature  | After after_feature() hook is executed.  |
  .   | scenario    | In before_scenario() hook   | After after_scenario() hook is executed. |
  .   | scenario    | In before_tag() in Scenario | After after_scenario() hook is executed. |
  .   | scenario    | In step implementation      | After after_scenario() hook is executed. |
  .   | scenario    | In step hooks               | After after_scenario() hook is executed. |


  @setup
  Scenario: Test Setup
    Given a new working directory
    And a file named "features/environment.py" with:
      """
      from __future__ import print_function

      # -- CLEANUP FUNCTIONS:
      class CleanupFuntion(object):
          def __init__(self, name=None):
              self.name = name or ""

          def __call__(self):
              print("CALLED: CleanupFunction:%s" % self.name)

      def cleanup_after_testrun():
          print("CALLED: cleanup_after_testrun")

      def cleanup_foo():
          print("CALLED: cleanup_foo")

      def cleanup_bar():
          print("CALLED: cleanup_bar")

      # -- HOOKS:
      def before_all(context):
          print("CALLED-HOOK: before_all")
          userdata = context.config.userdata
          use_cleanup = userdata.getbool("use_cleanup_after_testrun")
          if use_cleanup:
              print("REGISTER-CLEANUP: cleanup_after_testrun")
              context.add_cleanup(cleanup_after_testrun)

      def before_feature(context, feature):
          print("CALLED-HOOK: before_feature:%s" % feature.name)
          userdata = context.config.userdata
          use_cleanup = userdata.getbool("use_cleanup_after_feature")
          if use_cleanup and "cleanup.after_feature" in feature.tags:
              print("REGISTER-CLEANUP: cleanup_foo")
              context.add_cleanup(cleanup_foo)

      def after_feature(context, feature):
          print("CALLED-HOOK: after_feature: %s" % feature.name)

      def after_all(context):
          print("CALLED-HOOK: after_all")
      """
    And a file named "features/steps/use_steps.py" with:
      """
      import behave4cmd0.passing_steps
      """
    And a file named "features/alice.feature" with:
      """
      Feature: Alice
        Scenario: A1
          Given a step passes

        Scenario: A2
          When another step passes
      """
    And a file named "behave.ini" with:
      """
      [behave]
      show_timings = false
      stdout_capture = true
      """

  @cleanup.after_testrun
  Scenario: Cleanup registered in before_all hook
    When I run "behave -D use_cleanup_after_testrun -f plain features/alice.feature"
    Then it should pass with:
      """
      CALLED-HOOK: before_all
      REGISTER-CLEANUP: cleanup_after_testrun
      CALLED-HOOK: before_feature:Alice
      Feature: Alice
      """
    And the command output should contain:
      """
      Scenario: A2
        When another step passes ... passed

      CALLED-HOOK: after_feature: Alice
      CALLED-HOOK: after_all
      CALLED: cleanup_after_testrun
      """

  @cleanup.after_feature
  Scenario: Cleanup registered in before_feature hook
    Given a file named "features/environment.py" with:
      """
      from __future__ import print_function

      # -- CLEANUP FUNCTIONS:
      def cleanup_foo():
          print("CALLED: cleanup_foo")

      # -- HOOKS:
      def before_feature(context, feature):
          print("CALLED-HOOK: before_feature:%s" % feature.name)
          if "cleanup.after_feature" in feature.tags:
              print("REGISTER-CLEANUP: cleanup_foo")
              context.add_cleanup(cleanup_foo)

      def after_feature(context, feature):
          print("CALLED-HOOK: after_feature:%s" % feature.name)

      def after_all(context):
          print("CALLED-HOOK: after_all")
      """
    And a file named "features/bob.feature" with:
      """
      @cleanup.after_feature
      Feature: Bob
        Scenario: B1
          Given a step passes
      """
    When I run "behave -f plain features/bob.feature"
    Then it should pass with:
      """
      CALLED-HOOK: before_feature:Bob
      REGISTER-CLEANUP: cleanup_foo
      Feature: Bob
      """
    And the command output should contain:
      """
      Scenario: B1
        Given a step passes ... passed

      CALLED-HOOK: after_feature:Bob
      CALLED: cleanup_foo
      CALLED-HOOK: after_all
      """


  @cleanup.after_scenario
  Scenario: Cleanup registered in before_scenario hook
    Given a file named "features/environment.py" with:
      """
      from __future__ import print_function

      # -- CLEANUP FUNCTIONS:
      def cleanup_foo():
          print("CALLED: cleanup_foo")

      # -- HOOKS:
      def before_scenario(context, scenario):
          print("CALLED-HOOK: before_scenario:%s" % scenario.name)
          if "cleanup_foo" in scenario.tags:
              print("REGISTER-CLEANUP: cleanup_foo")
              context.add_cleanup(cleanup_foo)

      def after_scenario(context, scenario):
          print("CALLED-HOOK: after_scenario:%s" % scenario.name)
      """
    And a file named "features/charly.feature" with:
      """
      Feature: Charly
        @cleanup_foo
        Scenario: C1
          Given a step passes

        Scenario: C2
          When a step passes
      """
    When I run "behave -f plain features/charly.feature"
    Then it should pass with:
      """
      CALLED-HOOK: before_scenario:C1
      REGISTER-CLEANUP: cleanup_foo
      Scenario: C1
      """
    And the command output should contain:
      """
      Scenario: C1
        Given a step passes ... passed

      CALLED-HOOK: after_scenario:C1
      CALLED: cleanup_foo
      CALLED-HOOK: before_scenario:C2
      """

  @cleanup.after_scenario
  Scenario: Cleanups are executed in reverse registration order

    Given a file named "features/environment.py" with:
      """
      from __future__ import print_function

      # -- CLEANUP FUNCTIONS:
      def cleanup_foo():
          print("CALLED: cleanup_foo")

      def cleanup_bar():
          print("CALLED: cleanup_bar")

      # -- HOOKS:
      def before_scenario(context, scenario):
          print("CALLED-HOOK: before_scenario:%s" % scenario.name)
          if "cleanup_foo" in scenario.tags:
              print("REGISTER-CLEANUP: cleanup_foo")
              context.add_cleanup(cleanup_foo)
          if "cleanup_bar" in scenario.tags:
              print("REGISTER-CLEANUP: cleanup_bar")
              context.add_cleanup(cleanup_bar)

      def after_scenario(context, scenario):
          print("CALLED-HOOK: after_scenario:%s" % scenario.name)
      """
    And a file named "features/dodo.feature" with:
      """
      Feature: Dodo
        @cleanup_foo
        @cleanup_bar
        Scenario: D1
          Given a step passes

        Scenario: D2
          When a step passes
      """
    When I run "behave -f plain features/dodo.feature"
    Then it should pass with:
      """
      CALLED-HOOK: before_scenario:D1
      REGISTER-CLEANUP: cleanup_foo
      REGISTER-CLEANUP: cleanup_bar
      Scenario: D1
      """
    And the command output should contain:
      """
      Scenario: D1
        Given a step passes ... passed

      CALLED-HOOK: after_scenario:D1
      CALLED: cleanup_bar
      CALLED: cleanup_foo
      CALLED-HOOK: before_scenario:D2
      """
    And the command output should contain 1 times:
      """
      CALLED: cleanup_bar
      CALLED: cleanup_foo
      """

  @cleanup.after_scenario
  Scenario: Cleanup registered in step implementation
    Given a file named "features/environment.py" with:
      """
      from __future__ import print_function

      # -- HOOKS:
      def before_scenario(context, scenario):
          print("CALLED-HOOK: before_scenario:%s" % scenario.name)

      def after_scenario(context, scenario):
          print("CALLED-HOOK: after_scenario:%s" % scenario.name)
      """
    And a file named "features/steps/cleanup_steps.py" with:
      """
      from behave import given

      # -- CLEANUP FUNCTIONS:
      def cleanup_foo():
          print("CALLED: cleanup_foo")

      # -- STEPS:
      @given(u'I register a cleanup "{cleanup_name}"')
      def step_register_cleanup(context, cleanup_name):
          if cleanup_name == "cleanup_foo":
              context.add_cleanup(cleanup_foo)
          else:
              raise KeyError("Unknown_cleanup:%s" % cleanup_name)
      """
    And a file named "features/emily.feature" with:
      """
      Feature: Emily
        Scenario: E1
          Given I register a cleanup "cleanup_foo"

        Scenario: E2
          When a step passes
      """
    When I run "behave -f plain features/emily.feature"
    Then it should pass with:
      """
      Scenario: E1
          Given I register a cleanup "cleanup_foo" ... passed

      CALLED-HOOK: after_scenario:E1
      CALLED: cleanup_foo
      CALLED-HOOK: before_scenario:E2
      """
    And the command output should contain 1 times:
      """
      CALLED: cleanup_foo
      """

  @cleanup.after_scenario
  Scenario: Registered cleanup function args are passed to cleanup
    Given a file named "features/environment.py" with:
      """
      from __future__ import print_function

      # -- CLEANUP FUNCTIONS:
      def cleanup_foo(text):
          print('CALLED: cleanup_foo("%s")' % text)

      # -- HOOKS:
      def before_scenario(context, scenario):
          print("CALLED-HOOK: before_scenario:%s" % scenario.name)
          if "cleanup_foo" in scenario.tags:
              print('REGISTER-CLEANUP: cleanup_foo("Alice")')
              context.add_cleanup(cleanup_foo, "Alice")
              print('REGISTER-CLEANUP: cleanup_foo("Bob")')
              context.add_cleanup(cleanup_foo, "Bob")

      def after_scenario(context, scenario):
          print("CALLED-HOOK: after_scenario:%s" % scenario.name)
      """
    And a file named "features/frank.feature" with:
      """
      Feature: Frank
        @cleanup_foo
        Scenario: F1
          Given a step passes

        Scenario: F2
          When a step passes
      """
    When I run "behave -f plain features/frank.feature"
    Then it should pass with:
      """
      CALLED-HOOK: before_scenario:F1
      REGISTER-CLEANUP: cleanup_foo("Alice")
      REGISTER-CLEANUP: cleanup_foo("Bob")
      Scenario: F1
      """
    And the command output should contain:
      """
      Scenario: F1
        Given a step passes ... passed

      CALLED-HOOK: after_scenario:F1
      CALLED: cleanup_foo("Bob")
      CALLED: cleanup_foo("Alice")
      CALLED-HOOK: before_scenario:F2
      """