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