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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
|
import textwrap
def test_steps(pytester):
pytester.makefile(
".feature",
steps=textwrap.dedent(
"""\
Feature: Steps are executed one by one
Steps are executed one by one. Given and When sections
are not mandatory in some cases.
Scenario: Executed step by step
Given I have a foo fixture with value "foo"
And there is a list
When I append 1 to the list
And I append 2 to the list
And I append 3 to the list
Then foo should have value "foo"
But the list should be [1, 2, 3]
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import given, when, then, scenario
@scenario("steps.feature", "Executed step by step")
def test_steps():
pass
@given('I have a foo fixture with value "foo"', target_fixture="foo")
def _():
return "foo"
@given("there is a list", target_fixture="results")
def _():
return []
@when("I append 1 to the list")
def _(results):
results.append(1)
@when("I append 2 to the list")
def _(results):
results.append(2)
@when("I append 3 to the list")
def _(results):
results.append(3)
@then('foo should have value "foo"')
def _(foo):
assert foo == "foo"
@then("the list should be [1, 2, 3]")
def _(results):
assert results == [1, 2, 3]
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=1, failed=0)
def test_step_function_can_be_decorated_multiple_times(pytester):
pytester.makefile(
".feature",
steps=textwrap.dedent(
"""\
Feature: Steps decoration
Scenario: Step function can be decorated multiple times
Given there is a foo with value 42
And there is a second foo with value 43
When I do nothing
And I do nothing again
Then I make no mistakes
And I make no mistakes again
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import given, when, then, scenario, parsers
@scenario("steps.feature", "Step function can be decorated multiple times")
def test_steps():
pass
@given(parsers.parse("there is a foo with value {value}"), target_fixture="foo")
@given(parsers.parse("there is a second foo with value {value}"), target_fixture="second_foo")
def _(value):
return value
@when("I do nothing")
@when("I do nothing again")
def _():
pass
@then("I make no mistakes")
@then("I make no mistakes again")
def _():
assert True
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=1, failed=0)
def test_all_steps_can_provide_fixtures(pytester):
"""Test that given/when/then can all provide fixtures."""
pytester.makefile(
".feature",
steps=textwrap.dedent(
"""\
Feature: Step fixture
Scenario: Given steps can provide fixture
Given Foo is "bar"
Then foo should be "bar"
Scenario: When steps can provide fixture
When Foo is "baz"
Then foo should be "baz"
Scenario: Then steps can provide fixture
Then foo is "qux"
And foo should be "qux"
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import given, when, then, parsers, scenarios
scenarios("steps.feature")
@given(parsers.parse('Foo is "{value}"'), target_fixture="foo")
def _(value):
return value
@when(parsers.parse('Foo is "{value}"'), target_fixture="foo")
def _(value):
return value
@then(parsers.parse('Foo is "{value}"'), target_fixture="foo")
def _(value):
return value
@then(parsers.parse('foo should be "{value}"'))
def _(foo, value):
assert foo == value
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=3, failed=0)
def test_when_first(pytester):
pytester.makefile(
".feature",
steps=textwrap.dedent(
"""\
Feature: Steps are executed one by one
Steps are executed one by one. Given and When sections
are not mandatory in some cases.
Scenario: When step can be the first
When I do nothing
Then I make no mistakes
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import when, then, scenario
@scenario("steps.feature", "When step can be the first")
def test_steps():
pass
@when("I do nothing")
def _():
pass
@then("I make no mistakes")
def _():
assert True
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=1, failed=0)
def test_then_after_given(pytester):
pytester.makefile(
".feature",
steps=textwrap.dedent(
"""\
Feature: Steps are executed one by one
Steps are executed one by one. Given and When sections
are not mandatory in some cases.
Scenario: Then step can follow Given step
Given I have a foo fixture with value "foo"
Then foo should have value "foo"
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import given, then, scenario
@scenario("steps.feature", "Then step can follow Given step")
def test_steps():
pass
@given('I have a foo fixture with value "foo"', target_fixture="foo")
def _():
return "foo"
@then('foo should have value "foo"')
def _(foo):
assert foo == "foo"
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=1, failed=0)
def test_conftest(pytester):
pytester.makefile(
".feature",
steps=textwrap.dedent(
"""\
Feature: Steps are executed one by one
Steps are executed one by one. Given and When sections
are not mandatory in some cases.
Scenario: All steps are declared in the conftest
Given I have a bar
Then bar should have value "bar"
"""
),
)
pytester.makeconftest(
textwrap.dedent(
"""\
from pytest_bdd import given, then
@given("I have a bar", target_fixture="bar")
def _():
return "bar"
@then('bar should have value "bar"')
def _(bar):
assert bar == "bar"
"""
)
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import scenario
@scenario("steps.feature", "All steps are declared in the conftest")
def test_steps():
pass
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=1, failed=0)
def test_multiple_given(pytester):
"""Using the same given fixture raises an error."""
pytester.makefile(
".feature",
steps=textwrap.dedent(
"""\
Feature: Steps are executed one by one
Scenario: Using the same given twice
Given foo is "foo"
And foo is "bar"
Then foo should be "bar"
"""
),
)
pytester.makepyfile(
textwrap.dedent(
"""\
from pytest_bdd import parsers, given, then, scenario
@given(parsers.parse("foo is {value}"), target_fixture="foo")
def _(value):
return value
@then(parsers.parse("foo should be {value}"))
def _(foo, value):
assert foo == value
@scenario("steps.feature", "Using the same given twice")
def test_given_twice():
pass
"""
)
)
result = pytester.runpytest()
result.assert_outcomes(passed=1, failed=0)
def test_step_hooks(pytester):
"""When step fails."""
pytester.makefile(
".feature",
test="""
Scenario: When step has hook on failure
Given I have a bar
When it fails
Scenario: When step's dependency a has failure
Given I have a bar
When it's dependency fails
Scenario: When step is not found
Given not found
Scenario: When step validation error happens
Given foo
And foo
""",
)
pytester.makepyfile(
"""
import pytest
from pytest_bdd import given, when, scenario
@given('I have a bar')
def _():
return 'bar'
@when('it fails')
def _():
raise Exception('when fails')
@given('I have a bar')
def _():
return 'bar'
@pytest.fixture
def dependency():
raise Exception('dependency fails')
@when("it's dependency fails")
def _(dependency):
pass
@scenario('test.feature', "When step's dependency a has failure")
def test_when_dependency_fails():
pass
@scenario('test.feature', 'When step has hook on failure')
def test_when_fails():
pass
@scenario('test.feature', 'When step is not found')
def test_when_not_found():
pass
@when('foo')
def _():
return 'foo'
@scenario('test.feature', 'When step validation error happens')
def test_when_step_validation_error():
pass
"""
)
reprec = pytester.inline_run("-k test_when_fails")
reprec.assertoutcome(failed=1)
calls = reprec.getcalls("pytest_bdd_before_scenario")
assert calls[0].request
calls = reprec.getcalls("pytest_bdd_after_scenario")
assert calls[0].request
calls = reprec.getcalls("pytest_bdd_before_step")
assert calls[0].request
calls = reprec.getcalls("pytest_bdd_before_step_call")
assert calls[0].request
calls = reprec.getcalls("pytest_bdd_after_step")
assert calls[0].request
calls = reprec.getcalls("pytest_bdd_step_error")
assert calls[0].request
reprec = pytester.inline_run("-k test_when_not_found")
reprec.assertoutcome(failed=1)
calls = reprec.getcalls("pytest_bdd_step_func_lookup_error")
assert calls[0].request
reprec = pytester.inline_run("-k test_when_step_validation_error")
reprec.assertoutcome(failed=1)
reprec = pytester.inline_run("-k test_when_dependency_fails", "-vv")
reprec.assertoutcome(failed=1)
calls = reprec.getcalls("pytest_bdd_before_step")
assert len(calls) == 2
calls = reprec.getcalls("pytest_bdd_before_step_call")
assert len(calls) == 1
calls = reprec.getcalls("pytest_bdd_step_error")
assert calls[0].request
def test_step_trace(pytester):
"""Test step trace."""
pytester.makeini(
"""
[pytest]
console_output_style=classic
"""
)
pytester.makefile(
".feature",
test="""
Scenario: When step has failure
Given I have a bar
When it fails
Scenario: When step is not found
Given not found
Scenario: When step validation error happens
Given foo
And foo
""",
)
pytester.makepyfile(
"""
import pytest
from pytest_bdd import given, when, scenario
@given('I have a bar')
def _():
return 'bar'
@when('it fails')
def _():
raise Exception('when fails')
@scenario('test.feature', 'When step has failure')
def test_when_fails_inline():
pass
@scenario('test.feature', 'When step has failure')
def test_when_fails_decorated():
pass
@scenario('test.feature', 'When step is not found')
def test_when_not_found():
pass
@when('foo')
def _():
return 'foo'
@scenario('test.feature', 'When step validation error happens')
def test_when_step_validation_error():
pass
"""
)
result = pytester.runpytest("-k test_when_fails_inline", "-vv")
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(["*test_when_fails_inline*FAILED"])
assert "INTERNALERROR" not in result.stdout.str()
result = pytester.runpytest("-k test_when_fails_decorated", "-vv")
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(["*test_when_fails_decorated*FAILED"])
assert "INTERNALERROR" not in result.stdout.str()
result = pytester.runpytest("-k test_when_not_found", "-vv")
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(["*test_when_not_found*FAILED"])
assert "INTERNALERROR" not in result.stdout.str()
result = pytester.runpytest("-k test_when_step_validation_error", "-vv")
result.assert_outcomes(failed=1)
result.stdout.fnmatch_lines(["*test_when_step_validation_error*FAILED"])
assert "INTERNALERROR" not in result.stdout.str()
def test_steps_with_yield(pytester):
"""Test that steps definition containing a yield statement work the same way as
pytest fixture do, that is the code after the yield is executed during teardown."""
pytester.makefile(
".feature",
a="""\
Feature: A feature
Scenario: A scenario
When I setup stuff
Then stuff should be 42
""",
)
pytester.makepyfile(
textwrap.dedent(
"""\
import pytest
from pytest_bdd import given, when, then, scenarios
scenarios("a.feature")
@when("I setup stuff", target_fixture="stuff")
def _():
print("Setting up...")
yield 42
print("Tearing down...")
@then("stuff should be 42")
def _(stuff):
assert stuff == 42
print("Asserted stuff is 42")
"""
)
)
result = pytester.runpytest("-s")
result.assert_outcomes(passed=1)
result.stdout.fnmatch_lines(
[
"*Setting up...*",
"*Asserted stuff is 42*",
"*Tearing down...*",
]
)
|