| 1 |
@issue |
| 2 |
Feature: Issue #73: the current_matcher is not predictable |
| 3 |
|
| 4 |
. behave provides 2 matchers: ParseMatcher (parse) and RegexpMatcher (re). |
| 5 |
. The ParseMatcher is used per default when the test runner starts. |
| 6 |
. |
| 7 |
. A step definition file may change the matcher several times |
| 8 |
. by calling `use_step_matcher("re")` or `use_step_matcher("parse")`. |
| 9 |
. In order to make the writing of step definitions more predictable, |
| 10 |
. the matcher should be reset to the default matcher |
| 11 |
. before loading each step definition. |
| 12 |
. |
| 13 |
. A project can define its own default matcher by calling the |
| 14 |
. `use_step_matcher(...)` in the "environment.py" hook. |
| 15 |
. This matcher will be used as default before a step definition is loaded. |
| 16 |
|
| 17 |
|
| 18 |
Scenario: Verify that ParseMatcher is the default matcher |
| 19 |
Given a new working directory |
| 20 |
And a file named "features/steps/parse_steps.py" with: |
| 21 |
""" |
| 22 |
from behave import step |
| 23 |
|
| 24 |
@step(u'a step {outcome:w}') |
| 25 |
def step_passes(context, outcome): |
| 26 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 27 |
""" |
| 28 |
And a file named "features/passing.feature" with: |
| 29 |
""" |
| 30 |
Feature: |
| 31 |
Scenario: |
| 32 |
Given a step passes |
| 33 |
When a step passes |
| 34 |
Then a step passes |
| 35 |
""" |
| 36 |
When I run "behave -f plain features/passing.feature" |
| 37 |
Then it should pass with |
| 38 |
""" |
| 39 |
1 scenario passed, 0 failed, 0 skipped |
| 40 |
3 steps passed, 0 failed, 0 skipped, 0 undefined |
| 41 |
""" |
| 42 |
|
| 43 |
|
| 44 |
Scenario: Use only RegexMatcher in Step Definitions |
| 45 |
Given a new working directory |
| 46 |
And a file named "features/environment.py" with: |
| 47 |
""" |
| 48 |
from behave import use_step_matcher |
| 49 |
use_step_matcher("re") |
| 50 |
""" |
| 51 |
And a file named "features/steps/regexp_steps.py" with: |
| 52 |
""" |
| 53 |
from behave import step |
| 54 |
|
| 55 |
@step(u'a step (?P<outcome>passes|fails)') |
| 56 |
def step_passes(context, outcome): |
| 57 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 58 |
""" |
| 59 |
And a file named "features/passing.feature" with: |
| 60 |
""" |
| 61 |
Feature: |
| 62 |
Scenario: |
| 63 |
Given a step passes |
| 64 |
When a step passes |
| 65 |
Then a step passes |
| 66 |
""" |
| 67 |
When I run "behave -f plain features/passing.feature" |
| 68 |
Then it should pass with |
| 69 |
""" |
| 70 |
1 scenario passed, 0 failed, 0 skipped |
| 71 |
3 steps passed, 0 failed, 0 skipped, 0 undefined |
| 72 |
""" |
| 73 |
|
| 74 |
|
| 75 |
Scenario: Use ParseMatcher and RegexMatcher in Step Definitions (default=re) |
| 76 |
Given a new working directory |
| 77 |
And a file named "features/environment.py" with: |
| 78 |
""" |
| 79 |
from behave import use_step_matcher |
| 80 |
use_step_matcher("re") |
| 81 |
""" |
| 82 |
And a file named "features/steps/eparse_steps.py" with: |
| 83 |
""" |
| 84 |
from behave import step, use_step_matcher |
| 85 |
use_step_matcher("parse") |
| 86 |
|
| 87 |
@step(u'an extraordinary step {outcome:w}') |
| 88 |
def step_passes(context, outcome): |
| 89 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 90 |
""" |
| 91 |
And a file named "features/steps/regexp_steps.py" with: |
| 92 |
""" |
| 93 |
from behave import step |
| 94 |
|
| 95 |
@step(u'a step (?P<outcome>passes|fails)') |
| 96 |
def step_passes(context, outcome): |
| 97 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 98 |
""" |
| 99 |
And a file named "features/steps/xparse_steps.py" with: |
| 100 |
""" |
| 101 |
from behave import step, use_step_matcher |
| 102 |
use_step_matcher("parse") |
| 103 |
|
| 104 |
@step(u'another step {outcome:w}') |
| 105 |
def step_passes(context, outcome): |
| 106 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 107 |
""" |
| 108 |
And a file named "features/passing3.feature" with: |
| 109 |
""" |
| 110 |
Feature: |
| 111 |
Scenario: |
| 112 |
Given a step passes |
| 113 |
When another step passes |
| 114 |
Then an extraordinary step passes |
| 115 |
""" |
| 116 |
When I run "behave -f plain features/passing3.feature" |
| 117 |
Then it should pass with |
| 118 |
""" |
| 119 |
1 scenario passed, 0 failed, 0 skipped |
| 120 |
3 steps passed, 0 failed, 0 skipped, 0 undefined |
| 121 |
""" |
| 122 |
|
| 123 |
|
| 124 |
Scenario: Mix ParseMatcher and RegexMatcher in Step Definitions (default=re) |
| 125 |
Given a new working directory |
| 126 |
And a file named "features/environment.py" with: |
| 127 |
""" |
| 128 |
from behave import use_step_matcher |
| 129 |
use_step_matcher("re") |
| 130 |
""" |
| 131 |
And a file named "features/steps/given_steps.py" with: |
| 132 |
""" |
| 133 |
from behave import step, use_step_matcher |
| 134 |
|
| 135 |
use_step_matcher("parse") |
| 136 |
@given(u'a step {outcome:w}') |
| 137 |
def given_step_passes(context, outcome): |
| 138 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 139 |
|
| 140 |
use_step_matcher("re") |
| 141 |
@given(u'another step (?P<outcome>passes|fails)') |
| 142 |
def given_another_step_passes(context, outcome): |
| 143 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 144 |
|
| 145 |
use_step_matcher("parse") |
| 146 |
@given(u'an extraordinary step {outcome:w}') |
| 147 |
def given_extraordinary_step_passes(context, outcome): |
| 148 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 149 |
""" |
| 150 |
And a file named "features/steps/when_steps.py" with: |
| 151 |
""" |
| 152 |
from behave import step, use_step_matcher |
| 153 |
|
| 154 |
@when(u'a step (?P<outcome>passes|fails)') |
| 155 |
def when_step_passes(context, outcome): |
| 156 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 157 |
|
| 158 |
use_step_matcher("parse") |
| 159 |
@when(u'another step {outcome:w}') |
| 160 |
def when_another_step_passes(context, outcome): |
| 161 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 162 |
|
| 163 |
use_step_matcher("re") |
| 164 |
@when(u'an extraordinary step (?P<outcome>passes|fails)') |
| 165 |
def when_extraordinary_step_passes(context, outcome): |
| 166 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 167 |
""" |
| 168 |
And a file named "features/steps/then_steps.py" with: |
| 169 |
""" |
| 170 |
from behave import step, use_step_matcher |
| 171 |
|
| 172 |
use_step_matcher("parse") |
| 173 |
@then(u'a step {outcome:w}') |
| 174 |
def then_step_passes(context, outcome): |
| 175 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 176 |
|
| 177 |
use_step_matcher("re") |
| 178 |
@then(u'another step (?P<outcome>passes|fails)') |
| 179 |
def then_another_step_passes(context, outcome): |
| 180 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 181 |
|
| 182 |
use_step_matcher("parse") |
| 183 |
@then(u'an extraordinary step {outcome:w}') |
| 184 |
def then_extraordinary_step_passes(context, outcome): |
| 185 |
assert outcome == "passes", "FAIL: outcome={0}".format(outcome) |
| 186 |
""" |
| 187 |
And a file named "features/passing3.feature" with: |
| 188 |
""" |
| 189 |
Feature: |
| 190 |
Scenario: 1 |
| 191 |
Given a step passes |
| 192 |
When another step passes |
| 193 |
Then an extraordinary step passes |
| 194 |
|
| 195 |
Scenario: 2 |
| 196 |
Given another step passes |
| 197 |
When an extraordinary step passes |
| 198 |
Then a step passes |
| 199 |
|
| 200 |
Scenario: 3 |
| 201 |
Given an extraordinary step passes |
| 202 |
When a step passes |
| 203 |
Then another step passes |
| 204 |
""" |
| 205 |
When I run "behave -c -f pretty --no-timings features/passing3.feature" |
| 206 |
Then it should pass with: |
| 207 |
""" |
| 208 |
3 scenarios passed, 0 failed, 0 skipped |
| 209 |
9 steps passed, 0 failed, 0 skipped, 0 undefined |
| 210 |
""" |
| 211 |
And the command output should contain: |
| 212 |
""" |
| 213 |
Feature: # features/passing3.feature:1 |
| 214 |
Scenario: 1 # features/passing3.feature:2 |
| 215 |
Given a step passes # features/steps/given_steps.py:4 |
| 216 |
When another step passes # features/steps/when_steps.py:8 |
| 217 |
Then an extraordinary step passes # features/steps/then_steps.py:14 |
| 218 |
|
| 219 |
Scenario: 2 # features/passing3.feature:7 |
| 220 |
Given another step passes # features/steps/given_steps.py:9 |
| 221 |
When an extraordinary step passes # features/steps/when_steps.py:13 |
| 222 |
Then a step passes # features/steps/then_steps.py:4 |
| 223 |
|
| 224 |
Scenario: 3 # features/passing3.feature:12 |
| 225 |
Given an extraordinary step passes # features/steps/given_steps.py:14 |
| 226 |
When a step passes # features/steps/when_steps.py:3 |
| 227 |
Then another step passes # features/steps/then_steps.py:9 |
| 228 |
""" |