|
1 | 1 | """Step arguments tests."""
|
2 | 2 |
|
3 |
| -import functools |
4 |
| -from pytest_bdd import given, parsers, scenario, then, when |
| 3 | +import textwrap |
5 | 4 |
|
6 |
| -import pytest |
7 | 5 |
|
8 |
| -from pytest_bdd import exceptions |
| 6 | +def test_every_steps_takes_param_with_the_same_name(testdir): |
| 7 | + testdir.makefile( |
| 8 | + ".feature", |
| 9 | + arguments=textwrap.dedent( |
| 10 | + """\ |
| 11 | + Feature: Step arguments |
| 12 | + Scenario: Every step takes a parameter with the same name |
| 13 | + Given I have 1 Euro |
| 14 | + When I pay 2 Euro |
| 15 | + And I pay 1 Euro |
| 16 | + Then I should have 0 Euro |
| 17 | + And I should have 999999 Euro # In my dream... |
| 18 | +
|
| 19 | + """ |
| 20 | + ), |
| 21 | + ) |
| 22 | + |
| 23 | + testdir.makepyfile( |
| 24 | + textwrap.dedent( |
| 25 | + """\ |
| 26 | + import pytest |
| 27 | + from pytest_bdd import parsers, given, when, then, scenario |
| 28 | +
|
| 29 | + @scenario("arguments.feature", "Every step takes a parameter with the same name") |
| 30 | + def test_arguments(): |
| 31 | + pass |
| 32 | +
|
| 33 | + @pytest.fixture |
| 34 | + def values(): |
| 35 | + return [1, 2, 1, 0, 999999] |
| 36 | +
|
| 37 | +
|
| 38 | + @given(parsers.cfparse("I have {euro:d} Euro")) |
| 39 | + def i_have(euro, values): |
| 40 | + assert euro == values.pop(0) |
| 41 | +
|
| 42 | +
|
| 43 | + @when(parsers.cfparse("I pay {euro:d} Euro")) |
| 44 | + def i_pay(euro, values, request): |
| 45 | + assert euro == values.pop(0) |
| 46 | +
|
| 47 | +
|
| 48 | + @then(parsers.cfparse("I should have {euro:d} Euro")) |
| 49 | + def i_should_have(euro, values): |
| 50 | + assert euro == values.pop(0) |
| 51 | +
|
| 52 | + """ |
| 53 | + ) |
| 54 | + ) |
| 55 | + result = testdir.runpytest() |
| 56 | + result.assert_outcomes(passed=1) |
| 57 | + |
| 58 | + |
| 59 | +def test_argument_in_when_step_1(testdir): |
| 60 | + testdir.makefile( |
| 61 | + ".feature", |
| 62 | + arguments=textwrap.dedent( |
| 63 | + """\ |
| 64 | + Feature: Step arguments |
| 65 | + Scenario: Argument in when, step 1 |
| 66 | + Given I have an argument 1 |
| 67 | + When I get argument 5 |
| 68 | + Then My argument should be 5 |
| 69 | + """ |
| 70 | + ), |
| 71 | + ) |
| 72 | + |
| 73 | + testdir.makepyfile( |
| 74 | + textwrap.dedent( |
| 75 | + """\ |
| 76 | + from pytest_bdd import parsers, given, when, then, scenario |
| 77 | +
|
| 78 | + @scenario("arguments.feature", "Argument in when, step 1") |
| 79 | + def test_arguments(): |
| 80 | + pass |
| 81 | +
|
| 82 | + @given(parsers.cfparse("I have an argument {arg:Number}", extra_types=dict(Number=int))) |
| 83 | + def argument(arg): |
| 84 | + return dict(arg=arg) |
| 85 | +
|
| 86 | +
|
| 87 | + @when(parsers.cfparse("I get argument {arg:d}")) |
| 88 | + def get_argument(argument, arg): |
| 89 | + argument["arg"] = arg |
| 90 | +
|
| 91 | +
|
| 92 | + @then(parsers.cfparse("My argument should be {arg:d}")) |
| 93 | + def assert_that_my_argument_is_arg(argument, arg): |
| 94 | + assert argument["arg"] == arg |
| 95 | +
|
| 96 | + """ |
| 97 | + ) |
| 98 | + ) |
| 99 | + result = testdir.runpytest() |
| 100 | + result.assert_outcomes(passed=1) |
| 101 | + |
| 102 | + |
| 103 | +def test_argument_in_when_step_2(testdir): |
| 104 | + testdir.makefile( |
| 105 | + ".feature", |
| 106 | + arguments=textwrap.dedent( |
| 107 | + """\ |
| 108 | + Feature: Step arguments |
| 109 | + Scenario: Argument in when, step 2 |
| 110 | + Given I have an argument 1 |
| 111 | + When I get argument 10 |
| 112 | + Then My argument should be 10 |
| 113 | + """ |
| 114 | + ), |
| 115 | + ) |
9 | 116 |
|
10 |
| -scenario_when = functools.partial(scenario, "../when_arguments.feature") |
| 117 | + testdir.makepyfile( |
| 118 | + textwrap.dedent( |
| 119 | + """\ |
| 120 | + from pytest_bdd import parsers, given, when, then, scenario |
11 | 121 |
|
12 |
| -scenario_args = functools.partial(scenario, "../args_steps.feature") |
| 122 | + @scenario("arguments.feature", "Argument in when, step 2") |
| 123 | + def test_arguments(): |
| 124 | + pass |
13 | 125 |
|
| 126 | + @given(parsers.cfparse("I have an argument {arg:Number}", extra_types=dict(Number=int))) |
| 127 | + def argument(arg): |
| 128 | + return dict(arg=arg) |
14 | 129 |
|
15 |
| -@scenario_args("Every step takes a parameter with the same name") |
16 |
| -def test_steps(): |
17 |
| - pass |
18 | 130 |
|
| 131 | + @when(parsers.cfparse("I get argument {arg:d}")) |
| 132 | + def get_argument(argument, arg): |
| 133 | + argument["arg"] = arg |
19 | 134 |
|
20 |
| -@scenario_when("Argument in when, step 1") |
21 |
| -def test_argument_in_when_step_1(): |
22 |
| - pass |
23 | 135 |
|
| 136 | + @then(parsers.cfparse("My argument should be {arg:d}")) |
| 137 | + def assert_that_my_argument_is_arg(argument, arg): |
| 138 | + assert argument["arg"] == arg |
24 | 139 |
|
25 |
| -@scenario_when("Argument in when, step 2") |
26 |
| -def test_argument_in_when_step_2(): |
27 |
| - pass |
| 140 | + """ |
| 141 | + ) |
| 142 | + ) |
| 143 | + result = testdir.runpytest() |
| 144 | + result.assert_outcomes(passed=1) |
28 | 145 |
|
29 | 146 |
|
30 |
| -def test_multiple_given(request): |
| 147 | +def test_multiple_given(testdir): |
31 | 148 | """Using the same given fixture raises an error."""
|
32 |
| - |
33 |
| - @scenario_args("Using the same given fixture raises an error") |
34 |
| - def test(): |
35 |
| - pass |
36 |
| - |
37 |
| - with pytest.raises(exceptions.GivenAlreadyUsed): |
38 |
| - test(request) |
39 |
| - |
40 |
| - |
41 |
| -@given(parsers.cfparse("I have {euro:d} Euro")) |
42 |
| -def i_have(euro, values): |
43 |
| - assert euro == values.pop(0) |
44 |
| - |
45 |
| - |
46 |
| -@when(parsers.cfparse("I pay {euro:d} Euro")) |
47 |
| -def i_pay(euro, values, request): |
48 |
| - assert euro == values.pop(0) |
49 |
| - |
50 |
| - |
51 |
| -@then(parsers.cfparse("I should have {euro:d} Euro")) |
52 |
| -def i_should_have(euro, values): |
53 |
| - assert euro == values.pop(0) |
54 |
| - |
55 |
| - |
56 |
| -@given(parsers.cfparse("I have an argument {arg:Number}", extra_types=dict(Number=int))) |
57 |
| -def argument(arg): |
58 |
| - """I have an argument.""" |
59 |
| - return dict(arg=arg) |
60 |
| - |
61 |
| - |
62 |
| -@when(parsers.cfparse("I get argument {arg:d}")) |
63 |
| -def get_argument(argument, arg): |
64 |
| - """Getting argument.""" |
65 |
| - argument["arg"] = arg |
66 |
| - |
67 |
| - |
68 |
| -@then(parsers.cfparse("My argument should be {arg:d}")) |
69 |
| -def assert_that_my_argument_is_arg(argument, arg): |
70 |
| - """Assert that arg from when equals arg.""" |
71 |
| - assert argument["arg"] == arg |
| 149 | + testdir.makefile( |
| 150 | + ".feature", |
| 151 | + arguments=textwrap.dedent( |
| 152 | + """\ |
| 153 | + Feature: Step arguments |
| 154 | + Scenario: Using the same given fixture raises an error |
| 155 | + Given I have 1 Euro |
| 156 | + And I have 2 Euro |
| 157 | + """ |
| 158 | + ), |
| 159 | + ) |
| 160 | + testdir.makepyfile( |
| 161 | + textwrap.dedent( |
| 162 | + """\ |
| 163 | + from pytest_bdd import parsers, given, scenario |
| 164 | +
|
| 165 | + @scenario("arguments.feature", "Using the same given fixture raises an error") |
| 166 | + def test_arguments(): |
| 167 | + pass |
| 168 | +
|
| 169 | + @given(parsers.cfparse("I have {euro:d} Euro")) |
| 170 | + def i_have(euro): |
| 171 | + return euro |
| 172 | +
|
| 173 | + """ |
| 174 | + ) |
| 175 | + ) |
| 176 | + result = testdir.runpytest() |
| 177 | + result.assert_outcomes(failed=1) |
| 178 | + result.stdout.fnmatch_lines( |
| 179 | + '*GivenAlreadyUsed: Fixture "i_have" that implements this "I have 2 Euro" given step has been already used.*' |
| 180 | + ) |
0 commit comments