| 1 |
Feature: Background |
| 2 |
|
| 3 |
As a test writer |
| 4 |
I want to run a number of steps in each scenario |
| 5 |
And I want to avoid duplicating these steps for each scenario |
| 6 |
So that I write these steps only once (DRY principle). |
| 7 |
|
| 8 |
. SPECIFICATION: |
| 9 |
. * A feature can have an optional "Background" section |
| 10 |
. * The Background must be specified before any Scenario/ScenarioOutline |
| 11 |
. * The Background may occur at most once |
| 12 |
. * The Background steps are executed in each Scenario/ScenarioOutline |
| 13 |
. * The Background steps are executed before any Scenario steps |
| 14 |
. * If a Background step fails then the is marked as scenario failed |
| 15 |
. * If a Background fails in a scenario then other scenarios are still executed. |
| 16 |
. |
| 17 |
. RELATED: |
| 18 |
. * parser.background.sad_cases.feature |
| 19 |
. |
| 20 |
. NOTE: |
| 21 |
. Cucumber has a slightly different runtime behaviour. |
| 22 |
. When a background step fails the first scenario is marked as failed. |
| 23 |
. But the remaining scenarios are marked as skipped. |
| 24 |
. |
| 25 |
. This can lead to problems when you have sporadic background step failures. |
| 26 |
. For this reason, behave retries the background steps for each scenario. |
| 27 |
. But this may lead to an increased test duration if a systematic failure |
| 28 |
. occurs in the failing background step. |
| 29 |
. |
| 30 |
. SEE ALSO: |
| 31 |
. * https://github.com/cucumber/cucumber/blob/master/features/docs/gherkin/background.feature |
| 32 |
|
| 33 |
@setup |
| 34 |
Scenario: Feature Setup |
| 35 |
Given a new working directory |
| 36 |
And a file named "features/steps/background_steps.py" with: |
| 37 |
""" |
| 38 |
from behave import step |
| 39 |
|
| 40 |
@step('{word} background step passes') |
| 41 |
def step_background_step_passes(context, word): |
| 42 |
pass |
| 43 |
|
| 44 |
@step('{word} background step fails') |
| 45 |
def step_background_step_fails(context, word): |
| 46 |
assert False, "XFAIL: background step" |
| 47 |
|
| 48 |
@step('{word} background step fails sometimes') |
| 49 |
def step_background_step_fails_sometimes(context, word): |
| 50 |
should_fail = (context.scenarios_count % 2) == 0 |
| 51 |
if should_fail: |
| 52 |
step_background_step_fails(context, word) |
| 53 |
""" |
| 54 |
And a file named "features/steps/passing_steps.py" with: |
| 55 |
""" |
| 56 |
from behave import step |
| 57 |
|
| 58 |
@step('{word} step passes') |
| 59 |
def step_passes(context, word): |
| 60 |
pass |
| 61 |
|
| 62 |
@step('{word} step fails') |
| 63 |
def step_fails(context, word): |
| 64 |
assert False, "XFAIL" |
| 65 |
""" |
| 66 |
|
| 67 |
Scenario: Feature with a Background and Scenarios |
| 68 |
Given a file named "features/background_example1.feature" with: |
| 69 |
""" |
| 70 |
Feature: |
| 71 |
Background: |
| 72 |
Given a background step passes |
| 73 |
And another background step passes |
| 74 |
|
| 75 |
Scenario: S1 |
| 76 |
When a step passes |
| 77 |
|
| 78 |
Scenario: S2 |
| 79 |
Then a step passes |
| 80 |
And another step passes |
| 81 |
""" |
| 82 |
When I run "behave -f plain -T features/background_example1.feature" |
| 83 |
Then it should pass with: |
| 84 |
""" |
| 85 |
2 scenarios passed, 0 failed, 0 skipped |
| 86 |
7 steps passed, 0 failed, 0 skipped, 0 undefined |
| 87 |
""" |
| 88 |
And the command output should contain: |
| 89 |
""" |
| 90 |
Feature: |
| 91 |
Background: |
| 92 |
|
| 93 |
Scenario: S1 |
| 94 |
Given a background step passes ... passed |
| 95 |
And another background step passes ... passed |
| 96 |
When a step passes ... passed |
| 97 |
|
| 98 |
Scenario: S2 |
| 99 |
Given a background step passes ... passed |
| 100 |
And another background step passes ... passed |
| 101 |
Then a step passes ... passed |
| 102 |
And another step passes ... passed |
| 103 |
""" |
| 104 |
But note that "the Background steps are injected into each Scenario" |
| 105 |
And note that "the Background steps are executed before any Scenario steps" |
| 106 |
|
| 107 |
|
| 108 |
Scenario: Failing Background Step causes all Scenarios to fail |
| 109 |
Given a file named "features/background_fail_example.feature" with: |
| 110 |
""" |
| 111 |
Feature: |
| 112 |
|
| 113 |
Background: B1 |
| 114 |
Given a background step passes |
| 115 |
And a background step fails |
| 116 |
And another background step passes |
| 117 |
|
| 118 |
Scenario: S1 |
| 119 |
When a step passes |
| 120 |
|
| 121 |
Scenario: S2 |
| 122 |
Then a step passes |
| 123 |
And another step passes |
| 124 |
""" |
| 125 |
When I run "behave -f plain -T features/background_fail_example.feature" |
| 126 |
Then it should fail with: |
| 127 |
""" |
| 128 |
0 scenarios passed, 2 failed, 0 skipped |
| 129 |
2 steps passed, 2 failed, 5 skipped, 0 undefined |
| 130 |
""" |
| 131 |
And the command output should contain: |
| 132 |
""" |
| 133 |
Feature: |
| 134 |
Background: B1 |
| 135 |
|
| 136 |
Scenario: S1 |
| 137 |
Given a background step passes ... passed |
| 138 |
And a background step fails ... failed |
| 139 |
Assertion Failed: XFAIL: background step |
| 140 |
|
| 141 |
Scenario: S2 |
| 142 |
Given a background step passes ... passed |
| 143 |
And a background step fails ... failed |
| 144 |
Assertion Failed: XFAIL: background step |
| 145 |
""" |
| 146 |
And note that "the failing Background step causes all Scenarios to fail" |
| 147 |
|
| 148 |
|
| 149 |
Scenario: Failing Background Step does not prevent that other Scenarios are executed |
| 150 |
|
| 151 |
If a Background step fails sometimes |
| 152 |
it should be retried in the remaining Scenarios where it might pass. |
| 153 |
|
| 154 |
Given a file named "features/background_fails_sometimes_example.feature" with: |
| 155 |
""" |
| 156 |
Feature: |
| 157 |
|
| 158 |
Background: B2 |
| 159 |
Given a background step fails sometimes |
| 160 |
|
| 161 |
Scenario: S1 |
| 162 |
Given a step passes |
| 163 |
|
| 164 |
Scenario: S2 |
| 165 |
When another step passes |
| 166 |
|
| 167 |
Scenario: S3 |
| 168 |
Then another step passes |
| 169 |
""" |
| 170 |
And a file named "features/environment.py" with: |
| 171 |
""" |
| 172 |
scenarios_count = 0 |
| 173 |
|
| 174 |
def before_scenario(context, scenario): |
| 175 |
global scenarios_count |
| 176 |
context.scenarios_count = scenarios_count |
| 177 |
scenarios_count += 1 |
| 178 |
""" |
| 179 |
When I run "behave -f plain -T features/background_fails_sometimes_example.feature" |
| 180 |
Then it should fail with: |
| 181 |
""" |
| 182 |
1 scenario passed, 2 failed, 0 skipped |
| 183 |
2 steps passed, 2 failed, 2 skipped, 0 undefined |
| 184 |
""" |
| 185 |
And the command output should contain: |
| 186 |
""" |
| 187 |
Feature: |
| 188 |
Background: B2 |
| 189 |
|
| 190 |
Scenario: S1 |
| 191 |
Given a background step fails sometimes ... failed |
| 192 |
Assertion Failed: XFAIL: background step |
| 193 |
|
| 194 |
Scenario: S2 |
| 195 |
Given a background step fails sometimes ... passed |
| 196 |
When another step passes ... passed |
| 197 |
|
| 198 |
Scenario: S3 |
| 199 |
Given a background step fails sometimes ... failed |
| 200 |
Assertion Failed: XFAIL: background step |
| 201 |
""" |
| 202 |
|
| 203 |
|
| 204 |
Scenario: Feature with a Background and ScenarioOutlines |
| 205 |
Given a file named "features/background_outline_example.feature" with: |
| 206 |
""" |
| 207 |
Feature: |
| 208 |
Background: |
| 209 |
Given a background step passes |
| 210 |
And another background step passes |
| 211 |
|
| 212 |
Scenario Outline: SO1 |
| 213 |
When a step <outcome1> |
| 214 |
Then another step <outcome2> |
| 215 |
|
| 216 |
Examples: Alpha |
| 217 |
| outcome1 | outcome2 | |
| 218 |
| passes | passes | |
| 219 |
| passes | passes | |
| 220 |
|
| 221 |
Scenario Outline: SO2 |
| 222 |
Given <word1> step passes |
| 223 |
Then <word2> step passes |
| 224 |
|
| 225 |
Examples: |
| 226 |
| word1 | word2 | |
| 227 |
| a | a | |
| 228 |
| a | another | |
| 229 |
| another | a | |
| 230 |
""" |
| 231 |
When I run "behave -f plain -T features/background_outline_example.feature" |
| 232 |
Then it should pass with: |
| 233 |
""" |
| 234 |
5 scenarios passed, 0 failed, 0 skipped |
| 235 |
20 steps passed, 0 failed, 0 skipped, 0 undefined |
| 236 |
""" |
| 237 |
And the command output should contain: |
| 238 |
""" |
| 239 |
Feature: |
| 240 |
Background: |
| 241 |
|
| 242 |
Scenario Outline: SO1 -- @1.1 Alpha |
| 243 |
Given a background step passes ... passed |
| 244 |
And another background step passes ... passed |
| 245 |
When a step passes ... passed |
| 246 |
Then another step passes ... passed |
| 247 |
|
| 248 |
Scenario Outline: SO1 -- @1.2 Alpha |
| 249 |
Given a background step passes ... passed |
| 250 |
And another background step passes ... passed |
| 251 |
When a step passes ... passed |
| 252 |
Then another step passes ... passed |
| 253 |
|
| 254 |
Scenario Outline: SO2 -- @1.1 |
| 255 |
Given a background step passes ... passed |
| 256 |
And another background step passes ... passed |
| 257 |
Given a step passes ... passed |
| 258 |
Then a step passes ... passed |
| 259 |
|
| 260 |
Scenario Outline: SO2 -- @1.2 |
| 261 |
Given a background step passes ... passed |
| 262 |
And another background step passes ... passed |
| 263 |
Given a step passes ... passed |
| 264 |
Then another step passes ... passed |
| 265 |
|
| 266 |
Scenario Outline: SO2 -- @1.3 |
| 267 |
Given a background step passes ... passed |
| 268 |
And another background step passes ... passed |
| 269 |
Given another step passes ... passed |
| 270 |
Then a step passes ... passed |
| 271 |
""" |
| 272 |
But note that "the Background steps are injected into each ScenarioOutline" |
| 273 |
And note that "the Background steps are executed before any ScenarioOutline steps" |
| 274 |
|
| 275 |
|
| 276 |
Scenario: Failing Background Step causes all ScenarioOutlines to fail |
| 277 |
Given a file named "features/background_fail_outline_example.feature" with: |
| 278 |
""" |
| 279 |
Feature: |
| 280 |
Background: |
| 281 |
Given a background step passes |
| 282 |
And a background step fails |
| 283 |
But another background step passes |
| 284 |
|
| 285 |
Scenario Outline: SO1 |
| 286 |
When a step <outcome1> |
| 287 |
Then another step <outcome2> |
| 288 |
|
| 289 |
Examples: Alpha |
| 290 |
| outcome1 | outcome2 | |
| 291 |
| passes | passes | |
| 292 |
| passes | fails | |
| 293 |
| fails | passes | |
| 294 |
| fails | fails | |
| 295 |
|
| 296 |
Scenario Outline: SO2 |
| 297 |
When <word1> step passes |
| 298 |
|
| 299 |
Examples: Beta |
| 300 |
| word1 | |
| 301 |
| a | |
| 302 |
| another | |
| 303 |
""" |
| 304 |
When I run "behave -f plain -T features/background_fail_outline_example.feature" |
| 305 |
Then it should fail with: |
| 306 |
""" |
| 307 |
0 scenarios passed, 6 failed, 0 skipped |
| 308 |
6 steps passed, 6 failed, 16 skipped, 0 undefined |
| 309 |
""" |
| 310 |
And the command output should contain: |
| 311 |
""" |
| 312 |
Feature: |
| 313 |
Background: |
| 314 |
|
| 315 |
Scenario Outline: SO1 -- @1.1 Alpha |
| 316 |
Given a background step passes ... passed |
| 317 |
And a background step fails ... failed |
| 318 |
Assertion Failed: XFAIL: background step |
| 319 |
|
| 320 |
Scenario Outline: SO1 -- @1.2 Alpha |
| 321 |
Given a background step passes ... passed |
| 322 |
And a background step fails ... failed |
| 323 |
Assertion Failed: XFAIL: background step |
| 324 |
|
| 325 |
Scenario Outline: SO1 -- @1.3 Alpha |
| 326 |
Given a background step passes ... passed |
| 327 |
And a background step fails ... failed |
| 328 |
Assertion Failed: XFAIL: background step |
| 329 |
|
| 330 |
Scenario Outline: SO1 -- @1.4 Alpha |
| 331 |
Given a background step passes ... passed |
| 332 |
And a background step fails ... failed |
| 333 |
Assertion Failed: XFAIL: background step |
| 334 |
|
| 335 |
Scenario Outline: SO2 -- @1.1 Beta |
| 336 |
Given a background step passes ... passed |
| 337 |
And a background step fails ... failed |
| 338 |
Assertion Failed: XFAIL: background step |
| 339 |
|
| 340 |
Scenario Outline: SO2 -- @1.2 Beta |
| 341 |
Given a background step passes ... passed |
| 342 |
And a background step fails ... failed |
| 343 |
Assertion Failed: XFAIL: background step |
| 344 |
""" |
| 345 |
But note that "the failing Background step causes each ScenarioOutline to be marked as skipped" |
| 346 |
|
| 347 |
|
| 348 |
Scenario: Feature with Background after first Scenario should fail (SAD CASE) |
| 349 |
Given a file named "features/background_sad_example1.feature" with: |
| 350 |
""" |
| 351 |
Feature: |
| 352 |
Scenario: S1 |
| 353 |
When a step passes |
| 354 |
|
| 355 |
Background: B1 |
| 356 |
Given a background step passes |
| 357 |
|
| 358 |
Scenario: S2 |
| 359 |
Then a step passes |
| 360 |
And another step passes |
| 361 |
""" |
| 362 |
When I run "behave -f plain -T features/background_sad_example1.feature" |
| 363 |
Then it should fail with: |
| 364 |
""" |
| 365 |
Parser failure in state steps, at line 5: "Background: B1" |
| 366 |
REASON: Background may not occur after Scenario/ScenarioOutline. |
| 367 |
""" |
| 368 |
|
| 369 |
|
| 370 |
Scenario: Feature with two Backgrounds should fail (SAD CASE) |
| 371 |
Given a file named "features/background_sad_example2.feature" with: |
| 372 |
""" |
| 373 |
Feature: |
| 374 |
Background: B1 |
| 375 |
Given a background step passes |
| 376 |
|
| 377 |
Background: B2 (XFAIL) |
| 378 |
Given another background step passes |
| 379 |
|
| 380 |
Scenario: S1 |
| 381 |
When a step passes |
| 382 |
|
| 383 |
Scenario: S2 |
| 384 |
Then a step passes |
| 385 |
And another step passes |
| 386 |
""" |
| 387 |
When I run "behave -f plain -T features/background_sad_example2.feature" |
| 388 |
Then it should fail with: |
| 389 |
""" |
| 390 |
Parser failure in state steps, at line 5: "Background: B2 (XFAIL)" |
| 391 |
REASON: Background should not be used here. |
| 392 |
""" |