| 1 |
Feature: User-specific Configuration Data (userdata) |
| 2 |
|
| 3 |
As a test writer |
| 4 |
I want to provide my own configuration data |
| 5 |
So that the test suite and/or the environment can be adapted to its needs. |
| 6 |
|
| 7 |
. MECHANISM: |
| 8 |
. * Use -D name=value (--define) option to specify user data on command-line. |
| 9 |
. * Specify user data in section "behave.userdata" of "behave.ini" |
| 10 |
. * Load/setup user data in before_all() hook (advanced cases) |
| 11 |
. |
| 12 |
. USING USER DATA: |
| 13 |
. * context.config.userdata (as dict) |
| 14 |
. |
| 15 |
. SUPPORTED DATA TYPES (from "behave.ini" and command-line): |
| 16 |
. * string |
| 17 |
. * bool-like (= "true", if definition has no value) |
| 18 |
|
| 19 |
@setup |
| 20 |
Scenario: Feature Setup |
| 21 |
Given a new working directory |
| 22 |
And a file named "features/steps/pass_steps.py" with: |
| 23 |
""" |
| 24 |
from behave import step |
| 25 |
|
| 26 |
@step('{word:w} step passes') |
| 27 |
def step_passes(context, word): |
| 28 |
pass |
| 29 |
""" |
| 30 |
And a file named "features/steps/userdata_steps.py" with: |
| 31 |
""" |
| 32 |
from behave import step |
| 33 |
from hamcrest import assert_that, equal_to |
| 34 |
|
| 35 |
@step('the following user-data is provided') |
| 36 |
def step_userdata_is_provided_with_table(context): |
| 37 |
assert context.table, "REQUIRE: table" |
| 38 |
context.table.require_columns(["name", "value"]) |
| 39 |
userdata = context.config.userdata |
| 40 |
for row in context.table.rows: |
| 41 |
name = row["name"] |
| 42 |
expected_value = row["value"] |
| 43 |
if name in userdata: |
| 44 |
actual_value = userdata[name] |
| 45 |
assert_that(str(actual_value), equal_to(expected_value)) |
| 46 |
else: |
| 47 |
assert False, "MISSING: userdata %s" % name |
| 48 |
|
| 49 |
@step('I modify the user-data with') |
| 50 |
def step_modify_userdata_with_table(context): |
| 51 |
assert context.table, "REQUIRE: table" |
| 52 |
context.table.require_columns(["name", "value"]) |
| 53 |
userdata = context.config.userdata |
| 54 |
for row in context.table.rows: |
| 55 |
name = row["name"] |
| 56 |
value = row["value"] |
| 57 |
userdata[name] = value |
| 58 |
""" |
| 59 |
|
| 60 |
@userdata.define |
| 61 |
Scenario: Use define command-line option |
| 62 |
Given a file named "features/userdata_ex1.feature" with: |
| 63 |
""" |
| 64 |
Feature: |
| 65 |
Scenario: |
| 66 |
Given the following user-data is provided: |
| 67 |
| name | value | |
| 68 |
| person1 | Alice | |
| 69 |
| person2 | Bob | |
| 70 |
""" |
| 71 |
And an empty file named "behave.ini" |
| 72 |
When I run "behave -D person1=Alice --define person2=Bob features/userdata_ex1.feature" |
| 73 |
Then it should pass with: |
| 74 |
""" |
| 75 |
1 scenario passed, 0 failed, 0 skipped |
| 76 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 77 |
""" |
| 78 |
|
| 79 |
@userdata.define |
| 80 |
Scenario: Duplicated define with other value overrides first value |
| 81 |
Given a file named "features/userdata_ex2.feature" with: |
| 82 |
""" |
| 83 |
Feature: |
| 84 |
Scenario: |
| 85 |
Given the following user-data is provided: |
| 86 |
| name | value | |
| 87 |
| person1 | Bob | |
| 88 |
""" |
| 89 |
And an empty file named "behave.ini" |
| 90 |
When I run "behave -D person1=Alice -D person1=Bob features/userdata_ex2.feature" |
| 91 |
Then it should pass with: |
| 92 |
""" |
| 93 |
1 scenario passed, 0 failed, 0 skipped |
| 94 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 95 |
""" |
| 96 |
|
| 97 |
@userdata.define |
| 98 |
Scenario: Use boolean flag as command-line definition |
| 99 |
|
| 100 |
Ensure that command-line define without value part is a boolean flag. |
| 101 |
|
| 102 |
Given a file named "features/userdata_ex3.feature" with: |
| 103 |
""" |
| 104 |
Feature: |
| 105 |
Scenario: |
| 106 |
Given the following user-data is provided: |
| 107 |
| name | value | |
| 108 |
| DEBUG | true | |
| 109 |
""" |
| 110 |
And an empty file named "behave.ini" |
| 111 |
When I run "behave -D DEBUG features/userdata_ex3.feature" |
| 112 |
Then it should pass with: |
| 113 |
""" |
| 114 |
1 scenario passed, 0 failed, 0 skipped |
| 115 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 116 |
""" |
| 117 |
|
| 118 |
@userdata.config |
| 119 |
Scenario: Use user-data from behave configuration file |
| 120 |
Given a file named "behave.ini" with: |
| 121 |
""" |
| 122 |
[behave.userdata] |
| 123 |
person1 = Alice |
| 124 |
person2 = Bob |
| 125 |
""" |
| 126 |
When I run "behave features/userdata_ex1.feature" |
| 127 |
Then it should pass with: |
| 128 |
""" |
| 129 |
1 scenario passed, 0 failed, 0 skipped |
| 130 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 131 |
""" |
| 132 |
|
| 133 |
@userdata.config |
| 134 |
Scenario: Override user-data from behave configuration file on command-line |
| 135 |
Given a file named "features/userdata_config2.feature" with: |
| 136 |
""" |
| 137 |
Feature: |
| 138 |
Scenario: |
| 139 |
Given the following user-data is provided: |
| 140 |
| name | value | |
| 141 |
| person1 | Charly | |
| 142 |
| person2 | Bob | |
| 143 |
""" |
| 144 |
And a file named "behave.ini" with: |
| 145 |
""" |
| 146 |
[behave.userdata] |
| 147 |
person1 = Alice |
| 148 |
person2 = Bob |
| 149 |
""" |
| 150 |
When I run "behave -D person1=Charly features/userdata_config2.feature" |
| 151 |
Then it should pass with: |
| 152 |
""" |
| 153 |
1 scenario passed, 0 failed, 0 skipped |
| 154 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 155 |
""" |
| 156 |
|
| 157 |
@userdata.config |
| 158 |
Scenario: Extend user-data from behave configuration file on command-line |
| 159 |
Given a file named "features/userdata_config3.feature" with: |
| 160 |
""" |
| 161 |
Feature: |
| 162 |
Scenario: |
| 163 |
Given the following user-data is provided: |
| 164 |
| name | value | |
| 165 |
| person1 | Alice | |
| 166 |
| person2 | Bob | |
| 167 |
| person3 | Charly | |
| 168 |
""" |
| 169 |
And a file named "behave.ini" with: |
| 170 |
""" |
| 171 |
[behave.userdata] |
| 172 |
person1 = Alice |
| 173 |
person2 = Bob |
| 174 |
""" |
| 175 |
When I run "behave -D person3=Charly features/userdata_config3.feature" |
| 176 |
Then it should pass with: |
| 177 |
""" |
| 178 |
1 scenario passed, 0 failed, 0 skipped |
| 179 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 180 |
""" |
| 181 |
|
| 182 |
@userdata.load |
| 183 |
Scenario: Load user-data configuration in before_all hook (JSON) |
| 184 |
Given an empty file named "behave.ini" |
| 185 |
And a file named "features/environment.py" with: |
| 186 |
""" |
| 187 |
import json |
| 188 |
import os.path |
| 189 |
|
| 190 |
def before_all(context): |
| 191 |
userdata = context.config.userdata |
| 192 |
configfile = userdata.get("configfile", "userconfig.json") |
| 193 |
if os.path.exists(configfile): |
| 194 |
config = json.load(open(configfile)) |
| 195 |
userdata.update(config) |
| 196 |
""" |
| 197 |
And a file named "userconfig.json" with: |
| 198 |
""" |
| 199 |
{ |
| 200 |
"person1": "Anna", |
| 201 |
"person2": "Beatrix" |
| 202 |
} |
| 203 |
""" |
| 204 |
And a file named "features/userdata_load1.feature" with: |
| 205 |
""" |
| 206 |
Feature: |
| 207 |
Scenario: |
| 208 |
Given the following user-data is provided: |
| 209 |
| name | value | |
| 210 |
| person1 | Anna | |
| 211 |
| person2 | Beatrix | |
| 212 |
""" |
| 213 |
When I run "behave features/userdata_load1.feature" |
| 214 |
Then it should pass with: |
| 215 |
""" |
| 216 |
1 scenario passed, 0 failed, 0 skipped |
| 217 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 218 |
""" |
| 219 |
|
| 220 |
@userdata.load |
| 221 |
Scenario: Load user-data configuration in before_all hook (INI) |
| 222 |
Given a file named "behave.ini" with: |
| 223 |
""" |
| 224 |
[behave.userdata] |
| 225 |
configfile = userconfig.ini |
| 226 |
config_section = behave.userdata.more |
| 227 |
""" |
| 228 |
And a file named "userconfig.ini" with: |
| 229 |
""" |
| 230 |
[behave.userdata.more] |
| 231 |
person1 = Anna2 |
| 232 |
person2 = Beatrix2 |
| 233 |
""" |
| 234 |
And a file named "features/environment.py" with: |
| 235 |
""" |
| 236 |
try: |
| 237 |
import configparser |
| 238 |
except: |
| 239 |
import ConfigParser as configparser # -- PY2 |
| 240 |
|
| 241 |
def before_all(context): |
| 242 |
userdata = context.config.userdata |
| 243 |
configfile = userdata.get("configfile", "userconfig.ini") |
| 244 |
section = userdata.get("config_section", "behave.userdata") |
| 245 |
parser = configparser.SafeConfigParser() |
| 246 |
parser.read(configfile) |
| 247 |
if parser.has_section(section): |
| 248 |
userdata.update(parser.items(section)) |
| 249 |
""" |
| 250 |
And a file named "features/userdata_load2.feature" with: |
| 251 |
""" |
| 252 |
Feature: |
| 253 |
Scenario: |
| 254 |
Given the following user-data is provided: |
| 255 |
| name | value | |
| 256 |
| person1 | Anna2 | |
| 257 |
| person2 | Beatrix2 | |
| 258 |
""" |
| 259 |
When I run "behave features/userdata_load2.feature" |
| 260 |
Then it should pass with: |
| 261 |
""" |
| 262 |
1 scenario passed, 0 failed, 0 skipped |
| 263 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 264 |
""" |
| 265 |
|
| 266 |
@bad_practice |
| 267 |
@userdata.modify |
| 268 |
Scenario: Modified user-data is used by the remaining features/scenarios |
| 269 |
|
| 270 |
Given a file named "features/userdata_modify1.feature" with: |
| 271 |
""" |
| 272 |
Feature: |
| 273 |
Scenario: |
| 274 |
Given the following user-data is provided: |
| 275 |
| name | value | |
| 276 |
| person1 | Alice | |
| 277 |
| person2 | Bob | |
| 278 |
When I modify the user-data with: |
| 279 |
| name | value | |
| 280 |
| person1 | MODIFIED_VALUE | |
| 281 |
Then the following user-data is provided: |
| 282 |
| name | value | |
| 283 |
| person1 | MODIFIED_VALUE | |
| 284 |
| person2 | Bob | |
| 285 |
|
| 286 |
Scenario: Next scenario has modified user-data, too |
| 287 |
Given the following user-data is provided: |
| 288 |
| name | value | |
| 289 |
| person1 | MODIFIED_VALUE | |
| 290 |
| person2 | Bob | |
| 291 |
""" |
| 292 |
And a file named "behave.ini" with: |
| 293 |
""" |
| 294 |
[behave.userdata] |
| 295 |
person1 = Alice |
| 296 |
person2 = Bob |
| 297 |
""" |
| 298 |
When I run "behave features/userdata_modify1.feature" |
| 299 |
Then it should pass with: |
| 300 |
""" |
| 301 |
2 scenarios passed, 0 failed, 0 skipped |
| 302 |
4 steps passed, 0 failed, 0 skipped, 0 undefined |
| 303 |
""" |
| 304 |
But note that "modifying userdata is BAD-PRACTICE, except in before_all() hook" |
| 305 |
|
| 306 |
@userdata.case_sensitive |
| 307 |
Scenario: Loaded user-data from configuration should have case-sensitive keys |
| 308 |
|
| 309 |
Given a file named "features/userdata_case_sensitive.feature" with: |
| 310 |
""" |
| 311 |
Feature: |
| 312 |
Scenario: |
| 313 |
Given the following user-data is provided: |
| 314 |
| name | value | |
| 315 |
| CamelCaseKey | 1 | |
| 316 |
| CAPS_SNAKE_KEY | 2 | |
| 317 |
| lower_snake_key | 3 | |
| 318 |
""" |
| 319 |
And a file named "behave.ini" with: |
| 320 |
""" |
| 321 |
[behave.userdata] |
| 322 |
CamelCaseKey = 1 |
| 323 |
CAPS_SNAKE_KEY = 2 |
| 324 |
lower_snake_key = 3 |
| 325 |
""" |
| 326 |
When I run "behave features/userdata_case_sensitive.feature" |
| 327 |
Then it should pass with: |
| 328 |
""" |
| 329 |
1 scenario passed, 0 failed, 0 skipped |
| 330 |
1 step passed, 0 failed, 0 skipped, 0 undefined |
| 331 |
""" |