| 1 |
Feature: Scenario Outline -- Parametrized Scenarios |
| 2 |
|
| 3 |
As a test writer |
| 4 |
I want to use the DRY principle when writing scenarios |
| 5 |
So that I am more productive and my work is less error-prone. |
| 6 |
|
| 7 |
. COMMENT: |
| 8 |
. A Scenario Outline is basically a parametrized Scenario template. |
| 9 |
. It is instantiated for each examples row with the corresponding data. |
| 10 |
. |
| 11 |
. SCENARIO GENERICS BASICS: What can be parametrized? |
| 12 |
. * Scenario name: Based on Scenario Outline with placeholders |
| 13 |
. * Scenario steps: Step name with placeholders (including: step.text, step.table) |
| 14 |
. * Scenario tags: Based on Scenario Outline tags with placeholders |
| 15 |
. |
| 16 |
. CONSTRAINTS: |
| 17 |
. * placeholders (names) for tags should not contain any whitespace. |
| 18 |
. * a row data placeholder may override/hide a special placeholder (see below). |
| 19 |
. |
| 20 |
. SPECIAL PLACEHOLDERS: |
| 21 |
. | Placeholder | Description | |
| 22 |
. | examples.name | Name of the examples group (or empty string). | |
| 23 |
. | examples.index | Index of examples group (range: 1..N). | |
| 24 |
. | row.index | Index of row in examples group (range: 1..R). | |
| 25 |
. | row.id | Same as: "{example.index}.{row.index}" | |
| 26 |
|
| 27 |
|
| 28 |
@setup |
| 29 |
Scenario: Test Setup |
| 30 |
Given a new working directory |
| 31 |
And a file named "behave.ini" with: |
| 32 |
""" |
| 33 |
[behave] |
| 34 |
scenario_outline_annotation_schema = {name} -*- {examples.name} |
| 35 |
""" |
| 36 |
And a file named "features/steps/param_steps.py" with: |
| 37 |
""" |
| 38 |
from behave import step |
| 39 |
|
| 40 |
@step('a param {value:w}') |
| 41 |
def step_with_param(context, value): |
| 42 |
context.param = value |
| 43 |
|
| 44 |
@step('a param {name}={value}') |
| 45 |
def step_with_param_value(context, name, value): |
| 46 |
context.param_name = name |
| 47 |
context.param_value = value |
| 48 |
|
| 49 |
@step('a step with text') |
| 50 |
def step_with_param_value(context): |
| 51 |
assert context.text is not None, "REQUIRE: text" |
| 52 |
|
| 53 |
@step('a step with table') |
| 54 |
def step_with_param_value(context): |
| 55 |
assert context.table is not None, "REQUIRE: table" |
| 56 |
|
| 57 |
@step('an unknown param {name}={value}') |
| 58 |
def step_with_unknown_param_value(context, name, value): |
| 59 |
pass |
| 60 |
""" |
| 61 |
|
| 62 |
@parametrize.name |
| 63 |
Scenario: Parametrized name in generated Scenarios (Case: Row Placeholders) |
| 64 |
Given a file named "features/use_name_with_row_params.feature" with: |
| 65 |
""" |
| 66 |
Feature: SOP1 |
| 67 |
Scenario Outline: Use placeholder <name>-<param1> |
| 68 |
Given a param param1=<param1> |
| 69 |
And a param name=<name> |
| 70 |
|
| 71 |
Examples: E-<ID> |
| 72 |
| ID | name | param1 | |
| 73 |
| 001 | Alice | 100 | |
| 74 |
| 002 | Bob | 101 | |
| 75 |
""" |
| 76 |
When I run "behave -f progress3 features/use_name_with_row_params.feature" |
| 77 |
Then it should pass with: |
| 78 |
""" |
| 79 |
Use placeholder Alice-100 -*- E-001 .. |
| 80 |
Use placeholder Bob-101 -*- E-002 .. |
| 81 |
""" |
| 82 |
|
| 83 |
@parametrize.name |
| 84 |
Scenario: Parametrized name in generated Scenarios (Case: Special Placeholders) |
| 85 |
Given a file named "features/use_name_with_special_params.feature" with: |
| 86 |
""" |
| 87 |
Feature: SOP2 |
| 88 |
Scenario Outline: Use placeholder S<examples.index>-<row.index> <examples.name> |
| 89 |
Given a param name=<name> |
| 90 |
And a param row.id=<row.id> |
| 91 |
|
| 92 |
Examples: E-<ID>/@<row.id> |
| 93 |
| ID | name | param1 | |
| 94 |
| 001 | Alice | 100 | |
| 95 |
| 002 | Bob | 101 | |
| 96 |
""" |
| 97 |
When I run "behave -f progress3 features/use_name_with_special_params.feature" |
| 98 |
Then it should pass with: |
| 99 |
""" |
| 100 |
Use placeholder S1-1 E-001/@1.1 -*- E-001/@1.1 .. |
| 101 |
Use placeholder S1-2 E-002/@1.2 -*- E-002/@1.2 .. |
| 102 |
""" |
| 103 |
|
| 104 |
@parametrize.steps |
| 105 |
Scenario: Use placeholders in generated scenario steps |
| 106 |
Given a file named "features/use_steps_with_params.feature" with: |
| 107 |
""" |
| 108 |
Feature: |
| 109 |
Scenario Outline: Use row placeholders |
| 110 |
Given a param ID=<ID> |
| 111 |
And a param name=<name> |
| 112 |
And a param param1=<param1> |
| 113 |
|
| 114 |
Examples: |
| 115 |
| ID | name | param1 | |
| 116 |
| 001 | Alice | 101 | |
| 117 |
| 002 | Bob | 102 | |
| 118 |
""" |
| 119 |
When I run "behave -f plain --no-timings features/use_steps_with_params.feature" |
| 120 |
Then it should pass with: |
| 121 |
""" |
| 122 |
Scenario Outline: Use row placeholders -*- |
| 123 |
Given a param ID=001 ... passed |
| 124 |
And a param name=Alice ... passed |
| 125 |
And a param param1=101 ... passed |
| 126 |
|
| 127 |
Scenario Outline: Use row placeholders -*- |
| 128 |
Given a param ID=002 ... passed |
| 129 |
And a param name=Bob ... passed |
| 130 |
And a param param1=102 ... passed |
| 131 |
""" |
| 132 |
|
| 133 |
@parametrize.steps |
| 134 |
Scenario: Use an unknown placeholder in generated scenario steps |
| 135 |
Given a file named "features/use_steps_with_unknown_params.feature" with: |
| 136 |
""" |
| 137 |
Feature: |
| 138 |
Scenario Outline: Use unknown placeholders |
| 139 |
Given an unknown param unknown=<unknown> |
| 140 |
|
| 141 |
Examples: |
| 142 |
| ID | name | param1 | |
| 143 |
| 001 | Alice | 100 | |
| 144 |
""" |
| 145 |
When I run "behave -f plain --no-timings features/use_steps_with_unknown_params.feature" |
| 146 |
Then it should pass with: |
| 147 |
""" |
| 148 |
Scenario Outline: Use unknown placeholders -*- |
| 149 |
Given an unknown param unknown=<unknown> ... passed |
| 150 |
""" |
| 151 |
But note that "unknown placeholders are not replaced" |
| 152 |
|
| 153 |
|
| 154 |
@parametrize.steps |
| 155 |
Scenario: Use placeholders in generated scenario step.text |
| 156 |
Given a file named "features/use_steps_with_param_text.feature" with: |
| 157 |
''' |
| 158 |
Feature: |
| 159 |
Scenario Outline: Use parametrized step with text |
| 160 |
Given a step with text: |
| 161 |
""" |
| 162 |
<greeting> <name>; |
| 163 |
Travel agency: <travel agency> |
| 164 |
""" |
| 165 |
|
| 166 |
Examples: |
| 167 |
| ID | name | greeting | travel agency | |
| 168 |
| 001 | Alice | Hello | Pony express | |
| 169 |
''' |
| 170 |
When I run "behave -f plain --no-timings features/use_steps_with_param_text.feature" |
| 171 |
Then it should pass with: |
| 172 |
''' |
| 173 |
Scenario Outline: Use parametrized step with text -*- |
| 174 |
Given a step with text ... passed |
| 175 |
""" |
| 176 |
Hello Alice; |
| 177 |
Travel agency: Pony express |
| 178 |
""" |
| 179 |
''' |
| 180 |
And note that "placeholders in step.text are replaced with row data" |
| 181 |
|
| 182 |
|
| 183 |
@parametrize.steps |
| 184 |
Scenario: Use placeholders in generated scenario step.table |
| 185 |
Given a file named "features/use_steps_with_param_table.feature" with: |
| 186 |
""" |
| 187 |
Feature: |
| 188 |
Scenario Outline: Use parametrized step with table |
| 189 |
Given a step with table: |
| 190 |
| Id | Name | Travel Agency | row id | |
| 191 |
| <ID> | <name> | <travel agency> | <row.id> | |
| 192 |
|
| 193 |
Examples: |
| 194 |
| ID | name | greeting | travel agency | |
| 195 |
| 001 | Alice | Hello | Pony express | |
| 196 |
""" |
| 197 |
When I run "behave -f plain --no-timings features/use_steps_with_param_table.feature" |
| 198 |
Then it should pass with: |
| 199 |
""" |
| 200 |
Scenario Outline: Use parametrized step with table -*- |
| 201 |
Given a step with table ... passed |
| 202 |
| Id | Name | Travel Agency | row id | |
| 203 |
| 001 | Alice | Pony express | <row.id> | |
| 204 |
""" |
| 205 |
And note that "placeholders in step.table cells are replaced with row data" |
| 206 |
But note that "<row.id> is currently not supported in table cells (like other special placeholders)" |
| 207 |
|
| 208 |
|
| 209 |
@parametrize.steps |
| 210 |
Scenario: Use special placeholders in generated scenario steps |
| 211 |
Given a file named "features/use_steps_with_special_params.feature" with: |
| 212 |
""" |
| 213 |
Feature: |
| 214 |
Scenario Outline: Use special placeholders @<row.id> |
| 215 |
Given a param name=<name> |
| 216 |
And a param examples.name=<examples.name> |
| 217 |
And a param examples.index=<examples.index> |
| 218 |
And a param row.index=<row.index> |
| 219 |
And a param row.id=<row.id> |
| 220 |
|
| 221 |
Examples: E-<ID>/@<row.id> |
| 222 |
| ID | name | param1 | |
| 223 |
| 001 | Alice | 100 | |
| 224 |
| 002 | Bob | 101 | |
| 225 |
""" |
| 226 |
When I run "behave -f plain --no-timings features/use_steps_with_special_params.feature" |
| 227 |
Then it should pass with: |
| 228 |
""" |
| 229 |
Scenario Outline: Use special placeholders @1.1 -*- E-001/@1.1 |
| 230 |
Given a param name=Alice ... passed |
| 231 |
And a param examples.name=E-001/@1.1 ... passed |
| 232 |
And a param examples.index=1 ... passed |
| 233 |
And a param row.index=1 ... passed |
| 234 |
And a param row.id=1.1 ... passed |
| 235 |
|
| 236 |
Scenario Outline: Use special placeholders @1.2 -*- E-002/@1.2 |
| 237 |
Given a param name=Bob ... passed |
| 238 |
And a param examples.name=E-002/@1.2 ... passed |
| 239 |
And a param examples.index=1 ... passed |
| 240 |
And a param row.index=2 ... passed |
| 241 |
And a param row.id=1.2 ... passed |
| 242 |
""" |
| 243 |
|
| 244 |
|
| 245 |
@parametrize.name |
| 246 |
@parametrize.steps |
| 247 |
Scenario: When special placeholder name is used for row data (Case: Override) |
| 248 |
|
| 249 |
Ensure that row data (placeholder) can override special placeholder (if needed). |
| 250 |
AFFECTED: scenario.name, examples.name, step.name |
| 251 |
|
| 252 |
Given a file named "behave.ini" with: |
| 253 |
""" |
| 254 |
[behave] |
| 255 |
scenario_outline_annotation_schema = {name} -*- special:row.id=@{row.id} {examples.name} |
| 256 |
""" |
| 257 |
And a file named "features/use_name_with_overwritten_params.feature" with: |
| 258 |
""" |
| 259 |
Feature: SOP3 |
| 260 |
Scenario Outline: Use placeholder data:row.id=<row.id> |
| 261 |
Given a param name=<name> |
| 262 |
And a param row.id=<row.id> |
| 263 |
|
| 264 |
Examples: E-<ID>/<row.id> |
| 265 |
| ID | name | row.id | |
| 266 |
| 001 | Alice | 100 | |
| 267 |
| 002 | Bob | 101 | |
| 268 |
""" |
| 269 |
When I run "behave -f progress3 features/use_name_with_overwritten_params.feature" |
| 270 |
Then it should pass with: |
| 271 |
""" |
| 272 |
Use placeholder data:row.id=100 -*- special:[email protected] E-001/100 .. |
| 273 |
Use placeholder data:row.id=101 -*- special:[email protected] E-002/101 .. |
| 274 |
""" |
| 275 |
When I run "behave -f plain --no-timings features/use_name_with_overwritten_params.feature" |
| 276 |
Then it should pass with: |
| 277 |
""" |
| 278 |
Scenario Outline: Use placeholder data:row.id=100 -*- special:[email protected] E-001/100 |
| 279 |
Given a param name=Alice ... passed |
| 280 |
And a param row.id=100 ... passed |
| 281 |
|
| 282 |
Scenario Outline: Use placeholder data:row.id=101 -*- special:[email protected] E-002/101 |
| 283 |
Given a param name=Bob ... passed |
| 284 |
And a param row.id=101 ... passed |
| 285 |
""" |
| 286 |
But note that "a row data placeholder can override a special placeholder" |
| 287 |
And note that "the name annotation {row.id} still allows to refer to the special one" |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
@parametrize.steps |
| 292 |
Scenario: Placeholder value has placeholder syntax (Case: recursion-check) |
| 293 |
|
| 294 |
Ensure that weird placeholder value cases are handled correctly. |
| 295 |
Ensure that no recursion occurs. |
| 296 |
|
| 297 |
Given a file named "behave.ini" with: |
| 298 |
""" |
| 299 |
[behave] |
| 300 |
scenario_outline_annotation_schema = {name} -*- @{row.id} {examples.name} |
| 301 |
""" |
| 302 |
And a file named "features/use_value_with_placeholder_syntax.feature" with: |
| 303 |
""" |
| 304 |
Feature: |
| 305 |
Scenario Outline: Use weird placeholder values |
| 306 |
Given a param name=<name> |
| 307 |
|
| 308 |
Examples: |
| 309 |
| name | Expected | ID | Case: | |
| 310 |
| <ID> | 001 | 001 | Value refers to other, known placeholder.| |
| 311 |
| <ID> | 002 | 002 | Check if row specific value is used. | |
| 312 |
| <unknown> | <unkown> | 003 | Value refers to unknown placeholder. | |
| 313 |
| <name> | <name> | 004 | Value refers to itself (recursion?). | |
| 314 |
""" |
| 315 |
When I run "behave -f plain --no-timings features/use_value_with_placeholder_syntax.feature" |
| 316 |
Then it should pass with: |
| 317 |
""" |
| 318 |
Scenario Outline: Use weird placeholder values -*- @1.1 |
| 319 |
Given a param name=001 ... passed |
| 320 |
|
| 321 |
Scenario Outline: Use weird placeholder values -*- @1.2 |
| 322 |
Given a param name=002 ... passed |
| 323 |
|
| 324 |
Scenario Outline: Use weird placeholder values -*- @1.3 |
| 325 |
Given a param name=<unknown> ... passed |
| 326 |
|
| 327 |
Scenario Outline: Use weird placeholder values -*- @1.4 |
| 328 |
Given a param name=<name> ... passed |
| 329 |
""" |
| 330 |
|
| 331 |
@simple.case |
| 332 |
@parametrize.tags |
| 333 |
Scenario: Parametrized tags in a Scenario Outline |
| 334 |
Given a file named "behave.ini" with: |
| 335 |
""" |
| 336 |
[behave] |
| 337 |
scenario_outline_annotation_schema = {name} -*- @{row.id} {examples.name} |
| 338 |
""" |
| 339 |
And a file named "features/parametrized_tags.feature" with: |
| 340 |
""" |
| 341 |
Feature: |
| 342 |
@foo @outline.e<examples.index> @outline.row.<row.id> @outline.ID.<ID> |
| 343 |
Scenario Outline: Use parametrized tags |
| 344 |
Given a param name=<name> |
| 345 |
|
| 346 |
Examples: |
| 347 |
| ID | name | |
| 348 |
| 001 | Alice | |
| 349 |
| 002 | Bob | |
| 350 |
""" |
| 351 |
When I run "behave -f pretty -c --no-timings features/parametrized_tags.feature" |
| 352 |
Then it should pass with: |
| 353 |
""" |
| 354 |
@foo @outline.e1 @outline.row.1.1 @outline.ID.001 |
| 355 |
Scenario Outline: Use parametrized tags -*- @1.1 # features/parametrized_tags.feature:8 |
| 356 |
Given a param name=Alice # features/steps/param_steps.py:7 |
| 357 |
|
| 358 |
@foo @outline.e1 @outline.row.1.2 @outline.ID.002 |
| 359 |
Scenario Outline: Use parametrized tags -*- @1.2 # features/parametrized_tags.feature:9 |
| 360 |
Given a param name=Bob # features/steps/param_steps.py:7 |
| 361 |
""" |
| 362 |
But note that "special and row data placeholders can be used in tags" |
| 363 |
|
| 364 |
|
| 365 |
@parametrize.tags |
| 366 |
Scenario: Parametrized tags in a Scenario Outline (Case: Whitespace... in value) |
| 367 |
Given a file named "behave.ini" with: |
| 368 |
""" |
| 369 |
[behave] |
| 370 |
scenario_outline_annotation_schema = {name} -*- @{row.id} {examples.name} |
| 371 |
""" |
| 372 |
And a file named "features/parametrized_tags2.feature" with: |
| 373 |
""" |
| 374 |
Feature: |
| 375 |
@outline.name.<name> |
| 376 |
Scenario Outline: Use parametrized tags |
| 377 |
Given a param name=<name> |
| 378 |
|
| 379 |
Examples: |
| 380 |
| ID | name | Case: | |
| 381 |
| 001 | Alice Cooper | Placeholder value w/ whitespace | |
| 382 |
| 002 | Bob\tMarley | Placeholder value w/ tab | |
| 383 |
| 003 | Joe\nCocker | Placeholder value w/ newline | |
| 384 |
""" |
| 385 |
When I run "behave -f pretty -c --no-source features/parametrized_tags2.feature" |
| 386 |
Then it should pass with: |
| 387 |
""" |
| 388 |
@outline.name.Alice_Cooper |
| 389 |
Scenario Outline: Use parametrized tags -*- @1.1 |
| 390 |
Given a param name=Alice Cooper |
| 391 |
|
| 392 |
@outline.name.Bob_Marley |
| 393 |
Scenario Outline: Use parametrized tags -*- @1.2 |
| 394 |
Given a param name=Bob\tMarley |
| 395 |
|
| 396 |
@outline.name.Joe_Cocker |
| 397 |
Scenario Outline: Use parametrized tags -*- @1.3 |
| 398 |
Given a param name=Joe\nCocker |
| 399 |
""" |
| 400 |
But note that "placeholder values with special chars (whitespace, ...) are transformed" |
| 401 |
|