| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
""" |
| 3 |
Unit tests for :mod:`behave.fixture` module. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import absolute_import, print_function |
| 7 |
import sys |
| 8 |
import inspect |
| 9 |
from behave.fixture import \ |
| 10 |
fixture, use_fixture, is_context_manager, InvalidFixtureError, \ |
| 11 |
use_fixture_by_tag, use_composite_fixture_with, fixture_call_params |
| 12 |
from behave.runner import Context, CleanupError, scoped_context_layer |
| 13 |
from behave._types import Unknown |
| 14 |
import pytest |
| •15 |
from unittest.mock import Mock |
| 16 |
import six |
| 17 |
|
| 18 |
|
| 19 |
# ----------------------------------------------------------------------------- |
| 20 |
# TEST SUPPORT |
| 21 |
# ----------------------------------------------------------------------------- |
| 22 |
def make_runtime_context(runner=None): |
| 23 |
"""Build a runtime/runner context for the tests here (partly faked). |
| 24 |
|
| 25 |
:return: Runtime context object (normally used by the runner). |
| 26 |
""" |
| 27 |
if runner is None: |
| 28 |
runner = Mock() |
| 29 |
runner.config = Mock() |
| 30 |
return Context(runner) |
| 31 |
|
| 32 |
|
| 33 |
class BasicFixture(object): |
| 34 |
"""Helper class used in behave fixtures (test-support).""" |
| 35 |
def __init__(self, *args, **kwargs): |
| 36 |
setup_called = kwargs.pop("_setup_called", True) |
| 37 |
name = kwargs.get("name", self.__class__.__name__) |
| 38 |
self.name = name |
| 39 |
self.args = args |
| 40 |
self.kwargs = kwargs.copy() |
| 41 |
self.setup_called = setup_called |
| 42 |
self.cleanup_called = False |
| 43 |
|
| 44 |
def __del__(self): |
| 45 |
if not self.cleanup_called: |
| 46 |
# print("LATE-CLEANUP (in dtor): %s" % self.name) |
| 47 |
self.cleanup() |
| 48 |
|
| 49 |
@classmethod |
| 50 |
def setup(cls, *args, **kwargs): |
| 51 |
name = kwargs.get("name", cls.__name__) |
| 52 |
print("%s.setup: %s, kwargs=%r" % (cls.__name__, name, kwargs)) |
| 53 |
kwargs["_setup_called"] = True |
| 54 |
return cls(*args, **kwargs) |
| 55 |
|
| 56 |
def cleanup(self): |
| 57 |
self.cleanup_called = True |
| 58 |
print("%s.cleanup: %s" % (self.__class__.__name__, self.name)) |
| 59 |
|
| 60 |
def __str__(self): |
| 61 |
args_text = ", ".join([str(arg) for arg in self.args]) |
| 62 |
kwargs_parts = ["%s= %s" % (key, value) |
| 63 |
for key, value in sorted(six.iteritems(self.kwargs))] |
| 64 |
kwargs_text = ", ".join(kwargs_parts) |
| 65 |
return "%s: args=%s; kwargs=%s" % (self.name, args_text, kwargs_text) |
| 66 |
|
| 67 |
|
| 68 |
class FooFixture(BasicFixture): pass |
| 69 |
class BarFixture(BasicFixture): pass |
| 70 |
|
| 71 |
# -- FIXTURE EXCEPTIONS: |
| 72 |
class FixtureSetupError(RuntimeError): pass |
| 73 |
class FixtureCleanupError(RuntimeError): pass |
| 74 |
|
| 75 |
|
| 76 |
# ----------------------------------------------------------------------------- |
| 77 |
# CUSTOM ASSERTIONS: |
| 78 |
# ----------------------------------------------------------------------------- |
| 79 |
def assert_context_setup(context, fixture_name, fixture_class=Unknown): |
| 80 |
"""Ensure that fixture object is stored in context.""" |
| 81 |
the_fixture = getattr(context, fixture_name, Unknown) |
| 82 |
assert hasattr(context, fixture_name) |
| 83 |
assert the_fixture is not Unknown |
| 84 |
if fixture_class is not Unknown: |
| 85 |
assert isinstance(the_fixture, fixture_class) |
| 86 |
|
| 87 |
def assert_context_cleanup(context, fixture_name): |
| 88 |
"""Ensure that fixture object is no longer stored in context.""" |
| 89 |
assert not hasattr(context, fixture_name) |
| 90 |
|
| 91 |
def assert_fixture_setup_called(fixture_obj): |
| 92 |
"""Ensure that fixture setup was performed.""" |
| 93 |
if hasattr(fixture_obj, "setup_called"): |
| 94 |
assert fixture_obj.setup_called |
| 95 |
|
| 96 |
def assert_fixture_cleanup_called(fixture_obj): |
| 97 |
if hasattr(fixture_obj, "cleanup_called"): |
| 98 |
assert fixture_obj.cleanup_called |
| 99 |
|
| 100 |
def assert_fixture_cleanup_not_called(fixture_obj): |
| 101 |
if hasattr(fixture_obj, "cleanup_called"): |
| 102 |
assert not fixture_obj.cleanup_called |
| 103 |
|
| 104 |
|
| 105 |
# ----------------------------------------------------------------------------- |
| 106 |
# TEST SUITE: |
| 107 |
# ----------------------------------------------------------------------------- |
| 108 |
class TestFixtureDecorator(object): |
| 109 |
def test_decorator(self): |
| 110 |
@fixture |
| 111 |
def foo(context, *args, **kwargs): |
| 112 |
pass |
| 113 |
|
| 114 |
assert foo.behave_fixture == True |
| 115 |
assert foo.name is None |
| 116 |
assert foo.pattern is None |
| 117 |
assert callable(foo) |
| 118 |
|
| 119 |
def test_decorator_call_without_params(self): |
| 120 |
@fixture() |
| 121 |
def bar0(context, *args, **kwargs): |
| 122 |
pass |
| 123 |
|
| 124 |
assert bar0.behave_fixture == True |
| 125 |
assert bar0.name is None |
| 126 |
assert bar0.pattern is None |
| 127 |
assert callable(bar0) |
| 128 |
|
| 129 |
def test_decorator_call_with_name(self): |
| 130 |
@fixture(name="fixture.bar2") |
| 131 |
def bar1(context, *args, **kwargs): |
| 132 |
pass |
| 133 |
|
| 134 |
assert bar1.behave_fixture == True |
| 135 |
assert bar1.name == "fixture.bar2" |
| 136 |
assert bar1.pattern is None |
| 137 |
assert callable(bar1) |
| 138 |
|
| 139 |
def test_decorated_generator_function_is_callable(self): |
| 140 |
# -- SIMILAR-TO: TestUseFixture.test_with_generator_func() |
| 141 |
@fixture |
| 142 |
def foo(context, *args, **kwargs): |
| 143 |
fixture_object = FooFixture.setup(*args, **kwargs) |
| 144 |
yield fixture_object |
| 145 |
fixture_object.cleanup() |
| 146 |
|
| 147 |
# -- ENSURE: Decorated fixture function can be called w/ params. |
| 148 |
context = None |
| 149 |
func_it = foo(context, 1, 2, 3, name="foo_1") |
| 150 |
|
| 151 |
# -- VERIFY: Expectations |
| 152 |
assert is_context_manager(foo) |
| 153 |
assert inspect.isfunction(foo) |
| 154 |
assert inspect.isgenerator(func_it) |
| 155 |
|
| 156 |
|
| 157 |
def test_decorated_function_is_callable(self): |
| 158 |
# -- SIMILAR-TO: TestUseFixture.test_with_function() |
| 159 |
@fixture(name="fixture.bar") |
| 160 |
def bar(context, *args, **kwargs): |
| 161 |
fixture_object = BarFixture.setup(*args, **kwargs) |
| 162 |
return fixture_object |
| 163 |
|
| 164 |
# -- ENSURE: Decorated fixture function can be called w/ params. |
| 165 |
context = None |
| 166 |
the_fixture = bar(context, 3, 2, 1, name="bar_1") |
| 167 |
|
| 168 |
# -- VERIFY: Expectations |
| 169 |
assert inspect.isfunction(bar) |
| 170 |
assert isinstance(the_fixture, BarFixture) |
| 171 |
|
| 172 |
def test_decorator_with_non_callable_raises_type_error(self): |
| 173 |
class NotCallable(object): pass |
| 174 |
|
| 175 |
with pytest.raises(TypeError) as exc_info: |
| 176 |
not_callable = NotCallable() |
| 177 |
bad_fixture = fixture(not_callable) # DECORATED_BY_HAND |
| 178 |
|
| 179 |
exception_text = str(exc_info.value) |
| 180 |
assert "Invalid func:" in exception_text |
| 181 |
assert "NotCallable object" in exception_text |
| 182 |
|
| 183 |
class TestUseFixture(object): |
| 184 |
|
| 185 |
def test_basic_lifecycle(self): |
| 186 |
# -- NOTE: Use explicit checks instead of assert-helper functions. |
| 187 |
@fixture |
| 188 |
def foo(context, checkpoints, *args, **kwargs): |
| 189 |
checkpoints.append("foo.setup") |
| 190 |
yield FooFixture() |
| 191 |
checkpoints.append("foo.cleanup") |
| 192 |
|
| 193 |
checkpoints = [] |
| 194 |
context = make_runtime_context() |
| 195 |
with scoped_context_layer(context): |
| 196 |
the_fixture = use_fixture(foo, context, checkpoints) |
| 197 |
# -- ENSURE: Fixture setup is performed (and as expected) |
| 198 |
assert isinstance(the_fixture, FooFixture) |
| 199 |
assert checkpoints == ["foo.setup"] |
| 200 |
checkpoints.append("scoped-block") |
| 201 |
|
| 202 |
# -- ENSURE: Fixture cleanup was performed |
| 203 |
assert checkpoints == ["foo.setup", "scoped-block", "foo.cleanup"] |
| 204 |
|
| 205 |
def test_fixture_with_args(self): |
| 206 |
"""Ensures that positional args are passed to fixture function.""" |
| 207 |
@fixture |
| 208 |
def foo(context, *args, **kwargs): |
| 209 |
fixture_object = FooFixture.setup(*args, **kwargs) |
| 210 |
context.foo = fixture_object |
| 211 |
yield fixture_object |
| 212 |
fixture_object.cleanup() |
| 213 |
|
| 214 |
context = make_runtime_context() |
| 215 |
with scoped_context_layer(context): |
| 216 |
the_fixture = use_fixture(foo, context, 1, 2, 3) |
| 217 |
|
| 218 |
expected_args = (1, 2, 3) |
| 219 |
assert the_fixture.args == expected_args |
| 220 |
|
| 221 |
def test_fixture_with_kwargs(self): |
| 222 |
"""Ensures that keyword args are passed to fixture function.""" |
| 223 |
@fixture |
| 224 |
def bar(context, *args, **kwargs): |
| 225 |
fixture_object = BarFixture.setup(*args, **kwargs) |
| 226 |
context.bar = fixture_object |
| 227 |
return fixture_object |
| 228 |
|
| 229 |
context = make_runtime_context() |
| 230 |
with scoped_context_layer(context): |
| 231 |
the_fixture = use_fixture(bar, context, name="bar", timeout=10) |
| 232 |
|
| 233 |
expected_kwargs = dict(name="bar", timeout=10) |
| 234 |
assert the_fixture.kwargs == expected_kwargs |
| 235 |
|
| 236 |
def test_with_generator_function(self): |
| 237 |
@fixture |
| 238 |
def foo(context, checkpoints, *args, **kwargs): |
| 239 |
checkpoints.append("foo.setup") |
| 240 |
fixture_object = FooFixture.setup(*args, **kwargs) |
| 241 |
context.foo = fixture_object |
| 242 |
yield fixture_object |
| 243 |
fixture_object.cleanup() |
| 244 |
checkpoints.append("foo.cleanup.done") |
| 245 |
|
| 246 |
checkpoints = [] |
| 247 |
context = make_runtime_context() |
| 248 |
with scoped_context_layer(context): |
| 249 |
the_fixture = use_fixture(foo, context, checkpoints) |
| 250 |
assert_context_setup(context, "foo", FooFixture) |
| 251 |
assert_fixture_setup_called(the_fixture) |
| 252 |
assert_fixture_cleanup_not_called(the_fixture) |
| 253 |
assert checkpoints == ["foo.setup"] |
| 254 |
print("Do something...") |
| 255 |
assert_context_cleanup(context, "foo") |
| 256 |
assert_fixture_cleanup_called(the_fixture) |
| 257 |
assert checkpoints == ["foo.setup", "foo.cleanup.done"] |
| 258 |
|
| 259 |
def test_with_function(self): |
| 260 |
@fixture |
| 261 |
def bar(context, checkpoints, *args, **kwargs): |
| 262 |
checkpoints.append("bar.setup") |
| 263 |
fixture_object = BarFixture.setup(*args, **kwargs) |
| 264 |
context.bar = fixture_object |
| 265 |
return fixture_object |
| 266 |
|
| 267 |
checkpoints = [] |
| 268 |
context = make_runtime_context() |
| 269 |
with scoped_context_layer(context): |
| 270 |
the_fixture = use_fixture(bar, context, checkpoints) |
| 271 |
assert_context_setup(context, "bar", BarFixture) |
| 272 |
assert_fixture_setup_called(the_fixture) |
| 273 |
assert_fixture_cleanup_not_called(the_fixture) |
| 274 |
assert checkpoints == ["bar.setup"] |
| 275 |
print("Do something...") |
| 276 |
assert_context_cleanup(context, "foo") |
| 277 |
assert_fixture_cleanup_not_called(the_fixture) |
| 278 |
# -- NOT: Normal functions have no fixture-cleanup part. |
| 279 |
|
| 280 |
def test_can_use_fixture_two_times(self): |
| 281 |
"""Ensures that a fixture can be used multiple times |
| 282 |
(with different names) within a context layer. |
| 283 |
""" |
| 284 |
@fixture |
| 285 |
def foo(context, checkpoints, *args, **kwargs): |
| 286 |
fixture_object = FooFixture.setup(*args, **kwargs) |
| 287 |
setattr(context, fixture_object.name, fixture_object) |
| 288 |
checkpoints.append("foo.setup:%s" % fixture_object.name) |
| 289 |
yield fixture_object |
| 290 |
checkpoints.append("foo.cleanup:%s" % fixture_object.name) |
| 291 |
fixture_object.cleanup() |
| 292 |
|
| 293 |
checkpoints = [] |
| 294 |
context = make_runtime_context() |
| 295 |
with scoped_context_layer(context): |
| 296 |
the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1") |
| 297 |
the_fixture2 = use_fixture(foo, context, checkpoints, name="foo_2") |
| 298 |
# -- VERIFY: Fixture and context setup was performed. |
| 299 |
assert checkpoints == ["foo.setup:foo_1", "foo.setup:foo_2"] |
| 300 |
assert context.foo_1 is the_fixture1 |
| 301 |
assert context.foo_2 is the_fixture2 |
| 302 |
assert the_fixture1 is not the_fixture2 |
| 303 |
|
| 304 |
checkpoints.append("scoped-block") |
| 305 |
|
| 306 |
# -- VERIFY: Fixture and context cleanup is performed. |
| 307 |
assert_context_cleanup(context, "foo_1") |
| 308 |
assert_context_cleanup(context, "foo_2") |
| 309 |
assert checkpoints == ["foo.setup:foo_1", "foo.setup:foo_2", |
| 310 |
"scoped-block", |
| 311 |
"foo.cleanup:foo_2", "foo.cleanup:foo_1"] |
| 312 |
# -- NOTE: Cleanups occur in reverse order. |
| 313 |
|
| 314 |
def test_invalid_fixture_function(self): |
| 315 |
"""Test invalid generator function with more than one yield-statement |
| 316 |
(not a valid fixture/context-manager). |
| 317 |
""" |
| 318 |
@fixture |
| 319 |
def invalid_fixture(context, checkpoints, *args, **kwargs): |
| 320 |
checkpoints.append("bad.setup") |
| 321 |
yield FooFixture(*args, **kwargs) |
| 322 |
checkpoints.append("bad.cleanup") |
| 323 |
yield None # -- SYNDROME HERE: More than one yield-statement |
| 324 |
|
| 325 |
# -- PERFORM-TEST: |
| 326 |
checkpoints = [] |
| 327 |
context = make_runtime_context() |
| 328 |
with pytest.raises(InvalidFixtureError): |
| 329 |
with scoped_context_layer(context): |
| 330 |
the_fixture = use_fixture(invalid_fixture, context, checkpoints) |
| 331 |
assert checkpoints == ["bad.setup"] |
| 332 |
checkpoints.append("scoped-block") |
| 333 |
|
| 334 |
# -- VERIFY: Ensure normal cleanup-parts were performed. |
| 335 |
assert checkpoints == ["bad.setup", "scoped-block", "bad.cleanup"] |
| 336 |
|
| 337 |
def test_bad_with_setup_error(self): |
| 338 |
# -- SAD: cleanup_fixture() part is called when setup-error occurs, |
| 339 |
# but not the cleanup-part of the generator (generator-magic). |
| 340 |
@fixture |
| 341 |
def bad_with_setup_error(context, checkpoints, *args, **kwargs): |
| 342 |
checkpoints.append("bad.setup_with_error") |
| 343 |
raise FixtureSetupError() |
| 344 |
yield FooFixture(*args, **kwargs) |
| 345 |
checkpoints.append("bad.cleanup:NOT_REACHED") |
| 346 |
|
| 347 |
# -- PERFORM-TEST: |
| 348 |
the_fixture = None |
| 349 |
checkpoints = [] |
| 350 |
context = make_runtime_context() |
| 351 |
bad_fixture = bad_with_setup_error |
| 352 |
with pytest.raises(FixtureSetupError): |
| 353 |
with scoped_context_layer(context): |
| 354 |
the_fixture = use_fixture(bad_fixture, context, checkpoints) |
| 355 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 356 |
|
| 357 |
# -- VERIFY: Ensure normal cleanup-parts were performed. |
| 358 |
# * SAD: fixture-cleanup is not called due to fixture-setup error. |
| 359 |
assert the_fixture is None # -- NEVER STORED: Due to setup error. |
| 360 |
assert checkpoints == ["bad.setup_with_error"] |
| 361 |
|
| 362 |
|
| 363 |
def test_bad_with_setup_error_aborts_on_first_error(self): |
| 364 |
@fixture |
| 365 |
def foo(context, checkpoints, *args, **kwargs): |
| 366 |
fixture_name = kwargs.get("name", "") |
| 367 |
checkpoints.append("foo.setup:%s" % fixture_name) |
| 368 |
yield FooFixture(*args, **kwargs) |
| 369 |
checkpoints.append("foo.cleanup:%s" % fixture_name) |
| 370 |
|
| 371 |
@fixture |
| 372 |
def bad_with_setup_error(context, checkpoints, *args, **kwargs): |
| 373 |
checkpoints.append("bad.setup_with_error") |
| 374 |
raise FixtureSetupError() |
| 375 |
yield FooFixture(*args, **kwargs) |
| 376 |
checkpoints.append("bad.cleanup:NOT_REACHED") |
| 377 |
|
| 378 |
# -- PERFORM-TEST: |
| 379 |
the_fixture1 = None |
| 380 |
the_fixture2 = None |
| 381 |
the_fixture3 = None |
| 382 |
checkpoints = [] |
| 383 |
context = make_runtime_context() |
| 384 |
bad_fixture = bad_with_setup_error |
| 385 |
with pytest.raises(FixtureSetupError): |
| 386 |
with scoped_context_layer(context): |
| 387 |
the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1") |
| 388 |
the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="BAD") |
| 389 |
the_fixture3 = use_fixture(foo, context, checkpoints, name="NOT_REACHED") |
| 390 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 391 |
|
| 392 |
# -- VERIFY: Ensure cleanup-parts were performed until failure-point. |
| 393 |
assert isinstance(the_fixture1, FooFixture) |
| 394 |
assert the_fixture2 is None # -- NEVER-STORED: Due to setup error. |
| 395 |
assert the_fixture3 is None # -- NEVER-CREATED: Due to bad_fixture. |
| 396 |
assert checkpoints == [ |
| 397 |
"foo.setup:foo_1", "bad.setup_with_error", "foo.cleanup:foo_1"] |
| 398 |
|
| 399 |
def test_bad_with_cleanup_error(self): |
| 400 |
@fixture |
| 401 |
def bad_with_cleanup_error(context, checkpoints, *args, **kwargs): |
| 402 |
checkpoints.append("bad.setup") |
| 403 |
yield FooFixture(*args, **kwargs) |
| 404 |
checkpoints.append("bad.cleanup_with_error") |
| 405 |
raise FixtureCleanupError() |
| 406 |
|
| 407 |
# -- PERFORM TEST: |
| 408 |
checkpoints = [] |
| 409 |
context = make_runtime_context() |
| 410 |
bad_fixture = bad_with_cleanup_error |
| 411 |
with pytest.raises(FixtureCleanupError): |
| 412 |
with scoped_context_layer(context): |
| 413 |
use_fixture(bad_fixture, context, checkpoints) |
| 414 |
checkpoints.append("scoped-block") |
| 415 |
|
| 416 |
# -- VERIFY: Ensure normal cleanup-parts were performed or tried. |
| 417 |
assert checkpoints == ["bad.setup", "scoped-block", "bad.cleanup_with_error"] |
| 418 |
|
| 419 |
def test_bad_with_cleanup_error_performs_all_cleanups(self): |
| 420 |
@fixture |
| 421 |
def foo(context, checkpoints, *args, **kwargs): |
| 422 |
fixture_name = kwargs.get("name", "") |
| 423 |
checkpoints.append("foo.setup:%s" % fixture_name) |
| 424 |
yield FooFixture(*args, **kwargs) |
| 425 |
checkpoints.append("foo.cleanup:%s" % fixture_name) |
| 426 |
|
| 427 |
@fixture |
| 428 |
def bad_with_cleanup_error(context, checkpoints, *args, **kwargs): |
| 429 |
checkpoints.append("bad.setup") |
| 430 |
yield FooFixture(*args, **kwargs) |
| 431 |
checkpoints.append("bad.cleanup_with_error") |
| 432 |
raise FixtureCleanupError() |
| 433 |
checkpoints.append("bad.cleanup.done:NOT_REACHED") |
| 434 |
|
| 435 |
# -- PERFORM TEST: |
| 436 |
the_fixture1 = None |
| 437 |
the_fixture2 = None |
| 438 |
the_fixture3 = None |
| 439 |
checkpoints = [] |
| 440 |
context = make_runtime_context() |
| 441 |
bad_fixture = bad_with_cleanup_error |
| 442 |
with pytest.raises(FixtureCleanupError): |
| 443 |
with scoped_context_layer(context): |
| 444 |
the_fixture1 = use_fixture(foo, context, checkpoints, name="foo_1") |
| 445 |
the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="BAD") |
| 446 |
the_fixture3 = use_fixture(foo, context, checkpoints, name="foo_3") |
| 447 |
checkpoints.append("scoped-block") |
| 448 |
|
| 449 |
# -- VERIFY: Tries to perform all cleanups even when cleanup-error(s) occur. |
| 450 |
assert checkpoints == [ |
| 451 |
"foo.setup:foo_1", "bad.setup", "foo.setup:foo_3", |
| 452 |
"scoped-block", |
| 453 |
"foo.cleanup:foo_3", "bad.cleanup_with_error", "foo.cleanup:foo_1"] |
| 454 |
|
| 455 |
|
| 456 |
def test_bad_with_setup_and_cleanup_error(self): |
| 457 |
# -- GOOD: cleanup_fixture() part is called when setup-error occurs. |
| 458 |
# BUT: FixtureSetupError is hidden by FixtureCleanupError |
| 459 |
@fixture |
| 460 |
def bad_with_setup_and_cleanup_error(context, checkpoints, *args, **kwargs): |
| 461 |
def cleanup_bad_with_error(): |
| 462 |
checkpoints.append("bad.cleanup_with_error") |
| 463 |
raise FixtureCleanupError() |
| 464 |
|
| 465 |
checkpoints.append("bad.setup_with_error") |
| 466 |
context.add_cleanup(cleanup_bad_with_error) |
| 467 |
raise FixtureSetupError() |
| 468 |
return FooFixture("NOT_REACHED") |
| 469 |
|
| 470 |
# -- PERFORM TEST: |
| 471 |
checkpoints = [] |
| 472 |
context = make_runtime_context() |
| 473 |
bad_fixture = bad_with_setup_and_cleanup_error |
| 474 |
with pytest.raises(FixtureCleanupError) as exc_info: |
| 475 |
with scoped_context_layer(context): |
| 476 |
use_fixture(bad_fixture, context, checkpoints, name="BAD2") |
| 477 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 478 |
|
| 479 |
# -- VERIFY: Ensure normal cleanup-parts were performed or tried. |
| 480 |
# OOPS: FixtureCleanupError in fixture-cleanup hides FixtureSetupError |
| 481 |
assert checkpoints == ["bad.setup_with_error", "bad.cleanup_with_error"] |
| 482 |
assert isinstance(exc_info.value, FixtureCleanupError), "LAST-EXCEPTION-WINS" |
| 483 |
|
| 484 |
|
| 485 |
class TestUseFixtureByTag(object): |
| 486 |
def test_data_schema1(self): |
| 487 |
@fixture |
| 488 |
def foo(context, *args, **kwargs): |
| 489 |
# -- NOTE checkpoints: Injected from outer scope. |
| 490 |
checkpoints.append("foo.setup") |
| 491 |
yield "fixture:foo" |
| 492 |
checkpoints.append("foo.cleanup") |
| 493 |
|
| 494 |
fixture_registry = { |
| 495 |
"fixture.foo": foo, |
| 496 |
} |
| 497 |
|
| 498 |
# -- PERFORM-TEST: |
| 499 |
context = make_runtime_context() |
| 500 |
checkpoints = [] |
| 501 |
with scoped_context_layer(context): |
| 502 |
use_fixture_by_tag("fixture.foo", context, fixture_registry) |
| 503 |
checkpoints.append("scoped-block") |
| 504 |
|
| 505 |
# -- VERIFY: |
| 506 |
assert checkpoints == [ |
| 507 |
"foo.setup", "scoped-block", "foo.cleanup" |
| 508 |
] |
| 509 |
|
| 510 |
def test_data_schema2(self): |
| 511 |
@fixture |
| 512 |
def foo(context, *args, **kwargs): |
| 513 |
# -- NOTE checkpoints: Injected from outer scope. |
| 514 |
params = "%r, %r" % (args, kwargs) |
| 515 |
checkpoints.append("foo.setup: %s" % params) |
| 516 |
yield "fixture.foo" |
| 517 |
checkpoints.append("foo.cleanup: %s" % params) |
| 518 |
|
| 519 |
fixture_registry = { |
| 520 |
"fixture.foo": fixture_call_params(foo, 1, 2, 3, name="foo_1") |
| 521 |
} |
| 522 |
|
| 523 |
# -- PERFORM-TEST: |
| 524 |
context = make_runtime_context() |
| 525 |
checkpoints = [] |
| 526 |
with scoped_context_layer(context): |
| 527 |
use_fixture_by_tag("fixture.foo", context, fixture_registry) |
| 528 |
checkpoints.append("scoped-block") |
| 529 |
|
| 530 |
# -- VERIFY: |
| 531 |
assert checkpoints == [ |
| 532 |
"foo.setup: (1, 2, 3), {'name': 'foo_1'}", |
| 533 |
"scoped-block", |
| 534 |
"foo.cleanup: (1, 2, 3), {'name': 'foo_1'}", |
| 535 |
] |
| 536 |
|
| 537 |
def test_unknown_fixture_raises_lookup_error(self): |
| 538 |
fixture_registry = {} |
| 539 |
|
| 540 |
# -- PERFORM-TEST: |
| 541 |
context = make_runtime_context() |
| 542 |
with pytest.raises(LookupError) as exc_info: |
| 543 |
with scoped_context_layer(context): |
| 544 |
use_fixture_by_tag("UNKNOWN_FIXTURE", context, fixture_registry) |
| 545 |
|
| 546 |
# -- VERIFY: |
| 547 |
assert "Unknown fixture-tag: UNKNOWN_FIXTURE" in str(exc_info.value) |
| 548 |
|
| 549 |
def test_invalid_data_schema_raises_value_error(self): |
| 550 |
@fixture |
| 551 |
def foo(context, *args, **kwargs): |
| 552 |
pass |
| 553 |
|
| 554 |
class BadFixtureData(object): |
| 555 |
def __init__(self, fixture_func, *args, **kwargs): |
| 556 |
self.fixture_func = fixture_func |
| 557 |
self.fixture_args = args |
| 558 |
self.fixture_kwargs = kwargs |
| 559 |
|
| 560 |
fixture_registry = { |
| 561 |
"fixture.foo": BadFixtureData(foo, 1, 2, 3, name="foo_1") |
| 562 |
} |
| 563 |
|
| 564 |
# -- PERFORM-TEST: |
| 565 |
context = make_runtime_context() |
| 566 |
with pytest.raises(ValueError) as exc_info: |
| 567 |
with scoped_context_layer(context): |
| 568 |
use_fixture_by_tag("fixture.foo", context, fixture_registry) |
| 569 |
|
| 570 |
# -- VERIFY: |
| 571 |
expected = "fixture_data: Expected tuple or fixture-func, but is:" |
| 572 |
assert expected in str(exc_info.value) |
| 573 |
assert "BadFixtureData object" in str(exc_info.value) |
| 574 |
|
| 575 |
|
| 576 |
class TestCompositeFixture(object): |
| 577 |
def test_use_fixture(self): |
| 578 |
@fixture |
| 579 |
def fixture_foo(context, checkpoints, *args, **kwargs): |
| 580 |
fixture_name = kwargs.get("name", "foo") |
| 581 |
checkpoints.append("foo.setup:%s" % fixture_name) |
| 582 |
yield |
| 583 |
checkpoints.append("foo.cleanup:%s" % fixture_name) |
| 584 |
|
| 585 |
@fixture |
| 586 |
def composite2(context, checkpoints, *args, **kwargs): |
| 587 |
the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1") |
| 588 |
the_fixture2 = use_fixture(fixture_foo, context, checkpoints, name="_2") |
| 589 |
return (the_fixture1, the_fixture2) |
| 590 |
|
| 591 |
# -- PERFORM-TEST: |
| 592 |
context = make_runtime_context() |
| 593 |
checkpoints = [] |
| 594 |
with scoped_context_layer(context): |
| 595 |
use_fixture(composite2, context, checkpoints) |
| 596 |
checkpoints.append("scoped-block") |
| 597 |
|
| 598 |
assert checkpoints == [ |
| 599 |
"foo.setup:_1", "foo.setup:_2", |
| 600 |
"scoped-block", |
| 601 |
"foo.cleanup:_2", "foo.cleanup:_1", |
| 602 |
] |
| 603 |
|
| 604 |
def test_use_fixture_with_setup_error(self): |
| 605 |
@fixture |
| 606 |
def fixture_foo(context, checkpoints, *args, **kwargs): |
| 607 |
fixture_name = kwargs.get("name", "foo") |
| 608 |
checkpoints.append("foo.setup:%s" % fixture_name) |
| 609 |
yield |
| 610 |
checkpoints.append("foo.cleanup:%s" % fixture_name) |
| 611 |
|
| 612 |
@fixture |
| 613 |
def bad_with_setup_error(context, checkpoints, *args, **kwargs): |
| 614 |
checkpoints.append("bad.setup_with_error") |
| 615 |
raise FixtureSetupError("OOPS") |
| 616 |
yield |
| 617 |
checkpoints.append("bad.cleanup:NOT_REACHED") |
| 618 |
|
| 619 |
@fixture |
| 620 |
def composite3(context, checkpoints, *args, **kwargs): |
| 621 |
bad_fixture = bad_with_setup_error |
| 622 |
the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1") |
| 623 |
the_fixture2 = use_fixture(bad_fixture, context, checkpoints, name="OOPS") |
| 624 |
the_fixture3 = use_fixture(fixture_foo, context, checkpoints, |
| 625 |
name="_3:NOT_REACHED") |
| 626 |
return (the_fixture1, the_fixture2, the_fixture3) # NOT_REACHED |
| 627 |
|
| 628 |
# -- PERFORM-TEST: |
| 629 |
context = make_runtime_context() |
| 630 |
checkpoints = [] |
| 631 |
with pytest.raises(FixtureSetupError): |
| 632 |
with scoped_context_layer(context): |
| 633 |
use_fixture(composite3, context, checkpoints) |
| 634 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 635 |
|
| 636 |
# -- ENSURES: |
| 637 |
# * fixture1-cleanup is called even after fixture2-setup-error |
| 638 |
# * fixture3-setup/cleanup are not called due to fixture2-setup-error |
| 639 |
assert checkpoints == [ |
| 640 |
"foo.setup:_1", "bad.setup_with_error", "foo.cleanup:_1" |
| 641 |
] |
| 642 |
|
| 643 |
def test_use_fixture_with_block_error(self): |
| 644 |
@fixture |
| 645 |
def fixture_foo(context, checkpoints, *args, **kwargs): |
| 646 |
fixture_name = kwargs.get("name", "foo") |
| 647 |
checkpoints.append("foo.setup:%s" % fixture_name) |
| 648 |
yield |
| 649 |
checkpoints.append("foo.cleanup:%s" % fixture_name) |
| 650 |
|
| 651 |
@fixture |
| 652 |
def composite2(context, checkpoints, *args, **kwargs): |
| 653 |
the_fixture1 = use_fixture(fixture_foo, context, checkpoints, name="_1") |
| 654 |
the_fixture2 = use_fixture(fixture_foo, context, checkpoints, name="_2") |
| 655 |
return (the_fixture1, the_fixture2) |
| 656 |
|
| 657 |
# -- PERFORM-TEST: |
| 658 |
context = make_runtime_context() |
| 659 |
checkpoints = [] |
| 660 |
with pytest.raises(RuntimeError): |
| 661 |
with scoped_context_layer(context): |
| 662 |
use_fixture(composite2, context, checkpoints) |
| 663 |
checkpoints.append("scoped-block_with_error") |
| 664 |
raise RuntimeError("OOPS") |
| 665 |
checkpoints.append("scoped-block.done:NOT_REACHED") |
| 666 |
|
| 667 |
# -- ENSURES: |
| 668 |
# * fixture1-cleanup/cleanup is called even scoped-block-error |
| 669 |
# * fixture2-cleanup/cleanup is called even scoped-block-error |
| 670 |
# * fixture-cleanup occurs in reversed setup-order |
| 671 |
assert checkpoints == [ |
| 672 |
"foo.setup:_1", "foo.setup:_2", |
| 673 |
"scoped-block_with_error", |
| 674 |
"foo.cleanup:_2", "foo.cleanup:_1" |
| 675 |
] |
| 676 |
|
| 677 |
|
| 678 |
def test_use_composite_fixture(self): |
| 679 |
@fixture |
| 680 |
def fixture_foo(context, checkpoints, *args, **kwargs): |
| 681 |
fixture_name = kwargs.get("name", "foo") |
| 682 |
checkpoints.append("foo.setup:%s" % fixture_name) |
| 683 |
yield |
| 684 |
checkpoints.append("foo.cleanup:%s" % fixture_name) |
| 685 |
|
| 686 |
@fixture |
| 687 |
def composite2(context, checkpoints, *args, **kwargs): |
| 688 |
the_composite = use_composite_fixture_with(context, [ |
| 689 |
fixture_call_params(fixture_foo, checkpoints, name="_1"), |
| 690 |
fixture_call_params(fixture_foo, checkpoints, name="_2"), |
| 691 |
]) |
| 692 |
return the_composite |
| 693 |
|
| 694 |
# -- PERFORM-TEST: |
| 695 |
context = make_runtime_context() |
| 696 |
checkpoints = [] |
| 697 |
with scoped_context_layer(context): |
| 698 |
use_fixture(composite2, context, checkpoints) |
| 699 |
checkpoints.append("scoped-block") |
| 700 |
|
| 701 |
assert checkpoints == [ |
| 702 |
"foo.setup:_1", "foo.setup:_2", |
| 703 |
"scoped-block", |
| 704 |
"foo.cleanup:_2", "foo.cleanup:_1", |
| 705 |
] |
| 706 |
|
| 707 |
def test_use_composite_fixture_with_setup_error(self): |
| 708 |
@fixture |
| 709 |
def fixture_foo(context, checkpoints, *args, **kwargs): |
| 710 |
fixture_name = kwargs.get("name", "foo") |
| 711 |
checkpoints.append("foo.setup:%s" % fixture_name) |
| 712 |
yield |
| 713 |
checkpoints.append("foo.cleanup:%s" % fixture_name) |
| 714 |
|
| 715 |
@fixture |
| 716 |
def bad_with_setup_error(context, checkpoints, *args, **kwargs): |
| 717 |
checkpoints.append("bad.setup_with_error") |
| 718 |
raise FixtureSetupError("OOPS") |
| 719 |
yield |
| 720 |
checkpoints.append("bad.cleanup:NOT_REACHED") |
| 721 |
|
| 722 |
@fixture |
| 723 |
def composite3(context, checkpoints, *args, **kwargs): |
| 724 |
bad_fixture = bad_with_setup_error |
| 725 |
the_composite = use_composite_fixture_with(context, [ |
| 726 |
fixture_call_params(fixture_foo, checkpoints, name="_1"), |
| 727 |
fixture_call_params(bad_fixture, checkpoints, name="OOPS"), |
| 728 |
fixture_call_params(fixture_foo, checkpoints, name="_3:NOT_REACHED"), |
| 729 |
]) |
| 730 |
return the_composite |
| 731 |
|
| 732 |
# -- PERFORM-TEST: |
| 733 |
context = make_runtime_context() |
| 734 |
checkpoints = [] |
| 735 |
with pytest.raises(FixtureSetupError): |
| 736 |
with scoped_context_layer(context): |
| 737 |
use_fixture(composite3, context, checkpoints) |
| 738 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 739 |
|
| 740 |
# -- ENSURES: |
| 741 |
# * fixture1-cleanup is called even after fixture2-setup-error |
| 742 |
# * fixture3-setup/cleanup are not called due to fixture2-setup-error |
| 743 |
assert checkpoints == [ |
| 744 |
"foo.setup:_1", "bad.setup_with_error", "foo.cleanup:_1" |
| 745 |
] |
| 746 |
|
| 747 |
def test_use_composite_fixture_with_block_error(self): |
| 748 |
@fixture |
| 749 |
def fixture_foo(context, checkpoints, *args, **kwargs): |
| 750 |
fixture_name = kwargs.get("name", "foo") |
| 751 |
checkpoints.append("foo.setup:%s" % fixture_name) |
| 752 |
yield |
| 753 |
checkpoints.append("foo.cleanup:%s" % fixture_name) |
| 754 |
|
| 755 |
@fixture |
| 756 |
def composite2(context, checkpoints, *args, **kwargs): |
| 757 |
the_composite = use_composite_fixture_with(context, [ |
| 758 |
fixture_call_params(fixture_foo, checkpoints, name="_1"), |
| 759 |
fixture_call_params(fixture_foo, checkpoints, name="_2"), |
| 760 |
]) |
| 761 |
return the_composite |
| 762 |
|
| 763 |
# -- PERFORM-TEST: |
| 764 |
checkpoints = [] |
| 765 |
context = make_runtime_context() |
| 766 |
with pytest.raises(RuntimeError): |
| 767 |
with scoped_context_layer(context): |
| 768 |
use_fixture(composite2, context, checkpoints) |
| 769 |
checkpoints.append("scoped-block_with_error") |
| 770 |
raise RuntimeError("OOPS") |
| 771 |
checkpoints.append("scoped-block.done:NOT_REACHED") |
| 772 |
|
| 773 |
# -- ENSURES: |
| 774 |
# * fixture1-cleanup/cleanup is called even scoped-block-error |
| 775 |
# * fixture2-cleanup/cleanup is called even scoped-block-error |
| 776 |
# * fixture-cleanup occurs in reversed setup-order |
| 777 |
assert checkpoints == [ |
| 778 |
"foo.setup:_1", "foo.setup:_2", |
| 779 |
"scoped-block_with_error", |
| 780 |
"foo.cleanup:_2", "foo.cleanup:_1" |
| 781 |
] |
| 782 |
|
| 783 |
# -- BAD-EXAMPLE: Ensure SIMPLISTIC-COMPOSITE works as expected |
| 784 |
def test_simplistic_composite_with_setup_error_skips_cleanup(self): |
| 785 |
def setup_bad_fixture_with_error(text): |
| 786 |
raise FixtureSetupError("OOPS") |
| 787 |
|
| 788 |
@fixture |
| 789 |
def sad_composite2(context, checkpoints, *args, **kwargs): |
| 790 |
checkpoints.append("foo.setup:_1") |
| 791 |
the_fixture1 = FooFixture.setup(*args, **kwargs) |
| 792 |
checkpoints.append("bad.setup_with_error") |
| 793 |
the_fixture2 = setup_bad_fixture_with_error(text="OOPS") |
| 794 |
checkpoints.append("bad.setup.done:NOT_REACHED") |
| 795 |
yield (the_fixture1, the_fixture2) |
| 796 |
checkpoints.append("foo.cleanup:_1:NOT_REACHED") |
| 797 |
|
| 798 |
# -- PERFORM-TEST: |
| 799 |
context = make_runtime_context() |
| 800 |
checkpoints = [] |
| 801 |
with pytest.raises(FixtureSetupError): |
| 802 |
with scoped_context_layer(context): |
| 803 |
use_fixture(sad_composite2, context, checkpoints) |
| 804 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 805 |
|
| 806 |
# -- VERIFY: |
| 807 |
# * SAD: fixture1-cleanup is not called after fixture2-setup-error |
| 808 |
assert checkpoints == ["foo.setup:_1", "bad.setup_with_error"] |
| 809 |
|
| 810 |
def test_simplistic_composite_with_block_error_performs_cleanup(self): |
| 811 |
def setup_bad_fixture_with_error(text): |
| 812 |
raise FixtureSetupError("OOPS") |
| 813 |
|
| 814 |
@fixture |
| 815 |
def simplistic_composite2(context, checkpoints, *args, **kwargs): |
| 816 |
checkpoints.append("foo.setup:_1") |
| 817 |
the_fixture1 = FooFixture.setup(*args, name="_1") |
| 818 |
checkpoints.append("foo.setup:_2") |
| 819 |
the_fixture2 = FooFixture.setup(*args, name="_2") |
| 820 |
yield (the_fixture1, the_fixture2) |
| 821 |
checkpoints.append("foo.cleanup:_1") |
| 822 |
the_fixture1.cleanup() |
| 823 |
checkpoints.append("foo.cleanup:_2") |
| 824 |
the_fixture2.cleanup() |
| 825 |
|
| 826 |
# -- PERFORM-TEST: |
| 827 |
context = make_runtime_context() |
| 828 |
checkpoints = [] |
| 829 |
with pytest.raises(RuntimeError): |
| 830 |
with scoped_context_layer(context): |
| 831 |
use_fixture(simplistic_composite2, context, checkpoints) |
| 832 |
checkpoints.append("scoped-block_with_error") |
| 833 |
raise RuntimeError("OOPS") |
| 834 |
checkpoints.append("scoped-block.end:NOT_REACHED") |
| 835 |
|
| 836 |
# -- VERIFY: |
| 837 |
# * fixture1-setup/cleanup is called when block-error occurs |
| 838 |
# * fixture2-setup/cleanup is called when block-error occurs |
| 839 |
# * fixture-cleanups occurs in specified composite-cleanup order |
| 840 |
assert checkpoints == [ |
| 841 |
"foo.setup:_1", "foo.setup:_2", |
| 842 |
"scoped-block_with_error", |
| 843 |
"foo.cleanup:_1", "foo.cleanup:_2" |
| 844 |
] |
| 845 |
|
| 846 |
|
| 847 |
class TestFixtureCleanup(object): |
| 848 |
"""Check fixture-cleanup behaviour w/ different fixture implementations.""" |
| 849 |
|
| 850 |
def test_setup_eror_with_plaingen_then_cleanup_is_not_called(self): |
| 851 |
# -- CASE: Fixture is generator-function |
| 852 |
@fixture |
| 853 |
def foo(context, checkpoints): |
| 854 |
checkpoints.append("foo.setup.begin") |
| 855 |
raise FixtureSetupError("foo") |
| 856 |
checkpoints.append("foo.setup.done:NOT_REACHED") |
| 857 |
yield |
| 858 |
checkpoints.append("foo.cleanup:NOT_REACHED") |
| 859 |
|
| 860 |
checkpoints = [] |
| 861 |
context = make_runtime_context() |
| 862 |
with pytest.raises(FixtureSetupError): |
| 863 |
with scoped_context_layer(context): |
| 864 |
use_fixture(foo, context, checkpoints) |
| 865 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 866 |
|
| 867 |
assert checkpoints == ["foo.setup.begin"] |
| 868 |
|
| 869 |
def test_setup_eror_with_finallygen_then_cleanup_is_called(self): |
| 870 |
# -- CASE: Fixture is generator-function |
| 871 |
@fixture |
| 872 |
def foo(context, checkpoints): |
| 873 |
try: |
| 874 |
checkpoints.append("foo.setup.begin") |
| 875 |
raise FixtureSetupError("foo") |
| 876 |
checkpoints.append("foo.setup.done:NOT_REACHED") |
| 877 |
yield |
| 878 |
checkpoints.append("foo.cleanup:NOT_REACHED") |
| 879 |
finally: |
| 880 |
checkpoints.append("foo.cleanup.finally") |
| 881 |
|
| 882 |
checkpoints = [] |
| 883 |
context = make_runtime_context() |
| 884 |
with pytest.raises(FixtureSetupError): |
| 885 |
with scoped_context_layer(context): |
| 886 |
use_fixture(foo, context, checkpoints) |
| 887 |
# -- NEVER-REACHED: |
| 888 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 889 |
|
| 890 |
# -- ENSURE: fixture-cleanup (foo.cleanup) is called (EARLY-CLEANUP). |
| 891 |
assert checkpoints == ["foo.setup.begin", "foo.cleanup.finally"] |
| 892 |
|
| 893 |
|
| 894 |
def test_setup_error_with_context_cleanup1_then_cleanup_is_called(self): |
| 895 |
# -- CASE: Fixture is normal function |
| 896 |
@fixture |
| 897 |
def foo(context, checkpoints): |
| 898 |
def cleanup_foo(arg=""): |
| 899 |
checkpoints.append("cleanup_foo:%s" % arg) |
| 900 |
|
| 901 |
checkpoints.append("foo.setup_with_error:foo_1") |
| 902 |
context.add_cleanup(cleanup_foo, "foo_1") |
| 903 |
raise FixtureSetupError("foo") |
| 904 |
checkpoints.append("foo.setup.done:NOT_REACHED") |
| 905 |
|
| 906 |
checkpoints = [] |
| 907 |
context = make_runtime_context() |
| 908 |
with pytest.raises(FixtureSetupError): |
| 909 |
with scoped_context_layer(context): |
| 910 |
use_fixture(foo, context, checkpoints) |
| 911 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 912 |
|
| 913 |
# -- ENSURE: cleanup_foo() is called (LATE-CLEANUP on scope-exit) |
| 914 |
assert checkpoints == ["foo.setup_with_error:foo_1", "cleanup_foo:foo_1"] |
| 915 |
|
| 916 |
def test_setup_error_with_context_cleanup2_then_cleanup_is_called(self): |
| 917 |
# -- CASE: Fixture is generator-function (contextmanager) |
| 918 |
# NOTE: Explicit use of context.add_cleanup() |
| 919 |
@fixture |
| 920 |
def foo(context, checkpoints, **kwargs): |
| 921 |
def cleanup_foo(arg=""): |
| 922 |
checkpoints.append("cleanup_foo:%s" % arg) |
| 923 |
|
| 924 |
checkpoints.append("foo.setup_with_error:foo_1") |
| 925 |
context.add_cleanup(cleanup_foo, "foo_1") |
| 926 |
raise FixtureSetupError("foo_1") |
| 927 |
checkpoints.append("foo.setup.done:NOT_REACHED") |
| 928 |
yield |
| 929 |
checkpoints.append("foo.cleanup:NOT_REACHED") |
| 930 |
|
| 931 |
checkpoints = [] |
| 932 |
context = make_runtime_context() |
| 933 |
with pytest.raises(FixtureSetupError): |
| 934 |
with scoped_context_layer(context): |
| 935 |
use_fixture(foo, context, checkpoints) |
| 936 |
checkpoints.append("scoped-block:NOT_REACHED") |
| 937 |
|
| 938 |
# -- ENSURE: cleanup_foo() is called |
| 939 |
assert checkpoints == ["foo.setup_with_error:foo_1", "cleanup_foo:foo_1"] |
| 940 |
|
| 941 |
def test_block_eror_with_plaingen_then_cleanup_is_called(self): |
| 942 |
# -- CASE: Fixture is generator-function |
| 943 |
@fixture |
| 944 |
def foo(context, checkpoints): |
| 945 |
checkpoints.append("foo.setup") |
| 946 |
yield |
| 947 |
checkpoints.append("foo.cleanup") |
| 948 |
|
| 949 |
checkpoints = [] |
| 950 |
context = make_runtime_context() |
| 951 |
with pytest.raises(RuntimeError): |
| 952 |
with scoped_context_layer(context): |
| 953 |
use_fixture(foo, context, checkpoints) |
| 954 |
checkpoints.append("scoped-block_with_error") |
| 955 |
raise RuntimeError("scoped-block") |
| 956 |
checkpoints.append("NOT_REACHED") |
| 957 |
|
| 958 |
# -- ENSURE: |
| 959 |
assert checkpoints == [ |
| 960 |
"foo.setup", "scoped-block_with_error", "foo.cleanup" |
| 961 |
] |
| 962 |
|
| 963 |
def test_block_eror_with_context_cleanup_then_cleanup_is_called(self): |
| 964 |
# -- CASE: Fixture is normal-function |
| 965 |
@fixture |
| 966 |
def bar(context, checkpoints): |
| 967 |
def cleanup_bar(): |
| 968 |
checkpoints.append("cleanup_bar") |
| 969 |
|
| 970 |
checkpoints.append("bar.setup") |
| 971 |
context.add_cleanup(cleanup_bar) |
| 972 |
|
| 973 |
checkpoints = [] |
| 974 |
context = make_runtime_context() |
| 975 |
with pytest.raises(RuntimeError): |
| 976 |
with scoped_context_layer(context): |
| 977 |
use_fixture(bar, context, checkpoints) |
| 978 |
checkpoints.append("scoped-block_with_error") |
| 979 |
raise RuntimeError("scoped-block") |
| 980 |
checkpoints.append("NOT_REACHED") |
| 981 |
|
| 982 |
# -- ENSURE: |
| 983 |
assert checkpoints == [ |
| 984 |
"bar.setup", "scoped-block_with_error", "cleanup_bar" |
| 985 |
] |