| 1 |
===================== |
| 2 |
Feature Testing Setup |
| 3 |
===================== |
| 4 |
|
| 5 |
.. if you change any headings in here make sure you haven't broken the |
| 6 |
cross-references in the API documentation or module docstrings! |
| 7 |
|
| 8 |
Feature Testing Layout |
| 9 |
====================== |
| 10 |
|
| 11 |
*behave* works with three types of files: |
| 12 |
|
| 13 |
1. `feature files`_ written by your Business Analyst / Sponsor / whoever |
| 14 |
with your behaviour scenarios in it, and |
| 15 |
2. a "steps" directory with `Python step implementations`_ for the |
| 16 |
scenarios. |
| 17 |
3. optionally some `environmental controls`_ (code to run before and after |
| 18 |
steps, scenarios, features or the whole shooting match). |
| 19 |
|
| 20 |
.. _`feature files`: #gherkin-feature-testing-language |
| 21 |
.. _`Python step implementations`: tutorial.html#python-step-implementations |
| 22 |
.. _`environmental controls`: tutorial.html#environmental-controls |
| 23 |
|
| 24 |
These files are typically stored in a directory called "features". The |
| 25 |
minimum requirement for a features directory is:: |
| 26 |
|
| 27 |
features/ |
| 28 |
features/everything.feature |
| 29 |
features/steps/ |
| 30 |
features/steps/steps.py |
| 31 |
|
| 32 |
A more complex directory might look like:: |
| 33 |
|
| 34 |
features/ |
| 35 |
features/signup.feature |
| 36 |
features/login.feature |
| 37 |
features/account_details.feature |
| 38 |
features/environment.py |
| 39 |
features/steps/ |
| 40 |
features/steps/website.py |
| 41 |
features/steps/utils.py |
| 42 |
|
| 43 |
|
| 44 |
Layout Variations |
| 45 |
----------------- |
| 46 |
|
| 47 |
*behave* has some flexibility built in. It will actually try quite hard to |
| 48 |
find feature specifications. When launched you may pass on the command |
| 49 |
line: |
| 50 |
|
| 51 |
**nothing** |
| 52 |
In the absence of any information *behave* will attempt to load your |
| 53 |
features from a subdirectory called "features" in the directory you launched |
| 54 |
*behave*. |
| 55 |
|
| 56 |
**a features directory path** |
| 57 |
This is the path to a features directory laid out as described above. It may be called |
| 58 |
anything by *must* contain at least one "*name*.feature" file and a directory |
| 59 |
called "steps". The "environment.py" file, if present, must be in the same |
| 60 |
directory that contains the "steps" directory (not *in* the "steps" |
| 61 |
directory). |
| 62 |
|
| 63 |
**the path to a "*name*.feature" file** |
| 64 |
This tells *behave* where to find the feature file. To find the steps |
| 65 |
directory *behave* will look in the directory containing the feature |
| 66 |
file. If it is not present, *behave* will look in the parent directory, |
| 67 |
and then its parent, and so on until it hits the root of the filesystem. |
| 68 |
The "environment.py" file, if present, must be in the same directory |
| 69 |
that contains the "steps" directory (not *in* the "steps" directory). |
| 70 |
|
| 71 |
**a directory containing your feature files** |
| 72 |
Similar to the approach above, you're identifying the directory where your |
| 73 |
"*name*.feature" files are, and if the "steps" directory is not in the |
| 74 |
same place then *behave* will search for it just like above. This allows |
| 75 |
you to have a layout like:: |
| 76 |
|
| 77 |
tests/ |
| 78 |
tests/environment.py |
| 79 |
tests/features/signup.feature |
| 80 |
tests/features/login.feature |
| 81 |
tests/features/account_details.feature |
| 82 |
tests/steps/ |
| 83 |
tests/steps/website.py |
| 84 |
tests/steps/utils.py |
| 85 |
|
| 86 |
Note that with this approach, if you want to execute *behave* without having |
| 87 |
to explicitly specify the directory (first option) you can set the ``paths`` |
| 88 |
setting in your `configuration file`_ (e.g. ``paths=tests``). |
| 89 |
|
| 90 |
If you're having trouble setting things up and want to see what *behave* is |
| 91 |
doing in attempting to find your features use the "-v" (verbose) |
| 92 |
command-line switch. |
| 93 |
|
| 94 |
.. _`configuration file`: behave.html#configuration-files |
| 95 |
|
| 96 |
.. _chapter.gherkin: |
| 97 |
|
| 98 |
Gherkin: Feature Testing Language |
| 99 |
================================= |
| 100 |
|
| 101 |
*behave* `features`_ are written using a language called `Gherkin`_ (with |
| 102 |
`some modifications`_) and are named "*name*.feature". |
| 103 |
|
| 104 |
.. _`some modifications`: #modifications-to-the-gherkin-standard |
| 105 |
|
| 106 |
These files should be written using natural language - ideally by the |
| 107 |
non-technical business participants in the software project. Feature files |
| 108 |
serve two purposes – documentation and automated tests. |
| 109 |
|
| 110 |
It is very flexible but has a few simple rules that writers need to adhere to. |
| 111 |
|
| 112 |
Line endings terminate statements (eg, steps). Either spaces or tabs may be |
| 113 |
used for indentation (but spaces are more portable). Indentation is almost |
| 114 |
always ignored - it's a tool for the feature writer to express some |
| 115 |
structure in the text. Most lines start with a keyword ("Feature", |
| 116 |
"Scenario", "Given", ...) |
| 117 |
|
| 118 |
Comment lines are allowed anywhere in the file. They begin with zero or |
| 119 |
more spaces, followed by a sharp sign (#) and some amount of text. |
| 120 |
|
| 121 |
.. _`gherkin`: https://github.com/cucumber/cucumber/wiki/Gherkin |
| 122 |
|
| 123 |
|
| 124 |
Features |
| 125 |
-------- |
| 126 |
|
| 127 |
Features are composed of scenarios. They may optionally have a description, |
| 128 |
a background and a set of tags. In its simplest form a feature looks like: |
| 129 |
|
| 130 |
.. code-block:: gherkin |
| 131 |
|
| 132 |
Feature: feature name |
| 133 |
|
| 134 |
Scenario: some scenario |
| 135 |
Given some condition |
| 136 |
Then some result is expected. |
| 137 |
|
| 138 |
In all its glory it could look like: |
| 139 |
|
| 140 |
.. code-block:: gherkin |
| 141 |
|
| 142 |
@tags @tag |
| 143 |
Feature: feature name |
| 144 |
description |
| 145 |
further description |
| 146 |
|
| 147 |
Background: some requirement of this test |
| 148 |
Given some setup condition |
| 149 |
And some other setup action |
| 150 |
|
| 151 |
Scenario: some scenario |
| 152 |
Given some condition |
| 153 |
When some action is taken |
| 154 |
Then some result is expected. |
| 155 |
|
| 156 |
Scenario: some other scenario |
| 157 |
Given some other condition |
| 158 |
When some action is taken |
| 159 |
Then some other result is expected. |
| 160 |
|
| 161 |
Scenario: ... |
| 162 |
|
| 163 |
The feature name should just be some reasonably descriptive title for the |
| 164 |
feature being tested, like "the message posting interface". The following |
| 165 |
description is optional and serves to clarify any potential confusion or |
| 166 |
scope issue in the feature name. The description is for the benefit of |
| 167 |
humans reading the feature text. |
| 168 |
|
| 169 |
.. any other advice we could include here? |
| 170 |
|
| 171 |
The Background part and the Scenarios will be discussed in the following sections. |
| 172 |
|
| 173 |
|
| 174 |
Background |
| 175 |
---------- |
| 176 |
|
| 177 |
A background consists of a series of steps similar to `scenarios`_. |
| 178 |
It allows you to add some context to the scenarios of a feature. |
| 179 |
A background is executed before each scenario of this feature but after any |
| 180 |
of the before hooks. It is useful for performing setup operations like: |
| 181 |
|
| 182 |
* logging into a web browser or |
| 183 |
* setting up a database with test data used by the scenarios. |
| 184 |
|
| 185 |
The background description is for the benefit of humans reading the feature text. |
| 186 |
Again the background name should just be a reasonably descriptive title |
| 187 |
for the background operation being performed or requirement being met. |
| 188 |
|
| 189 |
A background section may exist only once within a feature file. |
| 190 |
In addition, a background must be defined before any scenario or |
| 191 |
scenario outline. |
| 192 |
|
| 193 |
It contains `steps`_ as described below. |
| 194 |
|
| 195 |
**Good practices for using Background** |
| 196 |
|
| 197 |
Don’t use "Background" to set up complicated state unless that state is actually something the client needs to know. |
| 198 |
For example, if the user and site names don’t matter to the client, you |
| 199 |
should use a high-level step such as "Given that I am logged in as a site |
| 200 |
owner". |
| 201 |
|
| 202 |
Keep your "Background" section short. |
| 203 |
You’re expecting the user to actually remember this stuff when reading |
| 204 |
your scenarios. If the background is more than 4 lines long, can you move |
| 205 |
some of the irrelevant details into high-level steps? See `calling steps |
| 206 |
from other steps`_. |
| 207 |
|
| 208 |
Make your "Background" section vivid. |
| 209 |
You should use colorful names and try to tell a story, because the human |
| 210 |
brain can keep track of stories much better than it can keep track of |
| 211 |
names like "User A", "User B", "Site 1", and so on. |
| 212 |
|
| 213 |
Keep your scenarios short, and don’t have too many. |
| 214 |
If the background section has scrolled off the screen, you should think |
| 215 |
about using higher-level steps, or splitting the features file in two. |
| 216 |
|
| 217 |
.. _`calling steps from other steps`: api.html#calling-steps-from-other-steps |
| 218 |
.. _`Cucumber Background description`: https://github.com/cucumber/cucumber/wiki/Background |
| 219 |
|
| 220 |
|
| 221 |
Scenarios |
| 222 |
--------- |
| 223 |
|
| 224 |
Scenarios describe the discrete behaviours being tested. They are given a |
| 225 |
title which should be a reasonably descriptive title for the scenario being |
| 226 |
tested. The scenario description is for the benefit of humans reading the |
| 227 |
feature text. |
| 228 |
|
| 229 |
Scenarios are composed of a series of `steps`_ as described below. The |
| 230 |
steps typically take the form of "given some condition" "then we expect |
| 231 |
some test will pass." In this simplest form, a scenario might be: |
| 232 |
|
| 233 |
.. code-block:: gherkin |
| 234 |
|
| 235 |
Scenario: we have some stock when we open the store |
| 236 |
Given that the store has just opened |
| 237 |
then we should have items for sale. |
| 238 |
|
| 239 |
There may be additional conditions imposed on the scenario, and these would |
| 240 |
take the form of "when" steps following the initial "given" condition. If |
| 241 |
necessary, additional "and" or "but" steps may also follow the "given", |
| 242 |
"when" and "then" steps if more needs to be tested. A more complex example |
| 243 |
of a scenario might be: |
| 244 |
|
| 245 |
.. code-block:: gherkin |
| 246 |
|
| 247 |
Scenario: Replaced items should be returned to stock |
| 248 |
Given that a customer buys a blue garment |
| 249 |
and I have two blue garments in stock |
| 250 |
but I have no red garments in stock |
| 251 |
and three black garments in stock. |
| 252 |
When he returns the garment for a replacement in black, |
| 253 |
then I should have three blue garments in stock |
| 254 |
and no red garments in stock, |
| 255 |
and two black garments in stock. |
| 256 |
|
| 257 |
It is good practise to have a scenario test only one behaviour or desired |
| 258 |
outcome. |
| 259 |
|
| 260 |
Scenarios contain `steps`_ as described below. |
| 261 |
|
| 262 |
|
| 263 |
Scenario Outlines |
| 264 |
----------------- |
| 265 |
|
| 266 |
These may be used when you have a set of expected conditions and outcomes |
| 267 |
to go along with your scenario `steps`_. |
| 268 |
|
| 269 |
An outline includes keywords in the step definitions which are filled in |
| 270 |
using values from example tables. You may have a number of example tables |
| 271 |
in each scenario outline. |
| 272 |
|
| 273 |
.. code-block:: gherkin |
| 274 |
|
| 275 |
Scenario Outline: Blenders |
| 276 |
Given I put <thing> in a blender, |
| 277 |
when I switch the blender on |
| 278 |
then it should transform into <other thing> |
| 279 |
|
| 280 |
Examples: Amphibians |
| 281 |
| thing | other thing | |
| 282 |
| Red Tree Frog | mush | |
| 283 |
|
| 284 |
Examples: Consumer Electronics |
| 285 |
| thing | other thing | |
| 286 |
| iPhone | toxic waste | |
| 287 |
| Galaxy Nexus | toxic waste | |
| 288 |
|
| 289 |
*behave* will run the scenario once for each (non-heading) line appearing |
| 290 |
in the example data tables. |
| 291 |
|
| 292 |
The values to replace are determined using the name appearing in the angle |
| 293 |
brackets "<*name*>" which must match a headings of the example tables. The |
| 294 |
name may include almost any character, though not the close angle bracket |
| 295 |
">". |
| 296 |
|
| 297 |
Substitution may also occur in `step data`_ if the "<*name*>" texts appear |
| 298 |
within the step data text or table cells. |
| 299 |
|
| 300 |
|
| 301 |
Steps |
| 302 |
----- |
| 303 |
|
| 304 |
Steps take a line each and begin with a *keyword* - one of "given", "when", |
| 305 |
"then", "and" or "but". |
| 306 |
|
| 307 |
In a formal sense the keywords are all Title Case, though some languages |
| 308 |
allow all-lowercase keywords where that makes sense. |
| 309 |
|
| 310 |
Steps should not need to contain significant degree of detail about the |
| 311 |
mechanics of testing; that is, instead of: |
| 312 |
|
| 313 |
.. code-block:: gherkin |
| 314 |
|
| 315 |
Given a browser client is used to load the URL "http://website.example/website/home.html" |
| 316 |
|
| 317 |
the step could instead simply say: |
| 318 |
|
| 319 |
.. code-block:: gherkin |
| 320 |
|
| 321 |
Given we are looking at the home page |
| 322 |
|
| 323 |
Steps are implemented using Python code which is implemented in the "steps" |
| 324 |
directory in Python modules (files with Python code which are named |
| 325 |
"*name*.py".) The naming of the Python modules does not matter. *All* modules |
| 326 |
in the "steps" directory will be imported by *behave* at startup to |
| 327 |
discover the step implementations. |
| 328 |
|
| 329 |
Given, When, Then (And, But) |
| 330 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 331 |
|
| 332 |
*behave* doesn't technically distinguish between the various kinds of steps. |
| 333 |
However, we strongly recommend that you do! These words have been carefully |
| 334 |
selected for their purpose, and you should know what the purpose is to get |
| 335 |
into the BDD mindset. |
| 336 |
|
| 337 |
Given |
| 338 |
""""" |
| 339 |
|
| 340 |
The purpose of givens is to **put the system in a known state** before the |
| 341 |
user (or external system) starts interacting with the system (in the When |
| 342 |
steps). Avoid talking about user interaction in givens. If you had worked |
| 343 |
with usecases, you would call this preconditions. |
| 344 |
|
| 345 |
Examples: |
| 346 |
|
| 347 |
- Create records (model instances) / set up the database state. |
| 348 |
- It's ok to call directly into your application model here. |
| 349 |
- Log in a user (An exception to the no-interaction recommendation. Things |
| 350 |
that "happened earlier" are ok). |
| 351 |
|
| 352 |
You might also use Given with a multiline table argument to set up database |
| 353 |
records instead of fixtures hard-coded in steps. This way you can read |
| 354 |
the scenario and make sense out of it without having to look elsewhere (at |
| 355 |
the fixtures). |
| 356 |
|
| 357 |
When |
| 358 |
"""" |
| 359 |
|
| 360 |
Each of these steps should **describe the key action** the user (or |
| 361 |
external system) performs. This is the interaction with your system which |
| 362 |
should (or perhaps should not) cause some state to change. |
| 363 |
|
| 364 |
Examples: |
| 365 |
|
| 366 |
- Interact with a web page (`Requests`_/`Twill`_/`Selenium`_ *interaction* |
| 367 |
etc should mostly go into When steps). |
| 368 |
- Interact with some other user interface element. |
| 369 |
- Developing a library? Kicking off some kind of action that has an |
| 370 |
observable effect somewhere else. |
| 371 |
|
| 372 |
.. _`requests`: http://docs.python-requests.org/en/latest/ |
| 373 |
.. _`twill`: http://twill.idyll.org/ |
| 374 |
.. _`selenium`: http://docs.seleniumhq.org/projects/webdriver/ |
| 375 |
|
| 376 |
|
| 377 |
Then |
| 378 |
"""" |
| 379 |
|
| 380 |
Here we **observe outcomes**. The observations should be related to the |
| 381 |
business value/benefit in your feature description. The observations should |
| 382 |
also be on some kind of *output* - that is something that comes *out* of |
| 383 |
the system (report, user interface, message) and not something that is |
| 384 |
deeply buried inside it (that has no business value). |
| 385 |
|
| 386 |
Examples: |
| 387 |
|
| 388 |
- Verify that something related to the Given+When is (or is not) in the output |
| 389 |
- Check that some external system has received the expected message (was an |
| 390 |
email with specific content sent?) |
| 391 |
|
| 392 |
While it might be tempting to implement Then steps to just look in the |
| 393 |
database - resist the temptation. You should only verify outcome that is |
| 394 |
observable for the user (or external system) and databases usually are not. |
| 395 |
|
| 396 |
And, But |
| 397 |
"""""""" |
| 398 |
|
| 399 |
If you have several givens, whens or thens you could write: |
| 400 |
|
| 401 |
.. code-block:: gherkin |
| 402 |
|
| 403 |
Scenario: Multiple Givens |
| 404 |
Given one thing |
| 405 |
Given an other thing |
| 406 |
Given yet an other thing |
| 407 |
When I open my eyes |
| 408 |
Then I see something |
| 409 |
Then I don't see something else |
| 410 |
|
| 411 |
Or you can make it read more fluently by writing: |
| 412 |
|
| 413 |
.. code-block:: gherkin |
| 414 |
|
| 415 |
Scenario: Multiple Givens |
| 416 |
Given one thing |
| 417 |
And an other thing |
| 418 |
And yet an other thing |
| 419 |
When I open my eyes |
| 420 |
Then I see something |
| 421 |
But I don't see something else |
| 422 |
|
| 423 |
The two scenarios are identical to *behave* - steps beginning with "and" or |
| 424 |
"but" are exactly the same kind of steps as all the others. They simply |
| 425 |
mimic the step that preceeds them. |
| 426 |
|
| 427 |
|
| 428 |
Step Data |
| 429 |
~~~~~~~~~ |
| 430 |
|
| 431 |
Steps may have some text or a table of data attached to them. |
| 432 |
|
| 433 |
Substitution of scenario outline values will be done in step data text or |
| 434 |
table data if the "<*name*>" texts appear within the step data text or |
| 435 |
table cells. |
| 436 |
|
| 437 |
|
| 438 |
Text |
| 439 |
"""" |
| 440 |
|
| 441 |
Any text block following a step wrapped in ``"""`` lines will be associated |
| 442 |
with the step. This is the one case where indentation is actually parsed: |
| 443 |
the leading whitespace is stripped from the text, and successive lines of |
| 444 |
the text should have at least the same amount of whitespace as the first |
| 445 |
line. |
| 446 |
|
| 447 |
So for this rather contrived example: |
| 448 |
|
| 449 |
.. code-block:: gherkin |
| 450 |
|
| 451 |
Scenario: some scenario |
| 452 |
Given a sample text loaded into the frobulator |
| 453 |
""" |
| 454 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do |
| 455 |
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut |
| 456 |
enim ad minim veniam, quis nostrud exercitation ullamco laboris |
| 457 |
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in |
| 458 |
reprehenderit in voluptate velit esse cillum dolore eu fugiat |
| 459 |
nulla pariatur. Excepteur sint occaecat cupidatat non proident, |
| 460 |
sunt in culpa qui officia deserunt mollit anim id est laborum. |
| 461 |
""" |
| 462 |
When we activate the frobulator |
| 463 |
Then we will find it similar to English |
| 464 |
|
| 465 |
The text is available to the Python step code as the ".text" attribute |
| 466 |
in the :class:`~behave.runner.Context` variable passed into each step |
| 467 |
function. The text supplied on the first step in a scenario will be |
| 468 |
available on the context variable for the duration of that scenario. Any |
| 469 |
further text present on a subsequent step will overwrite previously-set |
| 470 |
text. |
| 471 |
|
| 472 |
|
| 473 |
Table |
| 474 |
""""" |
| 475 |
|
| 476 |
You may associate a table of data with a step by simply entering it, |
| 477 |
indented, following the step. This can be useful for loading specific |
| 478 |
required data into a model. |
| 479 |
|
| 480 |
The table formatting doesn't have to be strictly lined up but it does need |
| 481 |
to have the same number of columns on each line. A column is anything |
| 482 |
appearing between two vertical bars "|". Any whitespace between the column |
| 483 |
content and the vertical bar is removed. |
| 484 |
|
| 485 |
.. code-block:: gherkin |
| 486 |
|
| 487 |
Scenario: some scenario |
| 488 |
Given a set of specific users |
| 489 |
| name | department | |
| 490 |
| Barry | Beer Cans | |
| 491 |
| Pudey | Silly Walks | |
| 492 |
| Two-Lumps | Silly Walks | |
| 493 |
|
| 494 |
When we count the number of people in each department |
| 495 |
Then we will find two people in "Silly Walks" |
| 496 |
But we will find one person in "Beer Cans" |
| 497 |
|
| 498 |
The table is available to the Python step code as the ".table" attribute |
| 499 |
in the :class:`~behave.runner.Context` variable passed into each step |
| 500 |
function. The table is an instance of :class:`~behave.model.Table` and |
| 501 |
for the example above could be accessed like so: |
| 502 |
|
| 503 |
.. code-block:: python |
| 504 |
|
| 505 |
@given('a set of specific users') |
| 506 |
def step_impl(context): |
| 507 |
for row in context.table: |
| 508 |
model.add_user(name=row['name'], department=row['department']) |
| 509 |
|
| 510 |
There's a variety of ways to access the table data - see the |
| 511 |
:class:`~behave.model.Table` API documentation for the full details. |
| 512 |
|
| 513 |
|
| 514 |
Tags |
| 515 |
---- |
| 516 |
|
| 517 |
You may also "tag" parts of your feature file. At the simplest level this |
| 518 |
allows *behave* to selectively check parts of your feature set. |
| 519 |
|
| 520 |
You may tag features, scenarios or scenario outlines but nothing else. |
| 521 |
Any tag that exists in a feature will be inherited by its scenarios and |
| 522 |
scenario outlines. |
| 523 |
|
| 524 |
Tags appear on the line preceding the feature or scenario you wish to tag. |
| 525 |
You may have many space-separated tags on a single line. |
| 526 |
|
| 527 |
A tag takes the form of the at symbol "@" followed by a word (which may |
| 528 |
include underscores "_"). Valid tag lines include:: |
| 529 |
|
| 530 |
@slow |
| 531 |
@wip |
| 532 |
@needs_database @slow |
| 533 |
|
| 534 |
For example: |
| 535 |
|
| 536 |
.. code-block:: gherkin |
| 537 |
|
| 538 |
@wip @slow |
| 539 |
Feature: annual reporting |
| 540 |
Some description of a slow reporting system. |
| 541 |
|
| 542 |
or: |
| 543 |
|
| 544 |
.. code-block:: gherkin |
| 545 |
|
| 546 |
@wip |
| 547 |
@slow |
| 548 |
Feature: annual reporting |
| 549 |
Some description of a slow reporting system. |
| 550 |
|
| 551 |
Tags may be used to `control your test run`_ by only including certain |
| 552 |
features or scenarios based on tag selection. The tag information may also |
| 553 |
be accessed from the `Python code backing up the tests`_. |
| 554 |
|
| 555 |
.. _`control your test run`: #controlling-your-test-run-with-tags |
| 556 |
.. _`Python code backing up the tests`: #accessing-tag-information-in-python |
| 557 |
|
| 558 |
|
| 559 |
Controlling Your Test Run With Tags |
| 560 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 561 |
|
| 562 |
Given a feature file with: |
| 563 |
|
| 564 |
.. code-block:: gherkin |
| 565 |
|
| 566 |
Feature: Fight or flight |
| 567 |
In order to increase the ninja survival rate, |
| 568 |
As a ninja commander |
| 569 |
I want my ninjas to decide whether to take on an |
| 570 |
opponent based on their skill levels |
| 571 |
|
| 572 |
@slow |
| 573 |
Scenario: Weaker opponent |
| 574 |
Given the ninja has a third level black-belt |
| 575 |
When attacked by a samurai |
| 576 |
Then the ninja should engage the opponent |
| 577 |
|
| 578 |
Scenario: Stronger opponent |
| 579 |
Given the ninja has a third level black-belt |
| 580 |
When attacked by Chuck Norris |
| 581 |
Then the ninja should run for his life |
| 582 |
|
| 583 |
then running ``behave --tags=slow`` will run just the scenarios tagged |
| 584 |
``@slow``. If you wish to check everything *except* the slow ones then you |
| 585 |
may run ``behave --tags=-slow``. |
| 586 |
|
| 587 |
Another common use-case is to tag a scenario you're working on with |
| 588 |
``@wip`` and then ``behave --tags=wip`` to just test that one case. |
| 589 |
|
| 590 |
Tag selection on the command-line may be combined: |
| 591 |
|
| 592 |
**--tags=wip,slow** |
| 593 |
This will select all the cases tagged *either* "wip" or "slow". |
| 594 |
|
| 595 |
**--tags=wip --tags=slow** |
| 596 |
This will select all the cases tagged *both* "wip" and "slow". |
| 597 |
|
| 598 |
If a feature or scenario is tagged and then skipped because of a |
| 599 |
command-line control then the *before_* and *after_* environment functions |
| 600 |
will not be called for that feature or scenario. |
| 601 |
|
| 602 |
|
| 603 |
Accessing Tag Information In Python |
| 604 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 605 |
|
| 606 |
The tags attached to a feature and scenario are available in |
| 607 |
the environment functions via the "feature" or "scenario" object passed to |
| 608 |
them. On those objects there is an attribute called "tags" which is a list |
| 609 |
of the tag names attached, in the order they're found in the features file. |
| 610 |
|
| 611 |
There are also `environmental controls`_ specific to tags, so in the above |
| 612 |
example *behave* will attempt to invoke an ``environment.py`` function |
| 613 |
``before_tag`` and ``after_tag`` before and after the Scenario tagged |
| 614 |
``@slow``, passing in the name "slow". If multiple tags are present then |
| 615 |
the functions will be called multiple times with each tag in the order |
| 616 |
they're defined in the feature file. |
| 617 |
|
| 618 |
Re-visiting the example from above; if only some of the features required a |
| 619 |
browser and web server then you could tag them ``@browser``: |
| 620 |
|
| 621 |
.. code-block:: python |
| 622 |
|
| 623 |
def before_feature(context, feature): |
| 624 |
model.init(environment='test') |
| 625 |
if 'browser' in feature.tags: |
| 626 |
context.server = simple_server.WSGIServer(('', 8000)) |
| 627 |
context.server.set_app(web_app.main(environment='test')) |
| 628 |
context.thread = threading.Thread(target=context.server.serve_forever) |
| 629 |
context.thread.start() |
| 630 |
context.browser = webdriver.Chrome() |
| 631 |
|
| 632 |
def after_feature(context, feature): |
| 633 |
if 'browser' in feature.tags: |
| 634 |
context.server.shutdown() |
| 635 |
context.thread.join() |
| 636 |
context.browser.quit() |
| 637 |
|
| 638 |
|
| 639 |
Languages Other Than English |
| 640 |
---------------------------- |
| 641 |
|
| 642 |
English is the default language used in parsing feature files. If you wish |
| 643 |
to use a different language you should check to see whether it is |
| 644 |
available:: |
| 645 |
|
| 646 |
behave --lang-list |
| 647 |
|
| 648 |
This command lists all the supported languages. If yours is present then |
| 649 |
you have two options: |
| 650 |
|
| 651 |
1. add a line to the top of the feature files like (for French): |
| 652 |
|
| 653 |
# language: fr |
| 654 |
|
| 655 |
2. use the command-line switch ``--lang``:: |
| 656 |
|
| 657 |
behave --lang=fr |
| 658 |
|
| 659 |
The feature file keywords will now use the French translations. To see what |
| 660 |
the language equivalents recognised by *behave* are, use:: |
| 661 |
|
| 662 |
behave --lang-help fr |
| 663 |
|
| 664 |
|
| 665 |
Modifications to the Gherkin Standard |
| 666 |
------------------------------------- |
| 667 |
|
| 668 |
*behave* can parse standard Gherkin files and extends Gherkin to allow |
| 669 |
lowercase step keywords because these can sometimes allow more readable |
| 670 |
feature specifications. |
| 671 |
|