Debian Codegraph @

docs/new_and_noteworthy_v1.2.6.rst

raw
1 Noteworthy in Version 1.2.6
2 ==============================================================================
3
4 Summary:
5
6 * Tagged Examples: Examples in a ScenarioOutline can now have tags.
7
8 * Feature model elements have now language attribute based on language tag
9 in feature file (or the default language tag that was used by the parser).
10
11 * Gherkin parser: Supports escaped-pipe in Gherkin table cell value
12
13 * Configuration: Supports now to define default tags in configfile
14
15 * Configuration: language data is now used as default-language that should
16 be used by the Gherkin parser. Language tags in the Feature file override
17 this setting.
18
19 * Runner: Can continue after a failed step in a scenario
20
21 * Runner: Hooks processing handles now exceptions.
22 Hook errors (exception in hook processing) lead now to scenario failures
23 (even if no step fails).
24
25 * Testing support for asynchronuous frameworks or protocols (:mod:`asyncio` based)
26
27 * Context-cleanups: Register cleanup functions that are executed at the end
28 of the test-scope (scenario, feature or test-run) via
29 :func:`~behave.runner.Context.add_cleanup()`.
30
31 * :ref:`docid.fixtures`: Simplify setup/cleanup in scenario, feature or test-run
32
33
34
35 Scenario Outline Improvements
36 -------------------------------------------------------------------------------
37
38 .. _tagged examples:
39
40 .. index::
41 pair: ScenarioOutline; tagged examples
42 pair: Gherkin parser; tagged examples
43
44 Tagged Examples
45 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46
47 :Since: behave 1.2.6.dev0
48
49 The Gherkin parser (and the model) supports now to use tags with the
50 ``Examples`` section in a ``Scenario Outline``. This functionality can be
51 used to provide multiple ``Examples`` sections, for example one section per
52 testing stage (development, integration testing, system testing, ...) or
53 one section per test team.
54
55 The following feature file provides a simple example of this functionality:
56
57 .. code-block:: gherkin
58
59 # -- FILE: features/tagged_examples.feature
60 Feature:
61 Scenario Outline: Wow
62 Given an employee "<name>"
63
64 @develop
65 Examples: Araxas
66 | name | birthyear |
67 | Alice | 1985 |
68 | Bob | 1975 |
69
70 @integration
71 Examples:
72 | name | birthyear |
73 | Charly | 1995 |
74
75 .. note::
76
77 The generated scenarios from a ScenarioOutline inherit the tags from
78 the ScenarioOutline and its Examples section::
79
80 # -- FOR scenario in scenario_outline.scenarios:
81 scenario.tags = scenario_outline.tags + examples.tags
82
83 To run only the first ``Examples`` section, you use:
84
85 .. code-block:: shell
86
87 behave --tags=@develop features/tagged_examples.feature
88
89 .. code-block:: gherkin
90
91 Scenario Outline: Wow -- @1.1 Araxas # features/tagged_examples.feature:7
92 Given an employee "Alice"
93
94 Scenario Outline: Wow -- @1.2 Araxas # features/tagged_examples.feature:8
95 Given an employee "Bob"
96
97
98 Tagged Examples with Active Tags and Userdata
99 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100
101 An even more natural fit is to use ``tagged examples`` together with
102 ``active tags`` and ``userdata``:
103
104 .. code-block:: gherkin
105
106 # -- FILE: features/tagged_examples2.feature
107 # VARIANT 2: With active tags and userdata.
108 Feature:
109 Scenario Outline: Wow
110 Given an employee "<name>"
111
112 @use.with_stage=develop
113 Examples: Araxas
114 | name | birthyear |
115 | Alice | 1985 |
116 | Bob | 1975 |
117
118 @use.with_stage=integration
119 Examples:
120 | name | birthyear |
121 | Charly | 1995 |
122
123 Select the ``Examples`` section now by using:
124
125 .. code-block:: shell
126
127 # -- VARIANT 1: Use userdata
128 behave -D stage=integration features/tagged_examples2.feature
129
130 # -- VARIANT 2: Use stage mechanism
131 behave --stage=integration features/tagged_examples2.feature
132
133
134 .. code-block:: python
135
136 # -- FILE: features/environment.py
137 from behave.tag_matcher import ActiveTagMatcher, setup_active_tag_values
138 import sys
139
140 # -- ACTIVE TAG SUPPORT: @use.with_{category}={value}, ...
141 active_tag_value_provider = {
142 "stage": "develop",
143 }
144 active_tag_matcher = ActiveTagMatcher(active_tag_value_provider)
145
146 # -- BEHAVE HOOKS:
147 def before_all(context):
148 userdata = context.config.userdata
149 stage = context.config.stage or userdata.get("stage", "develop")
150 userdata["stage"] = stage
151 setup_active_tag_values(active_tag_value_provider, userdata)
152
153 def before_scenario(context, scenario):
154 if active_tag_matcher.should_exclude_with(scenario.effective_tags):
155 sys.stdout.write("ACTIVE-TAG DISABLED: Scenario %s\n" % scenario.name)
156 scenario.skip(active_tag_matcher.exclude_reason)
157
158
159
160
161 Gherkin Parser Improvements
162 -------------------------------------------------------------------------------
163
164 Escaped-Pipe Support in Tables
165 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166
167 It is now possible to use the "|" (pipe) symbol in Gherkin tables by escaping it.
168 The pipe symbol is normally used as column separator in tables.
169
170 EXAMPLE:
171
172 .. code-block:: Gherkin
173
174 Scenario: Use escaped-pipe symbol
175 Given I use table data with:
176 | name | value |
177 | alice | one\|two\|three\|four |
178 Then table data for "alice" is "one|two|three|four"
179
180 .. seealso::
181
182 * `issue.features/issue0302.feature`_ for details
183
184 .. _`issue.features/issue0302.feature`: https://github.com/behave/behave/blob/master/issue.features/issue0302.feature
185
186
187 Configuration Improvements
188 -------------------------------------------------------------------------------
189
190 Language Option
191 ~~~~~~~~~~~~~~~
192
193 The interpretation of the ``language-tag`` comment in feature files (Gherkin)
194 and the configuration ``lang`` option on command-line and in the configuration file
195 changed slightly.
196
197 If a ``language-tag`` is used in a feature file,
198 it is now prefered over the command-line/configuration file settings.
199 This is especially useful when your feature files use multiple spoken languages
200 (in different files).
201
202 EXAMPLE:
203
204 .. code-block:: Gherkin
205
206 # -- FILE: features/french_1.feature
207 # language: fr
208 Fonctionnalité: Alice
209 ...
210
211 .. code-block:: ini
212
213 # -- FILE: behave.ini
214 [behave]
215 lang = de # Default (spoken) language to use: German
216 ...
217
218 .. note::
219
220 The feature object contains now a ``language`` attribute that contains
221 the information which language was used during Gherkin parsing.
222
223
224 Default Tags
225 ~~~~~~~~~~~~
226
227 It is now possible to define ``default tags`` in the configuration file.
228 ``Default tags`` are used when you do not specify tags on the command-line.
229
230 EXAMPLE:
231
232 .. code-block:: ini
233
234 # -- FILE: behave.ini
235 # Exclude/skip any feature/scenario with @xfail or @not_implemented tags
236 [behave]
237 default_tags = -@xfail -@not_implemented
238 ...
239
240
241
242 Runner Improvements
243 -------------------------------------------------------------------------------
244
245 Hook Errors cause Failures
246 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247
248 The behaviour of hook errors, meaning uncaught exceptions while processing hooks,
249 is changed in this release. The new behaviour causes the entity (test-run, feature, scenario),
250 for which the hook is executed, to fail.
251 In addition, a hook error in a ``before_all()``, ``before_feature()``,
252 ``before_scenario()``, and ``before_tag()`` hook causes its corresponding entity
253 to be skipped.
254
255 .. seealso::
256
257 * `features/runner.hook_errors.feature`_ for the detailled specification
258
259 .. _`features/runner.hook_errors.feature`: https://github.com/behave/behave/blob/master/features/runner.hook_errors.feature
260
261
262 Option: Continue after Failed Step in a Scenario
263 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
264
265 This behaviour is sometimes desired, when you want to see what happens in the
266 remaining steps of a scenario.
267
268 EXAMPLE:
269
270 .. code-block:: python
271
272 # -- FILE: features/environment.py
273 from behave.model import Scenario
274
275 def before_all(context):
276 userdata = context.config.userdata
277 continue_after_failed = userdata.getbool("runner.continue_after_failed_step", False)
278 Scenario.continue_after_failed_step = continue_after_failed
279
280 .. code-block:: shell
281
282 # -- ENABLE OPTION: Use userdata on command-line
283 behave -D runner.continue_after_failed_step=true features/
284
285 .. note::
286
287 A failing step normally causes correlated failures in most of the following steps.
288 Therefore, this behaviour is normally not desired.
289
290 .. seealso::
291
292 * `features/runner.continue_after_failed_step.feature`_ for the detailled specification
293
294 .. _`features/runner.continue_after_failed_step.feature`: https://github.com/behave/behave/blob/master/features/runner.continue_after_failed_step.feature
295
296
297 Testing asyncio Frameworks
298 -------------------------------------------------------------------------------
299
300 :Since: behave 1.2.6.dev0
301
302 The following support was added to simplify testing asynchronuous
303 framework and protocols that are based on :mod:`asyncio` module
304 (since Python 3.4).
305
306 There are basically two use cases:
307
308 * async-steps (with event_loop.run_until_complete() semantics)
309 * async-dispatch step(s) with async-collect step(s) later on
310
311
312 Async-steps
313 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
314
315 It is now possible to use ``async-steps`` in ``behave``.
316 An async-step is basically a coroutine as step-implementation for behave.
317 The async-step is wrapped into an ``event_loop.run_until_complete()`` call
318 by using the ``@async_run_until_complete`` step-decorator.
319
320 This avoids another layer of indirection that would otherwise be necessary,
321 to use the coroutine.
322
323 A simple example for the implementation of the async-steps is shown for:
324
325 * Python 3.5 with new ``async``/``await`` keywords
326 * Python 3.4 with ``@asyncio.coroutine`` decorator and ``yield from`` keyword
327
328 .. literalinclude:: ../examples/async_step/features/steps/async_steps35.py
329 :language: python
330 :prepend:
331 # -- FILE: features/steps/async_steps35.py
332
333
334 .. literalinclude:: ../examples/async_step/features/steps/async_steps34.py
335 :language: python
336 :prepend:
337 # -- FILE: features/steps/async_steps34.py
338
339 When you use the async-step from above in a feature file and run it with behave:
340
341 .. literalinclude:: ../examples/async_step/testrun_example.async_run.txt
342 :language: gherkin
343 :prepend:
344 # -- TEST-RUN OUTPUT:
345 $ behave -f plain features/async_run.feature
346
347 .. hidden:
348
349 .. literalinclude:: ../examples/async_step/features/async_run.feature
350 :language: gherkin
351 :prepend: # -- FILE: features/async_run.feature
352
353 .. note::
354
355 The async-step is wrapped with an ``event_loop.run_until_complete()`` call.
356 As the timings show, it actually needs approximatly 0.3 seconds to run.
357
358
359
360
361 Async-dispatch and async-collect
362 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
363
364 The other use case with testing async frameworks is that
365
366 * you dispatch one or more async-calls
367 * you collect (and verify) the results of the async-calls later-on
368
369 A simple example of this approach is shown in the following feature file:
370
371 .. literalinclude:: ../examples/async_step/features/async_dispatch.feature
372 :language: gherkin
373 :prepend: # -- FILE: features/async_dispatch.feature
374
375 When you run this feature file:
376
377 .. literalinclude:: ../examples/async_step/testrun_example.async_dispatch.txt
378 :language: gherkin
379 :prepend:
380 # -- TEST-RUN OUTPUT:
381 $ behave -f plain features/async_dispatch.feature
382
383 .. note::
384
385 The final async-collect step needs approx. 0.2 seconds until the two
386 dispatched async-tasks have finished.
387 In contrast, the async-dispatch steps basically need no time at all.
388
389 An :class:`AsyncContext` object is used on the context,
390 to hold the event loop information and the async-tasks that are of interest.
391
392 The implementation of the steps from above:
393
394 .. literalinclude:: ../examples/async_step/features/steps/async_dispatch_steps.py
395 :language: gherkin
396 :prepend:
397 # -- FILE: features/steps/async_dispatch_steps.py
398 # REQUIRES: Python 3.4 or newer
399
400
401 Context-based Cleanups
402 -------------------------------------------------------------------------------
403
404 It is now possible to register cleanup functions with the context object.
405 This functionality is normally used in:
406
407 * hooks (:func:`before_all()`, :func:`before_feature()`, :func:`before_scenario()`, ...)
408 * step implementations
409 * ...
410
411 .. code-block:: python
412
413 # -- SIGNATURE: Context.add_cleanup(cleanup_func, *args, **kwargs)
414 # CLEANUP CALL EXAMPLES:
415 context.add_cleanup(cleanup0) # CALLS LATER: cleanup0()
416 context.add_cleanup(cleanup1, 1, 2) # CALLS LATER: cleanup1(1, 2)
417 context.add_cleanup(cleanup2, name="Alice") # CALLS LATER: cleanup2(name="Alice")
418 context.add_cleanup(cleanup3, 1, 2, name="Bob") # CALLS LATER: cleanup3(1, 2, name="Bob")
419
420 The registered cleanup will be performed when the context layer is removed.
421 This depends on the the context layer when the cleanup function was registered
422 (test-run, feature, scenario).
423
424 Example:
425
426 .. code-block:: python
427
428 # -- FILE: features/environment.py
429 def before_all(context):
430 context.add_cleanup(cleanup_me)
431 # -- ON CLEANUP: Calls cleanup_me()
432 # Called after test-run.
433
434 def before_tag(context, tag):
435 if tag == "foo":
436 context.foo = setup_foo()
437 context.add_cleanup(cleanup_foo, context.foo)
438 # -- ON CLEANUP: Calls cleanup_foo(context.foo)
439 # CASE scenario tag: cleanup_foo() will be called after this scenario.
440 # CASE feature tag: cleanup_foo() will be called after this feature.
441
442 .. seealso::
443
444 For more details, see `features/runner.context_cleanup.feature`_ .
445
446 .. _`features/runner.context_cleanup.feature`: https://github.com/behave/behave/blob/master/features/runner.context_cleanup.feature
447
448
449 Fixtures
450 -------------------------------------------------------------------------------
451
452 Fixtures simplify setup/cleanup tasks that are often needed for testing.
453
454 Providing a Fixture
455 ~~~~~~~~~~~~~~~~~~~
456
457 .. code-block:: python
458
459 # -- FILE: behave4my_project/fixtures.py (or in: features/environment.py)
460 from behave import fixture
461 from somewhere.browser.firefox import FirefoxBrowser
462
463 # -- FIXTURE-VARIANT 1: Use generator-function
464 @fixture
465 def browser_firefox(context, timeout=30, **kwargs):
466 # -- SETUP-FIXTURE PART:
467 context.browser = FirefoxBrowser(timeout, **kwargs)
468 yield context.browser
469 # -- CLEANUP-FIXTURE PART:
470 context.browser.shutdown()
471
472 Using a Fixture
473 ~~~~~~~~~~~~~~~
474
475 .. code-block:: Gherkin
476
477 # -- FILE: features/use_fixture1.feature
478 Feature: Use Fixture on Scenario Level
479
480 @fixture.browser.firefox
481 Scenario: Use Web Browser Firefox
482 Given I load web page "https://somewhere.web"
483 ...
484 # -- AFTER-SCENARIO: Cleanup fixture.browser.firefox
485
486 .. code-block:: python
487
488 # -- FILE: features/environment.py
489 from behave import use_fixture
490 from behave4my_project.fixtures import browser_firefox
491
492 def before_tag(context, tag):
493 if tag == "fixture.browser.firefox":
494 use_fixture(browser_firefox, context, timeout=10)
495
496
497 .. seealso::
498
499 * :ref:`docid.fixtures` description for details
500 * `features/fixture.feature`_
501
502 .. _`features/fixture.feature`: https://github.com/behave/behave/blob/master/features/fixture.feature