| 1 |
.. _tutorial: |
| 2 |
|
| 3 |
======== |
| 4 |
Tutorial |
| 5 |
======== |
| 6 |
|
| 7 |
First, :doc:`install behave <install>`. |
| 8 |
|
| 9 |
Now make a directory called "features". In that directory create a file |
| 10 |
called "tutorial.feature" containing: |
| 11 |
|
| 12 |
.. code-block:: gherkin |
| 13 |
|
| 14 |
Feature: showing off behave |
| 15 |
|
| 16 |
Scenario: run a simple test |
| 17 |
Given we have behave installed |
| 18 |
When we implement a test |
| 19 |
Then behave will test it for us! |
| 20 |
|
| 21 |
Make a new directory called "features/steps". In that directory create a |
| 22 |
file called "tutorial.py" containing: |
| 23 |
|
| 24 |
.. code-block:: python |
| 25 |
|
| 26 |
from behave import * |
| 27 |
|
| 28 |
@given('we have behave installed') |
| 29 |
def step_impl(context): |
| 30 |
pass |
| 31 |
|
| 32 |
@when('we implement a test') |
| 33 |
def step_impl(context): |
| 34 |
assert True is not False |
| 35 |
|
| 36 |
@then('behave will test it for us!') |
| 37 |
def step_impl(context): |
| 38 |
assert context.failed is False |
| 39 |
|
| 40 |
Run behave:: |
| 41 |
|
| 42 |
% behave |
| 43 |
Feature: showing off behave # features/tutorial.feature:1 |
| 44 |
|
| 45 |
Scenario: run a simple test # features/tutorial.feature:3 |
| 46 |
Given we have behave installed # features/steps/tutorial.py:3 |
| 47 |
When we implement a test # features/steps/tutorial.py:7 |
| 48 |
Then behave will test it for us! # features/steps/tutorial.py:11 |
| 49 |
|
| 50 |
1 feature passed, 0 failed, 0 skipped |
| 51 |
1 scenario passed, 0 failed, 0 skipped |
| 52 |
3 steps passed, 0 failed, 0 skipped, 0 undefined |
| 53 |
|
| 54 |
Now, continue reading to learn how to make the most of *behave*. |
| 55 |
|
| 56 |
|
| 57 |
Features |
| 58 |
======== |
| 59 |
|
| 60 |
*behave* operates on directories containing: |
| 61 |
|
| 62 |
1. `feature files`_ written by your Business Analyst / Sponsor / whoever |
| 63 |
with your behaviour scenarios in it, and |
| 64 |
2. a "steps" directory with `Python step implementations`_ for the |
| 65 |
scenarios. |
| 66 |
|
| 67 |
You may optionally include some `environmental controls`_ (code to run |
| 68 |
before and after steps, scenarios, features or the whole shooting |
| 69 |
match). |
| 70 |
|
| 71 |
The minimum requirement for a features directory is:: |
| 72 |
|
| 73 |
features/ |
| 74 |
features/everything.feature |
| 75 |
features/steps/ |
| 76 |
features/steps/steps.py |
| 77 |
|
| 78 |
A more complex directory might look like:: |
| 79 |
|
| 80 |
features/ |
| 81 |
features/signup.feature |
| 82 |
features/login.feature |
| 83 |
features/account_details.feature |
| 84 |
features/environment.py |
| 85 |
features/steps/ |
| 86 |
features/steps/website.py |
| 87 |
features/steps/utils.py |
| 88 |
|
| 89 |
If you're having trouble setting things up and want to see what *behave* is |
| 90 |
doing in attempting to find your features use the "-v" (verbose) |
| 91 |
command-line switch. |
| 92 |
|
| 93 |
|
| 94 |
Feature Files |
| 95 |
============= |
| 96 |
|
| 97 |
A feature file has a :ref:`natural language format <chapter.gherkin>` |
| 98 |
describing a feature or part of a feature with representative examples of |
| 99 |
expected outcomes. |
| 100 |
They're plain-text (encoded in UTF-8) and look something like: |
| 101 |
|
| 102 |
.. code-block:: gherkin |
| 103 |
|
| 104 |
Feature: Fight or flight |
| 105 |
In order to increase the ninja survival rate, |
| 106 |
As a ninja commander |
| 107 |
I want my ninjas to decide whether to take on an |
| 108 |
opponent based on their skill levels |
| 109 |
|
| 110 |
Scenario: Weaker opponent |
| 111 |
Given the ninja has a third level black-belt |
| 112 |
When attacked by a samurai |
| 113 |
Then the ninja should engage the opponent |
| 114 |
|
| 115 |
Scenario: Stronger opponent |
| 116 |
Given the ninja has a third level black-belt |
| 117 |
When attacked by Chuck Norris |
| 118 |
Then the ninja should run for his life |
| 119 |
|
| 120 |
The "Given", "When" and "Then" parts of this prose form the actual steps |
| 121 |
that will be taken by *behave* in testing your system. These map to `Python |
| 122 |
step implementations`_. As a general guide: |
| 123 |
|
| 124 |
**Given** we *put the system in a known state* before the |
| 125 |
user (or external system) starts interacting with the system (in the When |
| 126 |
steps). Avoid talking about user interaction in givens. |
| 127 |
|
| 128 |
**When** we *take key actions* the user (or external system) performs. This |
| 129 |
is the interaction with your system which should (or perhaps should not) |
| 130 |
cause some state to change. |
| 131 |
|
| 132 |
**Then** we *observe outcomes*. |
| 133 |
|
| 134 |
You may also include "And" or "But" as a step - these are renamed by *behave* |
| 135 |
to take the name of their preceding step, so: |
| 136 |
|
| 137 |
.. code-block:: gherkin |
| 138 |
|
| 139 |
Scenario: Stronger opponent |
| 140 |
Given the ninja has a third level black-belt |
| 141 |
When attacked by Chuck Norris |
| 142 |
Then the ninja should run for his life |
| 143 |
And fall off a cliff |
| 144 |
|
| 145 |
In this case *behave* will look for a step definition for |
| 146 |
``"Then fall off a cliff"``. |
| 147 |
|
| 148 |
|
| 149 |
Scenario Outlines |
| 150 |
----------------- |
| 151 |
|
| 152 |
Sometimes a scenario should be run with a number of variables giving a set |
| 153 |
of known states, actions to take and expected outcomes, all using the same |
| 154 |
basic actions. You may use a Scenario Outline to achieve this: |
| 155 |
|
| 156 |
.. code-block:: gherkin |
| 157 |
|
| 158 |
Scenario Outline: Blenders |
| 159 |
Given I put <thing> in a blender, |
| 160 |
when I switch the blender on |
| 161 |
then it should transform into <other thing> |
| 162 |
|
| 163 |
Examples: Amphibians |
| 164 |
| thing | other thing | |
| 165 |
| Red Tree Frog | mush | |
| 166 |
|
| 167 |
Examples: Consumer Electronics |
| 168 |
| thing | other thing | |
| 169 |
| iPhone | toxic waste | |
| 170 |
| Galaxy Nexus | toxic waste | |
| 171 |
|
| 172 |
*behave* will run the scenario once for each (non-heading) line appearing |
| 173 |
in the example data tables. |
| 174 |
|
| 175 |
|
| 176 |
Step Data |
| 177 |
--------- |
| 178 |
|
| 179 |
Sometimes it's useful to associate a table of data with your step. |
| 180 |
|
| 181 |
Any text block following a step wrapped in ``"""`` lines will be associated |
| 182 |
with the step. For example: |
| 183 |
|
| 184 |
.. code-block:: gherkin |
| 185 |
|
| 186 |
Scenario: some scenario |
| 187 |
Given a sample text loaded into the frobulator |
| 188 |
""" |
| 189 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do |
| 190 |
eiusmod tempor incididunt ut labore et dolore magna aliqua. |
| 191 |
""" |
| 192 |
When we activate the frobulator |
| 193 |
Then we will find it similar to English |
| 194 |
|
| 195 |
The text is available to the Python step code as the ".text" attribute |
| 196 |
in the :class:`~behave.runner.Context` variable passed into each step |
| 197 |
function. |
| 198 |
|
| 199 |
You may also associate a table of data with a step by simply entering it, |
| 200 |
indented, following the step. This can be useful for loading specific |
| 201 |
required data into a model. |
| 202 |
|
| 203 |
.. code-block:: gherkin |
| 204 |
|
| 205 |
Scenario: some scenario |
| 206 |
Given a set of specific users |
| 207 |
| name | department | |
| 208 |
| Barry | Beer Cans | |
| 209 |
| Pudey | Silly Walks | |
| 210 |
| Two-Lumps | Silly Walks | |
| 211 |
|
| 212 |
When we count the number of people in each department |
| 213 |
Then we will find two people in "Silly Walks" |
| 214 |
But we will find one person in "Beer Cans" |
| 215 |
|
| 216 |
The table is available to the Python step code as the ".table" attribute |
| 217 |
in the :class:`~behave.runner.Context` variable passed into each step |
| 218 |
function. The table for the example above could be accessed like so: |
| 219 |
|
| 220 |
.. code-block:: python |
| 221 |
|
| 222 |
@given('a set of specific users') |
| 223 |
def step_impl(context): |
| 224 |
for row in context.table: |
| 225 |
model.add_user(name=row['name'], department=row['department']) |
| 226 |
|
| 227 |
There's a variety of ways to access the table data - see the |
| 228 |
:class:`~behave.model.Table` API documentation for the full details. |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
Python Step Implementations |
| 233 |
=========================== |
| 234 |
|
| 235 |
Steps used in the scenarios are implemented in Python files in the "steps" |
| 236 |
directory. You can call these whatever you like as long as they use |
| 237 |
the python ``*.py`` file extension. You don't need to tell *behave* which |
| 238 |
ones to use - it'll use all of them. |
| 239 |
|
| 240 |
The full detail of the Python side of *behave* is in the |
| 241 |
:doc:`API documentation <api>`. |
| 242 |
|
| 243 |
Steps are identified using decorators which match the predicate from the |
| 244 |
feature file: **given**, **when**, **then** and **step** (variants with Title case are also |
| 245 |
available if that's your preference.) The decorator accepts a string |
| 246 |
containing the rest of the phrase used in the scenario step it belongs to. |
| 247 |
|
| 248 |
Given a Scenario: |
| 249 |
|
| 250 |
.. code-block:: gherkin |
| 251 |
|
| 252 |
Scenario: Search for an account |
| 253 |
Given I search for a valid account |
| 254 |
Then I will see the account details |
| 255 |
|
| 256 |
Step code implementing the two steps here might look like |
| 257 |
(using selenium webdriver and some other helpers): |
| 258 |
|
| 259 |
.. code-block:: python |
| 260 |
|
| 261 |
@given('I search for a valid account') |
| 262 |
def step_impl(context): |
| 263 |
context.browser.get('http://localhost:8000/index') |
| 264 |
form = get_element(context.browser, tag='form') |
| 265 |
get_element(form, name="msisdn").send_keys('61415551234') |
| 266 |
form.submit() |
| 267 |
|
| 268 |
@then('I will see the account details') |
| 269 |
def step_impl(context): |
| 270 |
elements = find_elements(context.browser, id='no-account') |
| 271 |
eq_(elements, [], 'account not found') |
| 272 |
h = get_element(context.browser, id='account-head') |
| 273 |
ok_(h.text.startswith("Account 61415551234"), |
| 274 |
'Heading %r has wrong text' % h.text) |
| 275 |
|
| 276 |
The ``step`` decorator matches the step to *any* step type, "given", "when" |
| 277 |
or "then". The "and" and "but" step types are renamed internally to take |
| 278 |
the preceding step's keyword (so an "and" following a "given" will become a |
| 279 |
"given" internally and use a **given** decorated step). |
| 280 |
|
| 281 |
.. note:: |
| 282 |
|
| 283 |
Step function names do not need to have a unique symbol name, because the |
| 284 |
text matching selects the step function from the step registry before it is |
| 285 |
called as anonymous function. Hence, when *behave* prints out the missing |
| 286 |
step implementations in a test run, it uses "step_impl" for all functions |
| 287 |
by default. |
| 288 |
|
| 289 |
If you find you'd like your step implementation to invoke another step you |
| 290 |
may do so with the :class:`~behave.runner.Context` method |
| 291 |
:func:`~behave.runner.Context.execute_steps`. |
| 292 |
|
| 293 |
This function allows you to, for example: |
| 294 |
|
| 295 |
.. code-block:: python |
| 296 |
|
| 297 |
@when('I do the same thing as before') |
| 298 |
def step_impl(context): |
| 299 |
context.execute_steps(''' |
| 300 |
when I press the big red button |
| 301 |
and I duck |
| 302 |
''') |
| 303 |
|
| 304 |
This will cause the "when I do the same thing as before" step to execute |
| 305 |
the other two steps as though they had also appeared in the scenario file. |
| 306 |
|
| 307 |
|
| 308 |
Step Parameters |
| 309 |
--------------- |
| 310 |
|
| 311 |
You may find that your feature steps sometimes include very common phrases |
| 312 |
with only some variation. For example: |
| 313 |
|
| 314 |
.. code-block:: gherkin |
| 315 |
|
| 316 |
Scenario: look up a book |
| 317 |
Given I search for a valid book |
| 318 |
Then the result page will include "success" |
| 319 |
|
| 320 |
Scenario: look up an invalid book |
| 321 |
Given I search for a invalid book |
| 322 |
Then the result page will include "failure" |
| 323 |
|
| 324 |
You may define a single Python step that handles both of those Then |
| 325 |
clauses (with a Given step that puts some text into |
| 326 |
``context.response``): |
| 327 |
|
| 328 |
.. code-block:: python |
| 329 |
|
| 330 |
@then('the result page will include "{text}"') |
| 331 |
def step_impl(context, text): |
| 332 |
if text not in context.response: |
| 333 |
fail('%r not in %r' % (text, context.response)) |
| 334 |
|
| 335 |
There are several parsers available in *behave* (by default): |
| 336 |
|
| 337 |
**parse** (the default, based on: :pypi:`parse`) |
| 338 |
Provides a simple parser that replaces regular expressions for step parameters |
| 339 |
with a readable syntax like ``{param:Type}``. |
| 340 |
The syntax is inspired by the Python builtin ``string.format()`` function. |
| 341 |
Step parameters must use the named fields syntax of :pypi:`parse` |
| 342 |
in step definitions. The named fields are extracted, |
| 343 |
optionally type converted and then used as step function arguments. |
| 344 |
|
| 345 |
Supports type conversions by using type converters |
| 346 |
(see :func:`~behave.register_type()`). |
| 347 |
|
| 348 |
**cfparse** (extends: :pypi:`parse`, requires: :pypi:`parse_type`) |
| 349 |
Provides an extended parser with "Cardinality Field" (CF) support. |
| 350 |
Automatically creates missing type converters for related cardinality |
| 351 |
as long as a type converter for cardinality=1 is provided. |
| 352 |
Supports parse expressions like: |
| 353 |
|
| 354 |
* ``{values:Type+}`` (cardinality=1..N, many) |
| 355 |
* ``{values:Type*}`` (cardinality=0..N, many0) |
| 356 |
* ``{value:Type?}`` (cardinality=0..1, optional). |
| 357 |
|
| 358 |
Supports type conversions (as above). |
| 359 |
|
| 360 |
**re** |
| 361 |
This uses full regular expressions to parse the clause text. You will |
| 362 |
need to use named groups "(?P<name>...)" to define the variables pulled |
| 363 |
from the text and passed to your ``step()`` function. |
| 364 |
|
| 365 |
Type conversion is **not supported**. |
| 366 |
A step function writer may implement type conversion |
| 367 |
inside the step function (implementation). |
| 368 |
|
| 369 |
To specify which parser to use invoke :func:`~behave.use_step_matcher` |
| 370 |
with the name of the matcher to use. You may change matcher to suit |
| 371 |
specific step functions - the last call to ``use_step_matcher`` before a step |
| 372 |
function declaration will be the one it uses. |
| 373 |
|
| 374 |
.. note:: |
| 375 |
|
| 376 |
The function :func:`~behave.matchers.step_matcher()` is becoming deprecated. |
| 377 |
Use :func:`~behave.use_step_matcher()` instead. |
| 378 |
|
| 379 |
|
| 380 |
Context |
| 381 |
------- |
| 382 |
|
| 383 |
You'll have noticed the "context" variable that's passed around. It's a |
| 384 |
clever place where you and *behave* can store information to share around. |
| 385 |
It runs at three levels, automatically managed by *behave*. |
| 386 |
|
| 387 |
When *behave* launches into a new feature or scenario it adds a new layer |
| 388 |
to the context, allowing the new activity level to add new values, or |
| 389 |
overwrite ones previously defined, for the duration of that activity. These |
| 390 |
can be thought of as scopes. |
| 391 |
|
| 392 |
You can define values in your `environmental controls`_ file which may be |
| 393 |
set at the feature level and then overridden for some scenarios. Changes |
| 394 |
made at the scenario level won't permanently affect the value set at the |
| 395 |
feature level. |
| 396 |
|
| 397 |
You may also use it to share values between steps. For example, in some |
| 398 |
steps you define you might have: |
| 399 |
|
| 400 |
.. code-block:: python |
| 401 |
|
| 402 |
@given('I request a new widget for an account via SOAP') |
| 403 |
def step_impl(context): |
| 404 |
client = Client("http://127.0.0.1:8000/soap/") |
| 405 |
context.response = client.Allocate(customer_first='Firstname', |
| 406 |
customer_last='Lastname', colour='red') |
| 407 |
|
| 408 |
@then('I should receive an OK SOAP response') |
| 409 |
def step_impl(context): |
| 410 |
eq_(context.response['ok'], 1) |
| 411 |
|
| 412 |
There's also some values added to the context by *behave* itself: |
| 413 |
|
| 414 |
**table** |
| 415 |
This holds any table data associated with a step. |
| 416 |
|
| 417 |
**text** |
| 418 |
This holds any multi-line text associated with a step. |
| 419 |
|
| 420 |
**failed** |
| 421 |
This is set at the root of the context when any step fails. It is |
| 422 |
sometimes useful to use this combined with the ``--stop`` command-line |
| 423 |
option to prevent some mis-behaving resource from being cleaned up in an |
| 424 |
``after_feature()`` or similar (for example, a web browser being driven |
| 425 |
by Selenium.) |
| 426 |
|
| 427 |
The *context* variable in all cases is an instance of |
| 428 |
:class:`behave.runner.Context`. |
| 429 |
|
| 430 |
|
| 431 |
Environmental Controls |
| 432 |
====================== |
| 433 |
|
| 434 |
The environment.py module may define code to run before and after certain |
| 435 |
events during your testing: |
| 436 |
|
| 437 |
**before_step(context, step), after_step(context, step)** |
| 438 |
These run before and after every step. |
| 439 |
**before_scenario(context, scenario), after_scenario(context, scenario)** |
| 440 |
These run before and after each scenario is run. |
| 441 |
**before_feature(context, feature), after_feature(context, feature)** |
| 442 |
These run before and after each feature file is exercised. |
| 443 |
**before_tag(context, tag), after_tag(context, tag)** |
| 444 |
These run before and after a section tagged with the given name. They are |
| 445 |
invoked for each tag encountered in the order they're found in the |
| 446 |
feature file. See `controlling things with tags`_. |
| 447 |
**before_all(context), after_all(context)** |
| 448 |
These run before and after the whole shooting match. |
| 449 |
|
| 450 |
The feature, scenario and step objects represent the information parsed |
| 451 |
from the feature file. They have a number of attributes: |
| 452 |
|
| 453 |
**keyword** |
| 454 |
"Feature", "Scenario", "Given", etc. |
| 455 |
**name** |
| 456 |
The name of the step (the text after the keyword.) |
| 457 |
**tags** |
| 458 |
A list of the tags attached to the section or step. |
| 459 |
See `controlling things with tags`_. |
| 460 |
**filename** and **line** |
| 461 |
The file name (or "<string>") and line number of the statement. |
| 462 |
|
| 463 |
A common use-case for environmental controls might be to set up a web |
| 464 |
server and browser to run all your tests in. For example: |
| 465 |
|
| 466 |
.. code-block:: python |
| 467 |
|
| 468 |
# -- FILE: features/environment.py |
| 469 |
from behave import fixture, use_fixture |
| 470 |
from behave4my_project.fixtures import wsgi_server |
| 471 |
from selenium import webdriver |
| 472 |
|
| 473 |
@fixture |
| 474 |
def selenium_browser_chrome(context): |
| 475 |
# -- HINT: @behave.fixture is similar to @contextlib.contextmanager |
| 476 |
context.browser = webdriver.Chrome() |
| 477 |
yield context.browser |
| 478 |
# -- CLEANUP-FIXTURE PART: |
| 479 |
context.browser.quit() |
| 480 |
|
| 481 |
def before_all(context): |
| 482 |
use_fixture(wsgi_server, context, port=8000) |
| 483 |
use_fixture(selenium_browser_chrome, context) |
| 484 |
# -- HINT: CLEANUP-FIXTURE is performed after after_all() hook is called. |
| 485 |
|
| 486 |
def before_feature(context, feature): |
| 487 |
model.init(environment='test') |
| 488 |
|
| 489 |
|
| 490 |
.. code-block:: python |
| 491 |
|
| 492 |
# -- FILE: behave4my_project/fixtures.py |
| 493 |
# ALTERNATIVE: Place fixture in "features/environment.py" (but reuse is harder) |
| 494 |
from behave import fixture |
| 495 |
import threading |
| 496 |
from wsgiref import simple_server |
| 497 |
from my_application import model |
| 498 |
from my_application import web_app |
| 499 |
|
| 500 |
@fixture |
| 501 |
def wsgi_server(context, port=8000): |
| 502 |
context.server = simple_server.WSGIServer(('', port)) |
| 503 |
context.server.set_app(web_app.main(environment='test')) |
| 504 |
context.thread = threading.Thread(target=context.server.serve_forever) |
| 505 |
context.thread.start() |
| 506 |
yield context.server |
| 507 |
# -- CLEANUP-FIXTURE PART: |
| 508 |
context.server.shutdown() |
| 509 |
context.thread.join() |
| 510 |
|
| 511 |
|
| 512 |
Of course, if you wish, you could have a new browser for each feature, or to |
| 513 |
retain the database state between features or even initialise the database |
| 514 |
for each scenario. |
| 515 |
|
| 516 |
|
| 517 |
.. _`controlling things with tags`: |
| 518 |
|
| 519 |
Controlling Things With Tags |
| 520 |
============================ |
| 521 |
|
| 522 |
You may also "tag" parts of your feature file. At the simplest level this |
| 523 |
allows *behave* to selectively check parts of your feature set. |
| 524 |
|
| 525 |
Given a feature file with: |
| 526 |
|
| 527 |
.. code-block:: gherkin |
| 528 |
|
| 529 |
Feature: Fight or flight |
| 530 |
In order to increase the ninja survival rate, |
| 531 |
As a ninja commander |
| 532 |
I want my ninjas to decide whether to take on an |
| 533 |
opponent based on their skill levels |
| 534 |
|
| 535 |
@slow |
| 536 |
Scenario: Weaker opponent |
| 537 |
Given the ninja has a third level black-belt |
| 538 |
When attacked by a samurai |
| 539 |
Then the ninja should engage the opponent |
| 540 |
|
| 541 |
Scenario: Stronger opponent |
| 542 |
Given the ninja has a third level black-belt |
| 543 |
When attacked by Chuck Norris |
| 544 |
Then the ninja should run for his life |
| 545 |
|
| 546 |
then running ``behave --tags=slow`` will run just the scenarios tagged |
| 547 |
``@slow``. If you wish to check everything *except* the slow ones then you |
| 548 |
may run ``behave --tags=-slow``. |
| 549 |
|
| 550 |
Another common use-case is to tag a scenario you're working on with |
| 551 |
``@wip`` and then ``behave --tags=wip`` to just test that one case. |
| 552 |
|
| 553 |
Tag selection on the command-line may be combined: |
| 554 |
|
| 555 |
* ``--tags=wip,slow`` |
| 556 |
This will select all the cases tagged *either* "wip" or "slow". |
| 557 |
|
| 558 |
* ``--tags=wip --tags=slow`` |
| 559 |
This will select all the cases tagged *both* "wip" and "slow". |
| 560 |
|
| 561 |
If a feature or scenario is tagged and then skipped because of a |
| 562 |
command-line control then the *before_* and *after_* environment functions |
| 563 |
will not be called for that feature or scenario. Note that *behave* has |
| 564 |
additional support specifically for testing `works in progress`_. |
| 565 |
|
| 566 |
The tags attached to a feature and scenario are available in |
| 567 |
the environment functions via the "feature" or "scenario" object passed to |
| 568 |
them. On those objects there is an attribute called "tags" which is a list |
| 569 |
of the tag names attached, in the order they're found in the features file. |
| 570 |
|
| 571 |
There are also `environmental controls`_ specific to tags, so in the above |
| 572 |
example *behave* will attempt to invoke an ``environment.py`` function |
| 573 |
``before_tag`` and ``after_tag`` before and after the Scenario tagged |
| 574 |
``@slow``, passing in the name "slow". If multiple tags are present then |
| 575 |
the functions will be called multiple times with each tag in the order |
| 576 |
they're defined in the feature file. |
| 577 |
|
| 578 |
Re-visiting the example from above; if only some of the features required a |
| 579 |
browser and web server then you could tag them ``@browser``: |
| 580 |
|
| 581 |
.. code-block:: python |
| 582 |
|
| 583 |
# -- FILE: features/environment.py |
| 584 |
# HINT: Reusing some code parts from above. |
| 585 |
... |
| 586 |
|
| 587 |
def before_feature(context, feature): |
| 588 |
model.init(environment='test') |
| 589 |
if 'browser' in feature.tags: |
| 590 |
use_fixture(wsgi_server, context) |
| 591 |
use_fixture(selenium_browser_chrome, context) |
| 592 |
|
| 593 |
|
| 594 |
Works In Progress |
| 595 |
================= |
| 596 |
|
| 597 |
*behave* supports the concept of a highly-unstable "work in progress" |
| 598 |
scenario that you're actively developing. This scenario may produce strange |
| 599 |
logging, or odd output to stdout or just plain interact in unexpected ways |
| 600 |
with *behave*'s scenario runner. |
| 601 |
|
| 602 |
To make testing such scenarios simpler we've implemented a "-w" |
| 603 |
command-line flag. This flag: |
| 604 |
|
| 605 |
1. turns off stdout capture |
| 606 |
2. turns off logging capture; you will still need to configure your own |
| 607 |
logging handlers - we recommend a ``before_all()`` with: |
| 608 |
|
| 609 |
.. code-block:: python |
| 610 |
|
| 611 |
if not context.config.log_capture: |
| 612 |
logging.basicConfig(level=logging.DEBUG) |
| 613 |
|
| 614 |
3. turns off pretty output - no ANSI escape sequences to confuse your |
| 615 |
scenario's output |
| 616 |
4. only runs scenarios tagged with "@wip" |
| 617 |
5. stops at the first error |
| 618 |
|
| 619 |
|
| 620 |
Fixtures |
| 621 |
=================================== |
| 622 |
|
| 623 |
Fixtures simplify the setup/cleanup tasks that are often needed during test execution. |
| 624 |
|
| 625 |
.. code-block:: python |
| 626 |
|
| 627 |
# -- FILE: behave4my_project/fixtures.py (or in: features/environment.py) |
| 628 |
from behave import fixture |
| 629 |
from somewhere.browser.firefox import FirefoxBrowser |
| 630 |
|
| 631 |
# -- FIXTURE: Use generator-function |
| 632 |
@fixture |
| 633 |
def browser_firefox(context, timeout=30, **kwargs): |
| 634 |
# -- SETUP-FIXTURE PART: |
| 635 |
context.browser = FirefoxBrowser(timeout, **kwargs) |
| 636 |
yield context.browser |
| 637 |
# -- CLEANUP-FIXTURE PART: |
| 638 |
context.browser.shutdown() |
| 639 |
|
| 640 |
See :ref:`docid.fixtures` for more information. |
| 641 |
|
| 642 |
|
| 643 |
.. index:: |
| 644 |
single: debug-on-error |
| 645 |
|
| 646 |
.. _debug-on-error: |
| 647 |
|
| 648 |
Debug-on-Error (in Case of Step Failures) |
| 649 |
========================================= |
| 650 |
|
| 651 |
A "debug on error/failure" functionality can easily be provided, |
| 652 |
by using the ``after_step()`` hook. |
| 653 |
The debugger is started when a step fails. |
| 654 |
|
| 655 |
It is in general a good idea to enable this functionality only when needed |
| 656 |
(in interactive mode). The functionality is enabled (in this example) |
| 657 |
by using the user-specific configuration data. A user can: |
| 658 |
|
| 659 |
* provide a userdata define on command-line |
| 660 |
* store a value in the "behave.userdata" section of behave's configuration file |
| 661 |
|
| 662 |
.. code-block:: python |
| 663 |
|
| 664 |
# -- FILE: features/environment.py |
| 665 |
# USE: behave -D BEHAVE_DEBUG_ON_ERROR (to enable debug-on-error) |
| 666 |
# USE: behave -D BEHAVE_DEBUG_ON_ERROR=yes (to enable debug-on-error) |
| 667 |
# USE: behave -D BEHAVE_DEBUG_ON_ERROR=no (to disable debug-on-error) |
| 668 |
|
| 669 |
BEHAVE_DEBUG_ON_ERROR = False |
| 670 |
|
| 671 |
def setup_debug_on_error(userdata): |
| 672 |
global BEHAVE_DEBUG_ON_ERROR |
| 673 |
BEHAVE_DEBUG_ON_ERROR = userdata.getbool("BEHAVE_DEBUG_ON_ERROR") |
| 674 |
|
| 675 |
def before_all(context): |
| 676 |
setup_debug_on_error(context.config.userdata) |
| 677 |
|
| 678 |
def after_step(context, step): |
| 679 |
if BEHAVE_DEBUG_ON_ERROR and step.status == "failed": |
| 680 |
# -- ENTER DEBUGGER: Zoom in on failure location. |
| 681 |
# NOTE: Use IPython debugger, same for pdb (basic python debugger). |
| 682 |
import ipdb |
| 683 |
ipdb.post_mortem(step.exc_traceback) |