| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
# pylint: disable=too-many-lines, line-too-long |
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, with_statement |
| 5 |
from collections import defaultdict |
| 6 |
from platform import python_implementation |
| 7 |
import os.path |
| 8 |
import sys |
| 9 |
import warnings |
| 10 |
import tempfile |
| 11 |
import unittest |
| 12 |
import six |
| 13 |
from six import StringIO |
| 14 |
|
| •15 |
from unittest.mock import Mock, patch |
| •16 |
from .tools import eq_, ok_, raises, assert_raises |
| 17 |
|
| 18 |
from behave import runner_util |
| 19 |
from behave.model import Table |
| 20 |
from behave.step_registry import StepRegistry |
| 21 |
from behave import parser, runner |
| 22 |
from behave.configuration import ConfigError |
| 23 |
from behave.formatter.base import StreamOpener |
| 24 |
|
| 25 |
|
| 26 |
# -- CONVENIENCE-ALIAS: |
| 27 |
_text = six.text_type |
| 28 |
|
| 29 |
|
| 30 |
class TestContext(unittest.TestCase): |
| 31 |
# pylint: disable=invalid-name, protected-access, no-self-use |
| 32 |
|
| 33 |
def setUp(self): |
| 34 |
r = Mock() |
| 35 |
self.config = r.config = Mock() |
| 36 |
r.config.verbose = False |
| 37 |
self.context = runner.Context(r) |
| 38 |
|
| 39 |
def test_user_mode_shall_restore_behave_mode(self): |
| 40 |
# -- CASE: No exception is raised. |
| 41 |
initial_mode = runner.Context.BEHAVE |
| 42 |
eq_(self.context._mode, initial_mode) |
| 43 |
with self.context.use_with_user_mode(): |
| 44 |
eq_(self.context._mode, runner.Context.USER) |
| 45 |
self.context.thing = "stuff" |
| 46 |
eq_(self.context._mode, initial_mode) |
| 47 |
|
| 48 |
def test_user_mode_shall_restore_behave_mode_if_assert_fails(self): |
| 49 |
initial_mode = runner.Context.BEHAVE |
| 50 |
eq_(self.context._mode, initial_mode) |
| 51 |
try: |
| 52 |
with self.context.use_with_user_mode(): |
| 53 |
eq_(self.context._mode, runner.Context.USER) |
| 54 |
assert False, "XFAIL" |
| 55 |
except AssertionError: |
| 56 |
eq_(self.context._mode, initial_mode) |
| 57 |
|
| 58 |
def test_user_mode_shall_restore_behave_mode_if_exception_is_raised(self): |
| 59 |
initial_mode = runner.Context.BEHAVE |
| 60 |
eq_(self.context._mode, initial_mode) |
| 61 |
try: |
| 62 |
with self.context.use_with_user_mode(): |
| 63 |
eq_(self.context._mode, runner.Context.USER) |
| 64 |
raise RuntimeError("XFAIL") |
| 65 |
except RuntimeError: |
| 66 |
eq_(self.context._mode, initial_mode) |
| 67 |
|
| 68 |
def test_use_with_user_mode__shall_restore_initial_mode(self): |
| 69 |
# -- CASE: No exception is raised. |
| 70 |
# pylint: disable=protected-access |
| 71 |
initial_mode = runner.Context.BEHAVE |
| 72 |
self.context._mode = initial_mode |
| 73 |
with self.context.use_with_user_mode(): |
| 74 |
eq_(self.context._mode, runner.Context.USER) |
| 75 |
self.context.thing = "stuff" |
| 76 |
eq_(self.context._mode, initial_mode) |
| 77 |
|
| 78 |
def test_use_with_user_mode__shall_restore_initial_mode_with_error(self): |
| 79 |
# -- CASE: Exception is raised. |
| 80 |
# pylint: disable=protected-access |
| 81 |
initial_mode = runner.Context.BEHAVE |
| 82 |
self.context._mode = initial_mode |
| 83 |
try: |
| 84 |
with self.context.use_with_user_mode(): |
| 85 |
eq_(self.context._mode, runner.Context.USER) |
| 86 |
raise RuntimeError("XFAIL") |
| 87 |
except RuntimeError: |
| 88 |
eq_(self.context._mode, initial_mode) |
| 89 |
|
| 90 |
def test_use_with_behave_mode__shall_restore_initial_mode(self): |
| 91 |
# -- CASE: No exception is raised. |
| 92 |
# pylint: disable=protected-access |
| 93 |
initial_mode = runner.Context.USER |
| 94 |
self.context._mode = initial_mode |
| 95 |
with self.context._use_with_behave_mode(): |
| 96 |
eq_(self.context._mode, runner.Context.BEHAVE) |
| 97 |
self.context.thing = "stuff" |
| 98 |
eq_(self.context._mode, initial_mode) |
| 99 |
|
| 100 |
def test_use_with_behave_mode__shall_restore_initial_mode_with_error(self): |
| 101 |
# -- CASE: Exception is raised. |
| 102 |
# pylint: disable=protected-access |
| 103 |
initial_mode = runner.Context.USER |
| 104 |
self.context._mode = initial_mode |
| 105 |
try: |
| 106 |
with self.context._use_with_behave_mode(): |
| 107 |
eq_(self.context._mode, runner.Context.BEHAVE) |
| 108 |
raise RuntimeError("XFAIL") |
| 109 |
except RuntimeError: |
| 110 |
eq_(self.context._mode, initial_mode) |
| 111 |
|
| 112 |
def test_context_contains(self): |
| 113 |
eq_("thing" in self.context, False) |
| 114 |
self.context.thing = "stuff" |
| 115 |
eq_("thing" in self.context, True) |
| 116 |
self.context._push() |
| 117 |
eq_("thing" in self.context, True) |
| 118 |
|
| 119 |
def test_attribute_set_at_upper_level_visible_at_lower_level(self): |
| 120 |
self.context.thing = "stuff" |
| 121 |
self.context._push() |
| 122 |
eq_(self.context.thing, "stuff") |
| 123 |
|
| 124 |
def test_attribute_set_at_lower_level_not_visible_at_upper_level(self): |
| 125 |
self.context._push() |
| 126 |
self.context.thing = "stuff" |
| 127 |
self.context._pop() |
| 128 |
assert getattr(self.context, "thing", None) is None |
| 129 |
|
| 130 |
def test_attributes_set_at_upper_level_visible_at_lower_level(self): |
| 131 |
self.context.thing = "stuff" |
| 132 |
self.context._push() |
| 133 |
eq_(self.context.thing, "stuff") |
| 134 |
self.context.other_thing = "more stuff" |
| 135 |
self.context._push() |
| 136 |
eq_(self.context.thing, "stuff") |
| 137 |
eq_(self.context.other_thing, "more stuff") |
| 138 |
self.context.third_thing = "wombats" |
| 139 |
self.context._push() |
| 140 |
eq_(self.context.thing, "stuff") |
| 141 |
eq_(self.context.other_thing, "more stuff") |
| 142 |
eq_(self.context.third_thing, "wombats") |
| 143 |
|
| 144 |
def test_attributes_set_at_lower_level_not_visible_at_upper_level(self): |
| 145 |
self.context.thing = "stuff" |
| 146 |
|
| 147 |
self.context._push() |
| 148 |
self.context.other_thing = "more stuff" |
| 149 |
|
| 150 |
self.context._push() |
| 151 |
self.context.third_thing = "wombats" |
| 152 |
eq_(self.context.thing, "stuff") |
| 153 |
eq_(self.context.other_thing, "more stuff") |
| 154 |
eq_(self.context.third_thing, "wombats") |
| 155 |
|
| 156 |
self.context._pop() |
| 157 |
eq_(self.context.thing, "stuff") |
| 158 |
eq_(self.context.other_thing, "more stuff") |
| 159 |
assert getattr(self.context, "third_thing", None) is None, "%s is not None" % self.context.third_thing |
| 160 |
|
| 161 |
self.context._pop() |
| 162 |
eq_(self.context.thing, "stuff") |
| 163 |
assert getattr(self.context, "other_thing", None) is None, "%s is not None" % self.context.other_thing |
| 164 |
assert getattr(self.context, "third_thing", None) is None, "%s is not None" % self.context.third_thing |
| 165 |
|
| 166 |
def test_masking_existing_user_attribute_when_verbose_causes_warning(self): |
| 167 |
warns = [] |
| 168 |
|
| 169 |
def catch_warning(*args, **kwargs): |
| 170 |
warns.append(args[0]) |
| 171 |
|
| 172 |
old_showwarning = warnings.showwarning |
| 173 |
warnings.showwarning = catch_warning |
| 174 |
|
| 175 |
# pylint: disable=protected-access |
| 176 |
self.config.verbose = True |
| 177 |
with self.context.use_with_user_mode(): |
| 178 |
self.context.thing = "stuff" |
| 179 |
self.context._push() |
| 180 |
self.context.thing = "other stuff" |
| 181 |
|
| 182 |
warnings.showwarning = old_showwarning |
| 183 |
|
| 184 |
print(repr(warns)) |
| 185 |
assert warns, "warns is empty!" |
| 186 |
warning = warns[0] |
| 187 |
assert isinstance(warning, runner.ContextMaskWarning), "warning is not a ContextMaskWarning" |
| 188 |
info = warning.args[0] |
| 189 |
assert info.startswith("user code"), "%r doesn't start with 'user code'" % info |
| 190 |
assert "'thing'" in info, "%r not in %r" % ("'thing'", info) |
| 191 |
assert "tutorial" in info, '"tutorial" not in %r' % (info, ) |
| 192 |
|
| 193 |
def test_masking_existing_user_attribute_when_not_verbose_causes_no_warning(self): |
| 194 |
warns = [] |
| 195 |
|
| 196 |
def catch_warning(*args, **kwargs): |
| 197 |
warns.append(args[0]) |
| 198 |
|
| 199 |
old_showwarning = warnings.showwarning |
| 200 |
warnings.showwarning = catch_warning |
| 201 |
|
| 202 |
# explicit |
| 203 |
# pylint: disable=protected-access |
| 204 |
self.config.verbose = False |
| 205 |
with self.context.use_with_user_mode(): |
| 206 |
self.context.thing = "stuff" |
| 207 |
self.context._push() |
| 208 |
self.context.thing = "other stuff" |
| 209 |
|
| 210 |
warnings.showwarning = old_showwarning |
| 211 |
|
| 212 |
assert not warns |
| 213 |
|
| 214 |
def test_behave_masking_user_attribute_causes_warning(self): |
| 215 |
warns = [] |
| 216 |
|
| 217 |
def catch_warning(*args, **kwargs): |
| 218 |
warns.append(args[0]) |
| 219 |
|
| 220 |
old_showwarning = warnings.showwarning |
| 221 |
warnings.showwarning = catch_warning |
| 222 |
|
| 223 |
with self.context.use_with_user_mode(): |
| 224 |
self.context.thing = "stuff" |
| 225 |
# pylint: disable=protected-access |
| 226 |
self.context._push() |
| 227 |
self.context.thing = "other stuff" |
| 228 |
|
| 229 |
warnings.showwarning = old_showwarning |
| 230 |
|
| 231 |
print(repr(warns)) |
| 232 |
assert warns, "OOPS: warns is empty, but expected non-empty" |
| 233 |
warning = warns[0] |
| 234 |
assert isinstance(warning, runner.ContextMaskWarning), "warning is not a ContextMaskWarning" |
| 235 |
info = warning.args[0] |
| 236 |
assert info.startswith("behave runner"), "%r doesn't start with 'behave runner'" % info |
| 237 |
assert "'thing'" in info, "%r not in %r" % ("'thing'", info) |
| 238 |
filename = __file__.rsplit(".", 1)[0] |
| 239 |
if python_implementation() == "Jython": |
| 240 |
filename = filename.replace("$py", ".py") |
| 241 |
assert filename in info, "%r not in %r" % (filename, info) |
| 242 |
|
| 243 |
def test_setting_root_attribute_that_masks_existing_causes_warning(self): |
| 244 |
# pylint: disable=protected-access |
| 245 |
warns = [] |
| 246 |
|
| 247 |
def catch_warning(*args, **kwargs): |
| 248 |
warns.append(args[0]) |
| 249 |
|
| 250 |
old_showwarning = warnings.showwarning |
| 251 |
warnings.showwarning = catch_warning |
| 252 |
|
| 253 |
with self.context.use_with_user_mode(): |
| 254 |
self.context._push() |
| 255 |
self.context.thing = "teak" |
| 256 |
self.context._set_root_attribute("thing", "oak") |
| 257 |
|
| 258 |
warnings.showwarning = old_showwarning |
| 259 |
|
| 260 |
print(repr(warns)) |
| 261 |
assert warns |
| 262 |
warning = warns[0] |
| 263 |
assert isinstance(warning, runner.ContextMaskWarning) |
| 264 |
info = warning.args[0] |
| 265 |
assert info.startswith("behave runner"), "%r doesn't start with 'behave runner'" % info |
| 266 |
assert "'thing'" in info, "%r not in %r" % ("'thing'", info) |
| 267 |
filename = __file__.rsplit(".", 1)[0] |
| 268 |
if python_implementation() == "Jython": |
| 269 |
filename = filename.replace("$py", ".py") |
| 270 |
assert filename in info, "%r not in %r" % (filename, info) |
| 271 |
|
| 272 |
def test_context_deletable(self): |
| 273 |
eq_("thing" in self.context, False) |
| 274 |
self.context.thing = "stuff" |
| 275 |
eq_("thing" in self.context, True) |
| 276 |
del self.context.thing |
| 277 |
eq_("thing" in self.context, False) |
| 278 |
|
| 279 |
@raises(AttributeError) |
| 280 |
def test_context_deletable_raises(self): |
| 281 |
# pylint: disable=protected-access |
| 282 |
eq_("thing" in self.context, False) |
| 283 |
self.context.thing = "stuff" |
| 284 |
eq_("thing" in self.context, True) |
| 285 |
self.context._push() |
| 286 |
eq_("thing" in self.context, True) |
| 287 |
del self.context.thing |
| 288 |
|
| •289 |
|
| 290 |
class ExampleSteps(object): |
| 291 |
text = None |
| 292 |
table = None |
| 293 |
|
| 294 |
@staticmethod |
| 295 |
def step_passes(context): # pylint: disable=unused-argument |
| 296 |
pass |
| 297 |
|
| 298 |
@staticmethod |
| 299 |
def step_fails(context): # pylint: disable=unused-argument |
| 300 |
assert False, "XFAIL" |
| 301 |
|
| 302 |
@classmethod |
| 303 |
def step_with_text(cls, context): |
| 304 |
assert context.text is not None, "REQUIRE: multi-line text" |
| 305 |
cls.text = context.text |
| 306 |
|
| 307 |
@classmethod |
| 308 |
def step_with_table(cls, context): |
| 309 |
assert context.table, "REQUIRE: table" |
| 310 |
cls.table = context.table |
| 311 |
|
| 312 |
@classmethod |
| 313 |
def register_steps_with(cls, step_registry): |
| 314 |
# pylint: disable=bad-whitespace |
| 315 |
step_definitions = [ |
| 316 |
("step", "a step passes", cls.step_passes), |
| 317 |
("step", "a step fails", cls.step_fails), |
| 318 |
("step", "a step with text", cls.step_with_text), |
| 319 |
("step", "a step with a table", cls.step_with_table), |
| 320 |
] |
| 321 |
for keyword, pattern, func in step_definitions: |
| 322 |
step_registry.add_step_definition(keyword, pattern, func) |
| 323 |
|
| •324 |
|
| 325 |
class TestContext_ExecuteSteps(unittest.TestCase): |
| 326 |
""" |
| 327 |
Test the behave.runner.Context.execute_steps() functionality. |
| 328 |
""" |
| 329 |
# pylint: disable=invalid-name, no-self-use |
| 330 |
step_registry = None |
| 331 |
|
| 332 |
def setUp(self): |
| 333 |
if not self.step_registry: |
| 334 |
# -- SETUP ONCE: |
| 335 |
self.step_registry = StepRegistry() |
| 336 |
ExampleSteps.register_steps_with(self.step_registry) |
| 337 |
ExampleSteps.text = None |
| 338 |
ExampleSteps.table = None |
| 339 |
|
| 340 |
runner_ = Mock() |
| 341 |
self.config = runner_.config = Mock() |
| 342 |
runner_.config.verbose = False |
| 343 |
runner_.config.stdout_capture = False |
| 344 |
runner_.config.stderr_capture = False |
| 345 |
runner_.config.log_capture = False |
| •346 |
runner_.config.logging_format = None |
| 347 |
runner_.config.logging_datefmt = None |
| 348 |
runner_.step_registry = self.step_registry |
| 349 |
|
| 350 |
self.context = runner.Context(runner_) |
| 351 |
runner_.context = self.context |
| 352 |
self.context.feature = Mock() |
| 353 |
self.context.feature.parser = parser.Parser() |
| 354 |
self.context.runner = runner_ |
| 355 |
# self.context.text = None |
| 356 |
# self.context.table = None |
| 357 |
|
| 358 |
def test_execute_steps_with_simple_steps(self): |
| 359 |
doc = u""" |
| 360 |
Given a step passes |
| 361 |
Then a step passes |
| 362 |
""".lstrip() |
| 363 |
with patch("behave.step_registry.registry", self.step_registry): |
| 364 |
result = self.context.execute_steps(doc) |
| 365 |
eq_(result, True) |
| 366 |
|
| 367 |
def test_execute_steps_with_failing_step(self): |
| 368 |
doc = u""" |
| 369 |
Given a step passes |
| 370 |
When a step fails |
| 371 |
Then a step passes |
| 372 |
""".lstrip() |
| 373 |
with patch("behave.step_registry.registry", self.step_registry): |
| 374 |
try: |
| 375 |
result = self.context.execute_steps(doc) |
| 376 |
except AssertionError as e: |
| 377 |
ok_("FAILED SUB-STEP: When a step fails" in _text(e)) |
| 378 |
|
| 379 |
def test_execute_steps_with_undefined_step(self): |
| 380 |
doc = u""" |
| 381 |
Given a step passes |
| 382 |
When a step is undefined |
| 383 |
Then a step passes |
| 384 |
""".lstrip() |
| 385 |
with patch("behave.step_registry.registry", self.step_registry): |
| 386 |
try: |
| 387 |
result = self.context.execute_steps(doc) |
| 388 |
except AssertionError as e: |
| 389 |
ok_("UNDEFINED SUB-STEP: When a step is undefined" in _text(e)) |
| 390 |
|
| 391 |
def test_execute_steps_with_text(self): |
| 392 |
doc = u''' |
| 393 |
Given a step passes |
| 394 |
When a step with text: |
| 395 |
""" |
| 396 |
Lorem ipsum |
| 397 |
Ipsum lorem |
| 398 |
""" |
| 399 |
Then a step passes |
| 400 |
'''.lstrip() |
| 401 |
with patch("behave.step_registry.registry", self.step_registry): |
| 402 |
result = self.context.execute_steps(doc) |
| 403 |
expected_text = "Lorem ipsum\nIpsum lorem" |
| 404 |
eq_(result, True) |
| 405 |
eq_(expected_text, ExampleSteps.text) |
| 406 |
|
| 407 |
def test_execute_steps_with_table(self): |
| 408 |
doc = u""" |
| 409 |
Given a step with a table: |
| 410 |
| Name | Age | |
| 411 |
| Alice | 12 | |
| 412 |
| Bob | 23 | |
| 413 |
Then a step passes |
| 414 |
""".lstrip() |
| 415 |
with patch("behave.step_registry.registry", self.step_registry): |
| 416 |
# pylint: disable=bad-whitespace, bad-continuation |
| 417 |
result = self.context.execute_steps(doc) |
| 418 |
expected_table = Table([u"Name", u"Age"], 0, [ |
| 419 |
[u"Alice", u"12"], |
| 420 |
[u"Bob", u"23"], |
| 421 |
]) |
| 422 |
eq_(result, True) |
| 423 |
eq_(expected_table, ExampleSteps.table) |
| 424 |
|
| 425 |
def test_context_table_is_restored_after_execute_steps_without_table(self): |
| 426 |
doc = u""" |
| 427 |
Given a step passes |
| 428 |
Then a step passes |
| 429 |
""".lstrip() |
| 430 |
with patch("behave.step_registry.registry", self.step_registry): |
| 431 |
original_table = "<ORIGINAL_TABLE>" |
| 432 |
self.context.table = original_table |
| 433 |
self.context.execute_steps(doc) |
| 434 |
eq_(self.context.table, original_table) |
| 435 |
|
| 436 |
def test_context_table_is_restored_after_execute_steps_with_table(self): |
| 437 |
doc = u""" |
| 438 |
Given a step with a table: |
| 439 |
| Name | Age | |
| 440 |
| Alice | 12 | |
| 441 |
| Bob | 23 | |
| 442 |
Then a step passes |
| 443 |
""".lstrip() |
| 444 |
with patch("behave.step_registry.registry", self.step_registry): |
| 445 |
original_table = "<ORIGINAL_TABLE>" |
| 446 |
self.context.table = original_table |
| 447 |
self.context.execute_steps(doc) |
| 448 |
eq_(self.context.table, original_table) |
| 449 |
|
| 450 |
def test_context_text_is_restored_after_execute_steps_without_text(self): |
| 451 |
doc = u""" |
| 452 |
Given a step passes |
| 453 |
Then a step passes |
| 454 |
""".lstrip() |
| 455 |
with patch("behave.step_registry.registry", self.step_registry): |
| 456 |
original_text = "<ORIGINAL_TEXT>" |
| 457 |
self.context.text = original_text |
| 458 |
self.context.execute_steps(doc) |
| 459 |
eq_(self.context.text, original_text) |
| 460 |
|
| 461 |
def test_context_text_is_restored_after_execute_steps_with_text(self): |
| 462 |
doc = u''' |
| 463 |
Given a step passes |
| 464 |
When a step with text: |
| 465 |
""" |
| 466 |
Lorem ipsum |
| 467 |
Ipsum lorem |
| 468 |
""" |
| 469 |
'''.lstrip() |
| 470 |
with patch("behave.step_registry.registry", self.step_registry): |
| 471 |
original_text = "<ORIGINAL_TEXT>" |
| 472 |
self.context.text = original_text |
| 473 |
self.context.execute_steps(doc) |
| 474 |
eq_(self.context.text, original_text) |
| 475 |
|
| 476 |
|
| 477 |
@raises(ValueError) |
| 478 |
def test_execute_steps_should_fail_when_called_without_feature(self): |
| 479 |
doc = u""" |
| 480 |
Given a passes |
| 481 |
Then a step passes |
| 482 |
""".lstrip() |
| 483 |
with patch("behave.step_registry.registry", self.step_registry): |
| 484 |
self.context.feature = None |
| 485 |
self.context.execute_steps(doc) |
| 486 |
|
| 487 |
|
| 488 |
def create_mock_config(): |
| 489 |
config = Mock() |
| 490 |
config.steps_dir = "steps" |
| 491 |
config.environment_file = "environment.py" |
| 492 |
return config |
| 493 |
|
| 494 |
|
| 495 |
class TestRunner(object): |
| 496 |
# pylint: disable=invalid-name, no-self-use |
| 497 |
|
| 498 |
def test_load_hooks_execfiles_hook_file(self): |
| 499 |
with patch("behave.runner.exec_file") as ef: |
| 500 |
with patch("os.path.exists") as exists: |
| 501 |
exists.return_value = True |
| 502 |
base_dir = "fake/path" |
| 503 |
hooks_path = os.path.join(base_dir, "environment.py") |
| 504 |
|
| 505 |
r = runner.Runner(create_mock_config()) |
| 506 |
r.base_dir = base_dir |
| 507 |
r.load_hooks() |
| 508 |
|
| 509 |
exists.assert_called_with(hooks_path) |
| 510 |
ef.assert_called_with(hooks_path, r.hooks) |
| 511 |
|
| 512 |
def test_run_hook_runs_a_hook_that_exists(self): |
| 513 |
config = Mock() |
| 514 |
r = runner.Runner(config) |
| 515 |
# XXX r.config = Mock() |
| 516 |
r.config.stdout_capture = False |
| 517 |
r.config.stderr_capture = False |
| 518 |
r.config.dry_run = False |
| 519 |
r.hooks["before_lunch"] = hook = Mock() |
| 520 |
args = (runner.Context(Mock()), Mock(), Mock()) |
| 521 |
r.run_hook("before_lunch", *args) |
| 522 |
|
| 523 |
hook.assert_called_with(*args) |
| 524 |
|
| 525 |
def test_run_hook_does_not_runs_a_hook_that_exists_if_dry_run(self): |
| 526 |
r = runner.Runner(None) |
| 527 |
r.config = Mock() |
| 528 |
r.config.dry_run = True |
| 529 |
r.hooks["before_lunch"] = hook = Mock() |
| 530 |
args = (runner.Context(Mock()), Mock(), Mock()) |
| 531 |
r.run_hook("before_lunch", *args) |
| 532 |
|
| 533 |
assert len(hook.call_args_list) == 0 |
| 534 |
|
| 535 |
def test_setup_capture_creates_stringio_for_stdout(self): |
| 536 |
r = runner.Runner(Mock()) |
| 537 |
r.config.stdout_capture = True |
| 538 |
r.config.log_capture = False |
| 539 |
r.context = Mock() |
| 540 |
|
| 541 |
r.setup_capture() |
| 542 |
|
| 543 |
assert r.capture_controller.stdout_capture is not None |
| 544 |
assert isinstance(r.capture_controller.stdout_capture, StringIO) |
| 545 |
|
| 546 |
def test_setup_capture_does_not_create_stringio_if_not_wanted(self): |
| 547 |
r = runner.Runner(Mock()) |
| 548 |
r.config.stdout_capture = False |
| 549 |
r.config.stderr_capture = False |
| 550 |
r.config.log_capture = False |
| 551 |
|
| 552 |
r.setup_capture() |
| 553 |
|
| 554 |
assert r.capture_controller.stdout_capture is None |
| 555 |
|
| 556 |
@patch("behave.capture.LoggingCapture") |
| 557 |
def test_setup_capture_creates_memory_handler_for_logging(self, handler): |
| 558 |
r = runner.Runner(Mock()) |
| 559 |
r.config.stdout_capture = False |
| 560 |
r.config.log_capture = True |
| 561 |
r.context = Mock() |
| 562 |
|
| 563 |
r.setup_capture() |
| 564 |
|
| 565 |
assert r.capture_controller.log_capture is not None |
| 566 |
handler.assert_called_with(r.config) |
| 567 |
r.capture_controller.log_capture.inveigle.assert_called_with() |
| 568 |
|
| 569 |
def test_setup_capture_does_not_create_memory_handler_if_not_wanted(self): |
| 570 |
r = runner.Runner(Mock()) |
| 571 |
r.config.stdout_capture = False |
| 572 |
r.config.stderr_capture = False |
| 573 |
r.config.log_capture = False |
| 574 |
|
| 575 |
r.setup_capture() |
| 576 |
|
| 577 |
assert r.capture_controller.log_capture is None |
| 578 |
|
| 579 |
def test_start_stop_capture_switcheroos_sys_stdout(self): |
| 580 |
old_stdout = sys.stdout |
| 581 |
sys.stdout = new_stdout = Mock() |
| 582 |
|
| 583 |
r = runner.Runner(Mock()) |
| 584 |
r.config.stdout_capture = True |
| 585 |
r.config.log_capture = False |
| 586 |
r.context = Mock() |
| 587 |
|
| 588 |
r.setup_capture() |
| 589 |
r.start_capture() |
| 590 |
|
| 591 |
eq_(sys.stdout, r.capture_controller.stdout_capture) |
| 592 |
|
| 593 |
r.stop_capture() |
| 594 |
|
| 595 |
eq_(sys.stdout, new_stdout) |
| 596 |
|
| 597 |
sys.stdout = old_stdout |
| 598 |
|
| 599 |
def test_start_stop_capture_leaves_sys_stdout_alone_if_off(self): |
| 600 |
r = runner.Runner(Mock()) |
| 601 |
r.config.stdout_capture = False |
| 602 |
r.config.log_capture = False |
| 603 |
|
| 604 |
old_stdout = sys.stdout |
| 605 |
|
| 606 |
r.start_capture() |
| 607 |
|
| 608 |
eq_(sys.stdout, old_stdout) |
| 609 |
|
| 610 |
r.stop_capture() |
| 611 |
|
| 612 |
eq_(sys.stdout, old_stdout) |
| 613 |
|
| 614 |
def test_teardown_capture_removes_log_tap(self): |
| 615 |
r = runner.Runner(Mock()) |
| 616 |
r.config.stdout_capture = False |
| 617 |
r.config.log_capture = True |
| 618 |
|
| 619 |
r.capture_controller.log_capture = Mock() |
| 620 |
|
| 621 |
r.teardown_capture() |
| 622 |
|
| 623 |
r.capture_controller.log_capture.abandon.assert_called_with() |
| 624 |
|
| 625 |
def test_exec_file(self): |
| 626 |
fn = tempfile.mktemp() |
| 627 |
with open(fn, "w") as f: |
| 628 |
f.write("spam = __file__\n") |
| 629 |
g = {} |
| 630 |
l = {} |
| 631 |
runner_util.exec_file(fn, g, l) |
| 632 |
assert "__file__" in l |
| 633 |
# pylint: disable=too-many-format-args |
| 634 |
assert "spam" in l, '"spam" variable not set in locals (%r)' % (g, l) |
| 635 |
# pylint: enable=too-many-format-args |
| 636 |
eq_(l["spam"], fn) |
| 637 |
|
| 638 |
def test_run_returns_true_if_everything_passed(self): |
| 639 |
r = runner.Runner(Mock()) |
| 640 |
r.setup_capture = Mock() |
| 641 |
r.setup_paths = Mock() |
| 642 |
r.run_with_paths = Mock() |
| 643 |
r.run_with_paths.return_value = True |
| 644 |
assert r.run() |
| 645 |
|
| 646 |
def test_run_returns_false_if_anything_failed(self): |
| 647 |
r = runner.Runner(Mock()) |
| 648 |
r.setup_capture = Mock() |
| 649 |
r.setup_paths = Mock() |
| 650 |
r.run_with_paths = Mock() |
| 651 |
r.run_with_paths.return_value = False |
| 652 |
assert not r.run() |
| 653 |
|
| 654 |
|
| 655 |
class TestRunWithPaths(unittest.TestCase): |
| 656 |
# pylint: disable=invalid-name, no-self-use |
| 657 |
|
| 658 |
def setUp(self): |
| 659 |
self.config = Mock() |
| 660 |
self.config.reporters = [] |
| 661 |
self.config.logging_level = None |
| 662 |
self.config.logging_filter = None |
| 663 |
self.config.outputs = [Mock(), StreamOpener(stream=sys.stdout)] |
| 664 |
self.config.format = ["plain", "progress"] |
| •665 |
self.config.logging_format = None |
| 666 |
self.config.logging_datefmt = None |
| 667 |
self.runner = runner.Runner(self.config) |
| 668 |
self.load_hooks = self.runner.load_hooks = Mock() |
| 669 |
self.load_step_definitions = self.runner.load_step_definitions = Mock() |
| 670 |
self.run_hook = self.runner.run_hook = Mock() |
| 671 |
self.run_step = self.runner.run_step = Mock() |
| 672 |
self.feature_locations = self.runner.feature_locations = Mock() |
| 673 |
self.calculate_summaries = self.runner.calculate_summaries = Mock() |
| 674 |
|
| 675 |
self.formatter_class = patch("behave.formatter.pretty.PrettyFormatter") |
| 676 |
formatter_class = self.formatter_class.start() |
| 677 |
formatter_class.return_value = self.formatter = Mock() |
| 678 |
|
| 679 |
def tearDown(self): |
| 680 |
self.formatter_class.stop() |
| 681 |
|
| 682 |
def test_loads_hooks_and_step_definitions(self): |
| 683 |
self.feature_locations.return_value = [] |
| 684 |
self.runner.run_with_paths() |
| 685 |
|
| 686 |
assert self.load_hooks.called |
| 687 |
assert self.load_step_definitions.called |
| 688 |
|
| 689 |
def test_runs_before_all_and_after_all_hooks(self): |
| 690 |
# Make runner.feature_locations() and runner.run_hook() the same mock so |
| 691 |
# we can make sure things happen in the right order. |
| 692 |
self.runner.feature_locations = self.run_hook |
| 693 |
self.runner.feature_locations.return_value = [] |
| 694 |
self.runner.context = Mock() |
| 695 |
self.runner.run_with_paths() |
| 696 |
|
| 697 |
eq_(self.run_hook.call_args_list, [ |
| 698 |
((), {}), |
| 699 |
(("before_all", self.runner.context), {}), |
| 700 |
(("after_all", self.runner.context), {}), |
| 701 |
]) |
| 702 |
|
| 703 |
@patch("behave.parser.parse_file") |
| 704 |
@patch("os.path.abspath") |
| 705 |
def test_parses_feature_files_and_appends_to_feature_list(self, abspath, |
| 706 |
parse_file): |
| 707 |
feature_locations = ["one", "two", "three"] |
| 708 |
feature = Mock() |
| 709 |
feature.tags = [] |
| 710 |
feature.__iter__ = Mock(return_value=iter([])) |
| 711 |
feature.run.return_value = False |
| 712 |
self.runner.feature_locations.return_value = feature_locations |
| 713 |
abspath.side_effect = lambda x: x.upper() |
| 714 |
self.config.lang = "fritz" |
| 715 |
self.config.format = ["plain"] |
| 716 |
self.config.outputs = [StreamOpener(stream=sys.stdout)] |
| 717 |
self.config.output.encoding = None |
| 718 |
self.config.exclude = lambda s: False |
| 719 |
self.config.junit = False |
| 720 |
self.config.summary = False |
| 721 |
parse_file.return_value = feature |
| 722 |
|
| 723 |
self.runner.run_with_paths() |
| 724 |
|
| 725 |
expected_parse_file_args = \ |
| 726 |
[((x.upper(),), {"language": "fritz"}) for x in feature_locations] |
| 727 |
eq_(parse_file.call_args_list, expected_parse_file_args) |
| 728 |
eq_(self.runner.features, [feature] * 3) |
| 729 |
|
| 730 |
|
| 731 |
class FsMock(object): |
| 732 |
def __init__(self, *paths): |
| 733 |
self.base = os.path.abspath(".") |
| 734 |
self.sep = os.path.sep |
| 735 |
|
| 736 |
# This bit of gymnastics is to support Windows. We feed in a bunch of |
| 737 |
# paths in places using FsMock that assume that POSIX-style paths |
| 738 |
# work. This is faster than fixing all of those but at some point we |
| 739 |
# should totally do it properly with os.path.join() and all that. |
| 740 |
def full_split(path): |
| 741 |
bits = [] |
| 742 |
|
| 743 |
while path: |
| 744 |
path, bit = os.path.split(path) |
| 745 |
bits.insert(0, bit) |
| 746 |
|
| 747 |
return bits |
| 748 |
|
| 749 |
paths = [os.path.join(self.base, *full_split(path)) for path in paths] |
| 750 |
print(repr(paths)) |
| 751 |
self.paths = paths |
| 752 |
self.files = set() |
| 753 |
self.dirs = defaultdict(list) |
| 754 |
separators = [sep for sep in (os.path.sep, os.path.altsep) if sep] |
| 755 |
for path in paths: |
| 756 |
if path[-1] in separators: |
| 757 |
self.dirs[path[:-1]] = [] |
| 758 |
d, p = os.path.split(path[:-1]) |
| 759 |
while d and p: |
| 760 |
self.dirs[d].append(p) |
| 761 |
d, p = os.path.split(d) |
| 762 |
else: |
| 763 |
self.files.add(path) |
| 764 |
d, f = os.path.split(path) |
| 765 |
self.dirs[d].append(f) |
| 766 |
self.calls = [] |
| 767 |
|
| 768 |
def listdir(self, dir): |
| 769 |
# pylint: disable=W0622 |
| 770 |
# W0622 Redefining built-in dir |
| 771 |
self.calls.append(("listdir", dir)) |
| 772 |
return self.dirs.get(dir, []) |
| 773 |
|
| 774 |
def isfile(self, path): |
| 775 |
self.calls.append(("isfile", path)) |
| 776 |
return path in self.files |
| 777 |
|
| 778 |
def isdir(self, path): |
| 779 |
self.calls.append(("isdir", path)) |
| 780 |
return path in self.dirs |
| 781 |
|
| 782 |
def exists(self, path): |
| 783 |
self.calls.append(("exists", path)) |
| 784 |
return path in self.dirs or path in self.files |
| 785 |
|
| 786 |
def walk(self, path, locations=None): |
| 787 |
if locations is None: |
| 788 |
assert path in self.dirs, "%s not in %s" % (path, self.dirs) |
| 789 |
locations = [] |
| 790 |
dirnames = [] |
| 791 |
filenames = [] |
| 792 |
for e in self.dirs[path]: |
| 793 |
if os.path.join(path, e) in self.dirs: |
| 794 |
dirnames.append(e) |
| 795 |
self.walk(os.path.join(path, e), locations) |
| 796 |
else: |
| 797 |
filenames.append(e) |
| 798 |
locations.append((path, dirnames, filenames)) |
| 799 |
return locations |
| 800 |
|
| 801 |
# utilities that we need |
| 802 |
# pylint: disable=no-self-use |
| 803 |
def dirname(self, path, orig=os.path.dirname): |
| 804 |
return orig(path) |
| 805 |
|
| 806 |
def abspath(self, path, orig=os.path.abspath): |
| 807 |
return orig(path) |
| 808 |
|
| 809 |
def join(self, x, y, orig=os.path.join): |
| 810 |
return orig(x, y) |
| 811 |
|
| 812 |
def split(self, path, orig=os.path.split): |
| 813 |
return orig(path) |
| 814 |
|
| 815 |
def splitdrive(self, path, orig=os.path.splitdrive): |
| 816 |
return orig(path) |
| 817 |
|
| 818 |
|
| 819 |
class TestFeatureDirectory(object): |
| 820 |
# pylint: disable=invalid-name, no-self-use |
| 821 |
|
| 822 |
def test_default_path_no_steps(self): |
| 823 |
config = create_mock_config() |
| 824 |
config.paths = [] |
| 825 |
config.verbose = True |
| 826 |
r = runner.Runner(config) |
| 827 |
|
| 828 |
fs = FsMock() |
| 829 |
|
| 830 |
# will look for a "features" directory and not find one |
| 831 |
with patch("os.path", fs): |
| 832 |
assert_raises(ConfigError, r.setup_paths) |
| 833 |
|
| 834 |
ok_(("isdir", os.path.join(fs.base, "features", "steps")) in fs.calls) |
| 835 |
|
| 836 |
def test_default_path_no_features(self): |
| 837 |
config = create_mock_config() |
| 838 |
config.paths = [] |
| 839 |
config.verbose = True |
| 840 |
r = runner.Runner(config) |
| 841 |
|
| 842 |
fs = FsMock("features/steps/") |
| 843 |
with patch("os.path", fs): |
| 844 |
with patch("os.walk", fs.walk): |
| 845 |
assert_raises(ConfigError, r.setup_paths) |
| 846 |
|
| 847 |
def test_default_path(self): |
| 848 |
config = create_mock_config() |
| 849 |
config.paths = [] |
| 850 |
config.verbose = True |
| 851 |
r = runner.Runner(config) |
| 852 |
|
| 853 |
fs = FsMock("features/steps/", "features/foo.feature") |
| 854 |
|
| 855 |
with patch("os.path", fs): |
| 856 |
with patch("os.walk", fs.walk): |
| 857 |
with r.path_manager: |
| 858 |
r.setup_paths() |
| 859 |
|
| 860 |
eq_(r.base_dir, os.path.abspath("features")) |
| 861 |
|
| 862 |
def test_supplied_feature_file(self): |
| 863 |
config = create_mock_config() |
| 864 |
config.paths = ["foo.feature"] |
| 865 |
config.verbose = True |
| 866 |
r = runner.Runner(config) |
| 867 |
r.context = Mock() |
| 868 |
|
| 869 |
fs = FsMock("steps/", "foo.feature") |
| 870 |
|
| 871 |
with patch("os.path", fs): |
| 872 |
with patch("os.walk", fs.walk): |
| 873 |
with r.path_manager: |
| 874 |
r.setup_paths() |
| 875 |
ok_(("isdir", os.path.join(fs.base, "steps")) in fs.calls) |
| 876 |
ok_(("isfile", os.path.join(fs.base, "foo.feature")) in fs.calls) |
| 877 |
|
| 878 |
eq_(r.base_dir, fs.base) |
| 879 |
|
| 880 |
def test_supplied_feature_file_no_steps(self): |
| 881 |
config = create_mock_config() |
| 882 |
config.paths = ["foo.feature"] |
| 883 |
config.verbose = True |
| 884 |
r = runner.Runner(config) |
| 885 |
|
| 886 |
fs = FsMock("foo.feature") |
| 887 |
|
| 888 |
with patch("os.path", fs): |
| 889 |
with patch("os.walk", fs.walk): |
| 890 |
with r.path_manager: |
| 891 |
assert_raises(ConfigError, r.setup_paths) |
| 892 |
|
| 893 |
def test_supplied_feature_directory(self): |
| 894 |
config = create_mock_config() |
| 895 |
config.paths = ["spam"] |
| 896 |
config.verbose = True |
| 897 |
r = runner.Runner(config) |
| 898 |
|
| 899 |
fs = FsMock("spam/", "spam/steps/", "spam/foo.feature") |
| 900 |
|
| 901 |
with patch("os.path", fs): |
| 902 |
with patch("os.walk", fs.walk): |
| 903 |
with r.path_manager: |
| 904 |
r.setup_paths() |
| 905 |
|
| 906 |
ok_(("isdir", os.path.join(fs.base, "spam", "steps")) in fs.calls) |
| 907 |
|
| 908 |
eq_(r.base_dir, os.path.join(fs.base, "spam")) |
| 909 |
|
| 910 |
def test_supplied_feature_directory_no_steps(self): |
| 911 |
config = create_mock_config() |
| 912 |
config.paths = ["spam"] |
| 913 |
config.verbose = True |
| 914 |
r = runner.Runner(config) |
| 915 |
|
| 916 |
fs = FsMock("spam/", "spam/foo.feature") |
| 917 |
|
| 918 |
with patch("os.path", fs): |
| 919 |
with patch("os.walk", fs.walk): |
| 920 |
assert_raises(ConfigError, r.setup_paths) |
| 921 |
|
| 922 |
ok_(("isdir", os.path.join(fs.base, "spam", "steps")) in fs.calls) |
| 923 |
|
| 924 |
def test_supplied_feature_directory_missing(self): |
| 925 |
config = create_mock_config() |
| 926 |
config.paths = ["spam"] |
| 927 |
config.verbose = True |
| 928 |
r = runner.Runner(config) |
| 929 |
|
| 930 |
fs = FsMock() |
| 931 |
|
| 932 |
with patch("os.path", fs): |
| 933 |
with patch("os.walk", fs.walk): |
| 934 |
assert_raises(ConfigError, r.setup_paths) |
| 935 |
|
| 936 |
|
| 937 |
class TestFeatureDirectoryLayout2(object): |
| 938 |
# pylint: disable=invalid-name, no-self-use |
| 939 |
|
| 940 |
def test_default_path(self): |
| 941 |
config = create_mock_config() |
| 942 |
config.paths = [] |
| 943 |
config.verbose = True |
| 944 |
r = runner.Runner(config) |
| 945 |
|
| 946 |
fs = FsMock( |
| 947 |
"features/", |
| 948 |
"features/steps/", |
| 949 |
"features/group1/", |
| 950 |
"features/group1/foo.feature", |
| 951 |
) |
| 952 |
|
| 953 |
with patch("os.path", fs): |
| 954 |
with patch("os.walk", fs.walk): |
| 955 |
with r.path_manager: |
| 956 |
r.setup_paths() |
| 957 |
|
| 958 |
eq_(r.base_dir, os.path.abspath("features")) |
| 959 |
|
| 960 |
def test_supplied_root_directory(self): |
| 961 |
config = create_mock_config() |
| 962 |
config.paths = ["features"] |
| 963 |
config.verbose = True |
| 964 |
r = runner.Runner(config) |
| 965 |
|
| 966 |
fs = FsMock( |
| 967 |
"features/", |
| 968 |
"features/group1/", |
| 969 |
"features/group1/foo.feature", |
| 970 |
"features/steps/", |
| 971 |
) |
| 972 |
|
| 973 |
with patch("os.path", fs): |
| 974 |
with patch("os.walk", fs.walk): |
| 975 |
with r.path_manager: |
| 976 |
r.setup_paths() |
| 977 |
|
| 978 |
ok_(("isdir", os.path.join(fs.base, "features", "steps")) in fs.calls) |
| 979 |
eq_(r.base_dir, os.path.join(fs.base, "features")) |
| 980 |
|
| 981 |
def test_supplied_root_directory_no_steps(self): |
| 982 |
config = create_mock_config() |
| 983 |
config.paths = ["features"] |
| 984 |
config.verbose = True |
| 985 |
r = runner.Runner(config) |
| 986 |
|
| 987 |
fs = FsMock( |
| 988 |
"features/", |
| 989 |
"features/group1/", |
| 990 |
"features/group1/foo.feature", |
| 991 |
) |
| 992 |
|
| 993 |
with patch("os.path", fs): |
| 994 |
with patch("os.walk", fs.walk): |
| 995 |
with r.path_manager: |
| 996 |
assert_raises(ConfigError, r.setup_paths) |
| 997 |
|
| 998 |
ok_(("isdir", os.path.join(fs.base, "features", "steps")) in fs.calls) |
| 999 |
eq_(r.base_dir, None) |
| 1000 |
|
| 1001 |
|
| 1002 |
def test_supplied_feature_file(self): |
| 1003 |
config = create_mock_config() |
| 1004 |
config.paths = ["features/group1/foo.feature"] |
| 1005 |
config.verbose = True |
| 1006 |
r = runner.Runner(config) |
| 1007 |
r.context = Mock() |
| 1008 |
|
| 1009 |
fs = FsMock( |
| 1010 |
"features/", |
| 1011 |
"features/group1/", |
| 1012 |
"features/group1/foo.feature", |
| 1013 |
"features/steps/", |
| 1014 |
) |
| 1015 |
|
| 1016 |
with patch("os.path", fs): |
| 1017 |
with patch("os.walk", fs.walk): |
| 1018 |
with r.path_manager: |
| 1019 |
r.setup_paths() |
| 1020 |
|
| 1021 |
ok_(("isdir", os.path.join(fs.base, "features", "steps")) in fs.calls) |
| 1022 |
ok_(("isfile", os.path.join(fs.base, "features", "group1", "foo.feature")) in fs.calls) |
| 1023 |
eq_(r.base_dir, fs.join(fs.base, "features")) |
| 1024 |
|
| 1025 |
def test_supplied_feature_file_no_steps(self): |
| 1026 |
config = create_mock_config() |
| 1027 |
config.paths = ["features/group1/foo.feature"] |
| 1028 |
config.verbose = True |
| 1029 |
r = runner.Runner(config) |
| 1030 |
|
| 1031 |
fs = FsMock( |
| 1032 |
"features/", |
| 1033 |
"features/group1/", |
| 1034 |
"features/group1/foo.feature", |
| 1035 |
) |
| 1036 |
|
| 1037 |
with patch("os.path", fs): |
| 1038 |
with patch("os.walk", fs.walk): |
| 1039 |
with r.path_manager: |
| 1040 |
assert_raises(ConfigError, r.setup_paths) |
| 1041 |
|
| 1042 |
def test_supplied_feature_directory(self): |
| 1043 |
config = create_mock_config() |
| 1044 |
config.paths = ["features/group1"] |
| 1045 |
config.verbose = True |
| 1046 |
r = runner.Runner(config) |
| 1047 |
|
| 1048 |
fs = FsMock( |
| 1049 |
"features/", |
| 1050 |
"features/group1/", |
| 1051 |
"features/group1/foo.feature", |
| 1052 |
"features/steps/", |
| 1053 |
) |
| 1054 |
|
| 1055 |
with patch("os.path", fs): |
| 1056 |
with patch("os.walk", fs.walk): |
| 1057 |
with r.path_manager: |
| 1058 |
r.setup_paths() |
| 1059 |
|
| 1060 |
ok_(("isdir", os.path.join(fs.base, "features", "steps")) in fs.calls) |
| 1061 |
eq_(r.base_dir, os.path.join(fs.base, "features")) |
| 1062 |
|
| 1063 |
|
| 1064 |
def test_supplied_feature_directory_no_steps(self): |
| 1065 |
config = create_mock_config() |
| 1066 |
config.paths = ["features/group1"] |
| 1067 |
config.verbose = True |
| 1068 |
r = runner.Runner(config) |
| 1069 |
|
| 1070 |
fs = FsMock( |
| 1071 |
"features/", |
| 1072 |
"features/group1/", |
| 1073 |
"features/group1/foo.feature", |
| 1074 |
) |
| 1075 |
|
| 1076 |
with patch("os.path", fs): |
| 1077 |
with patch("os.walk", fs.walk): |
| 1078 |
assert_raises(ConfigError, r.setup_paths) |
| 1079 |
|
| 1080 |
ok_(("isdir", os.path.join(fs.base, "features", "steps")) in fs.calls) |
| 1081 |
|