1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
from logging import getLogger, ERROR, Filter, shutdown
from textwrap import dedent
from unittest import TestCase
from warnings import catch_warnings
from testfixtures import Replacer, LogCapture, compare, Replace, ShouldWarn
from testfixtures.mock import Mock
from testfixtures.shouldraise import ShouldAssert
root = getLogger()
one = getLogger('one')
two = getLogger('two')
child = getLogger('one.child')
class DummyFilter(Filter):
def filter(self, _):
return True
class TestLogCapture(TestCase):
def test_simple(self):
root.info('before')
l = LogCapture()
root.info('during')
l.uninstall()
root.info('after')
assert str(l) == "root INFO\n during"
def test_simple_strict(self):
log_capture = LogCapture(ensure_checks_above=ERROR)
root.error('during')
log_capture.uninstall()
with ShouldAssert("Not asserted ERROR log(s): [('root', 'ERROR', 'during')]"):
log_capture.ensure_checked()
def test_simple_strict_re_defaulted(self):
with Replace('testfixtures.LogCapture.default_ensure_checks_above', ERROR):
LogCapture.default_ensure_checks_above = ERROR
log_capture = LogCapture()
root.error('during')
log_capture.uninstall()
with ShouldAssert("Not asserted ERROR log(s): [('root', 'ERROR', 'during')]"):
log_capture.ensure_checked()
def test_simple_strict_asserted_by_check(self):
log_capture = LogCapture(ensure_checks_above=ERROR)
root.error('during')
log_capture.uninstall()
log_capture.check(("root", "ERROR", "during"))
log_capture.ensure_checked()
def test_simple_strict_asserted_by_check_present_ordered(self):
log_capture = LogCapture(ensure_checks_above=ERROR)
root.error('during')
log_capture.uninstall()
log_capture.check_present(("root", "ERROR", "during"))
log_capture.ensure_checked()
def test_simple_strict_asserted_by_check_present_unordered(self):
log_capture = LogCapture(ensure_checks_above=ERROR)
root.error('during')
log_capture.uninstall()
log_capture.check_present(("root", "ERROR", "during"), order_matters=False)
log_capture.ensure_checked()
def test_simple_strict_not_asserted_by_check_present(self):
log_capture = LogCapture(ensure_checks_above=ERROR)
root.error('before')
root.error('during')
log_capture.uninstall()
log_capture.check_present(("root", "ERROR", "during"))
with ShouldAssert("Not asserted ERROR log(s): [('root', 'ERROR', 'before')]"):
log_capture.ensure_checked()
def test_simple_strict_asserted_by_containment(self):
log_capture = LogCapture(ensure_checks_above=ERROR)
root.error('during')
log_capture.uninstall()
assert ("root", "ERROR", "during") in log_capture
assert ("root", "INFO", "during") not in log_capture
log_capture.ensure_checked()
def test_simple_strict_asserted_by_mark_all_checked(self):
log_capture = LogCapture(ensure_checks_above=ERROR)
root.error('during')
log_capture.uninstall()
log_capture.mark_all_checked()
log_capture.ensure_checked()
def test_simple_strict_ctx(self):
with ShouldAssert("Not asserted ERROR log(s): [('root', 'ERROR', 'during')]"):
with LogCapture(ensure_checks_above=ERROR):
root.error('during')
def test_simple_strict_asserted_ctx(self):
with LogCapture(ensure_checks_above=ERROR) as log_capture:
root.error('during')
log_capture.check(("root", "ERROR", "during"))
def test_specific_logger(self):
l = LogCapture('one')
root.info('1')
one.info('2')
two.info('3')
child.info('4')
l.uninstall()
assert str(l) == (
"one INFO\n 2\n"
"one.child INFO\n 4"
)
def test_multiple_loggers(self):
l = LogCapture(('one.child','two'))
root.info('1')
one.info('2')
two.info('3')
child.info('4')
l.uninstall()
assert str(l) == (
"two INFO\n 3\n"
"one.child INFO\n 4"
)
def test_simple_manual_install(self):
l = LogCapture(install=False)
root.info('before')
l.install()
root.info('during')
l.uninstall()
root.info('after')
assert str(l) == "root INFO\n during"
def test_uninstall(self):
# Lets start off with a couple of loggers:
root = getLogger()
child = getLogger('child')
# Add a dummy filter so we can verify it is swapped out
# during the capture, and swapped back in after `uninstall`.
root.addFilter(DummyFilter())
# Lets also record the handlers for these loggers before
# we start the test:
before_root = root.handlers[:]
before_child = child.handlers[:]
# Lets also record the levels for the loggers:
old_root_level=root.level
old_child_level=child.level
# Also record the filters:
old_root_filters = root.filters[:]
old_child_filters = child.filters[:]
# Now the test:
try:
root.setLevel(49)
child.setLevel(69)
l1 = LogCapture()
l2 = LogCapture('child')
root = getLogger()
root.info('1')
child = getLogger('child')
assert root.level == 1
assert child.level == 1
assert root.filters == []
assert child.filters == []
child.info('2')
assert str(l1) == (
"root INFO\n 1\n"
"child INFO\n 2"
)
assert str(l2) == (
"child INFO\n 2"
)
# Add a dummy filter to the child,
# which should be removed by `uninstall`.
child.addFilter(DummyFilter())
l2.uninstall()
l1.uninstall()
assert root.level == 49
assert child.level == 69
finally:
root.setLevel(old_root_level)
child.setLevel(old_child_level)
# Now we check the handlers are as they were before
# the test:
assert root.handlers == before_root
assert child.handlers == before_child
# Also check the filters were restored:
assert root.filters == old_root_filters
assert child.filters == old_child_filters
def test_uninstall_all(self):
before_handlers_root = root.handlers[:]
before_handlers_child = child.handlers[:]
l1 = LogCapture()
l2 = LogCapture('one.child')
# We can see that the LogCaptures have changed the
# handlers, removing existing ones and installing
# their own:
assert len(root.handlers) == 1
assert root.handlers != before_handlers_root
assert len(child.handlers) == 1
assert child.handlers != before_handlers_child
# Now we show the function in action:
LogCapture.uninstall_all()
# ...and we can see the handlers are back as
# they were beefore:
assert before_handlers_root == root.handlers
assert before_handlers_child == child.handlers
def test_two_logcaptures_on_same_logger(self):
# If you create more than one LogCapture on a single
# logger, the 2nd one installed will stop the first
# one working!
l1 = LogCapture()
root.info('1st message')
assert str(l1) == "root INFO\n 1st message"
l2 = LogCapture()
root.info('2nd message')
# So, l1 missed this message:
assert str(l1) == "root INFO\n 1st message"
# ...because l2 kicked it out and recorded the message:
assert str(l2) == "root INFO\n 2nd message"
LogCapture.uninstall_all()
def test_uninstall_more_than_once(self):
# There's no problem with uninstalling a LogCapture
# more than once:
old_level = root.level
try:
root.setLevel(49)
l = LogCapture()
assert root.level == 1
l.uninstall()
assert root.level == 49
root.setLevel(69)
l.uninstall()
assert root.level == 69
finally:
root.setLevel(old_level)
# And even when loggers have been uninstalled, there's
# no problem having uninstall_all as a backstop:
l.uninstall_all()
def test_with_statement(self):
root.info('before')
with LogCapture() as l:
root.info('during')
root.info('after')
assert str(l) == "root INFO\n during"
class LogCaptureTests(TestCase):
def test_remove_existing_handlers(self):
logger = getLogger()
# get original handlers
original_handlers = logger.handlers
# put in a stub which will blow up if used
try:
logger.handlers = start = [object()]
with LogCapture() as l:
logger.info('during')
l.check(('root', 'INFO', 'during'))
compare(logger.handlers, start)
finally:
# only executed if the test fails
logger.handlers = original_handlers
def test_atexit(self):
# http://bugs.python.org/issue25532
from testfixtures.mock import call
m = Mock()
with Replacer() as r:
# make sure the marker is false, other tests will
# probably have set it
r.replace('testfixtures.LogCapture.atexit_setup', False)
r.replace('atexit.register', m.register)
l = LogCapture()
expected = [call.register(l.atexit)]
compare(expected, m.mock_calls)
with catch_warnings(record=True) as w:
l.atexit()
self.assertTrue(len(w), 1)
compare(str(w[0].message), (
"LogCapture instances not uninstalled by shutdown, "
"loggers captured:\n"
"(None,)"
))
l.uninstall()
compare(set(), LogCapture.instances)
# check re-running has no ill effects
l.atexit()
def test_numeric_log_level(self):
with LogCapture() as log:
getLogger().log(42, 'running in the family')
log.check(('root', 'Level 42', 'running in the family'))
def test_enable_disabled_logger(self):
logger = getLogger('disabled')
logger.disabled = True
with LogCapture('disabled') as log:
logger.info('a log message')
log.check(('disabled', 'INFO', 'a log message'))
compare(logger.disabled, True)
def test_no_propogate(self):
logger = getLogger('child')
# paranoid check
compare(logger.propagate, True)
with LogCapture() as global_log:
with LogCapture('child', propagate=False) as child_log:
logger.info('a log message')
child_log.check(('child', 'INFO', 'a log message'))
global_log.check()
compare(logger.propagate, True)
def test_len_and_getitem(self):
with LogCapture() as log:
compare(len(log), expected=0)
getLogger('foo').info('a log message')
compare(len(log), expected=1)
compare(log[0], expected=('foo', 'INFO', 'a log message'))
def test_truthiness_when_empty(self):
handler = LogCapture(install=False)
assert handler
assert bool(handler)
def test_shutdown_while_installed(self):
with LogCapture():
with ShouldWarn(
UserWarning(
'LogCapture instance closed while still installed, loggers captured:\n'
'(None,)'
)
):
shutdown()
# second shutdown should be fine:
shutdown()
class TestCheckPresent:
def test_order_matters_ok(self):
with LogCapture() as log:
root.info('one')
root.error('junk')
root.warning('two')
root.error('junk')
root.error('three')
log.check_present(
('root', 'INFO', 'one'),
('root', 'WARNING', 'two'),
('root', 'ERROR', 'three'),
)
def test_order_matters_not_okay(self):
with LogCapture() as log:
root.error('junk')
with ShouldAssert(dedent("""\
ignored:
[('root', 'ERROR', 'junk')]
same:
[]
expected:
[('root', 'INFO', 'one')]
actual:
[]""")):
log.check_present(
('root', 'INFO', 'one'),
)
def test_order_matters_not_okay_recursive(self):
with LogCapture(recursive_check=True) as log:
root.error('junk')
with ShouldAssert(dedent("""\
same:
[]
expected:
[('root', 'INFO', 'one')]
actual:
[('root', 'ERROR', 'junk')]
While comparing [0]: sequence not as expected:
same:
('root',)
expected:
('INFO', 'one')
actual:
('ERROR', 'junk')
While comparing [0][1]: 'INFO' (expected) != 'ERROR' (actual)""")):
log.check_present(
('root', 'INFO', 'one'),
)
def test_order_matters_but_wrong(self):
with LogCapture() as log:
root.info('one')
root.error('j1')
root.error('three')
root.warning('two')
root.error('j2')
with ShouldAssert(dedent("""\
ignored:
[('root', 'ERROR', 'j1'), ('root', 'ERROR', 'three'), ('root', 'ERROR', 'j2')]
same:
[('root', 'INFO', 'one'), ('root', 'WARNING', 'two')]
expected:
[('root', 'ERROR', 'three')]
actual:
[]""")):
log.check_present(
('root', 'INFO', 'one'),
('root', 'WARNING', 'two'),
('root', 'ERROR', 'three'),
)
def test_order_doesnt_matter_ok(self):
with LogCapture() as log:
root.info('one')
root.error('junk')
root.warning('two')
root.error('junk')
root.error('three')
log.check_present(
('root', 'ERROR', 'three'),
('root', 'INFO', 'one'),
('root', 'WARNING', 'two'),
order_matters=False
)
def test_order_doesnt_matter_not_okay(self):
with LogCapture() as log:
root.error('junk')
with ShouldAssert(dedent("""\
ignored:
[('root', 'ERROR', 'junk')]
in expected but not actual:
[('root', 'INFO', 'one')]""")):
log.check_present(
('root', 'INFO', 'one'),
order_matters=False
)
def test_single_item_ok(self):
with LogCapture() as log:
root.info('one')
root.error('junk')
root.warning('two')
root.error('junk')
root.error('three')
log.check_present(
('root', 'WARNING', 'two'),
)
def test_single_item_not_ok(self):
with LogCapture(attributes=['getMessage']) as log:
root.info('one')
root.error('junk')
root.error('three')
with ShouldAssert(dedent("""\
ignored:
['one', 'junk', 'three']
same:
[]
expected:
['two']
actual:
[]""")):
log.check_present('two')
def test_multiple_identical_expected_order_matters(self):
with LogCapture() as log:
root.info('one')
root.info('one')
root.error('junk')
root.warning('two')
root.error('junk')
root.warning('two')
log.check_present(
('root', 'INFO', 'one'),
('root', 'INFO', 'one'),
('root', 'WARNING', 'two'),
('root', 'WARNING', 'two'),
)
def test_multiple_identical_expected_order_doesnt_matter_ok(self):
with LogCapture() as log:
root.info('one')
root.warning('two')
root.error('junk')
root.warning('two')
root.error('junk')
root.info('one')
log.check_present(
('root', 'INFO', 'one'),
('root', 'INFO', 'one'),
('root', 'WARNING', 'two'),
('root', 'WARNING', 'two'),
order_matters=False
)
def test_multiple_identical_expected_order_doesnt_matter_not_ok(self):
with LogCapture() as log:
root.error('junk')
root.info('one')
root.warning('two')
root.error('junk')
root.info('one')
with ShouldAssert(dedent("""\
ignored:
[('root', 'ERROR', 'junk'), ('root', 'ERROR', 'junk')]
same:
[('root', 'INFO', 'one'), ('root', 'INFO', 'one'), ('root', 'WARNING', 'two')]
in expected but not actual:
[('root', 'WARNING', 'two')]""")):
log.check_present(
('root', 'INFO', 'one'),
('root', 'INFO', 'one'),
('root', 'WARNING', 'two'),
('root', 'WARNING', 'two'),
order_matters=False
)
def test_entries_are_dictionaries(self):
def extract(record):
return {'level': record.levelname, 'message': record.getMessage()}
with LogCapture(attributes=extract) as log:
root.info('one')
root.error('junk')
root.warning('two')
root.error('junk')
root.info('one')
log.check_present(
{'level': 'INFO', 'message': 'one'},
{'level': 'INFO', 'message': 'one'},
{'level': 'WARNING', 'message': 'two'},
order_matters=False
)
def test_almost_same_order_matters(self):
with LogCapture() as log:
root.info('one')
root.error('junk')
root.warning('two')
root.error('junk')
with ShouldAssert(dedent("""\
ignored:
[('root', 'ERROR', 'junk'), ('root', 'ERROR', 'junk')]
same:
[('root', 'INFO', 'one'), ('root', 'WARNING', 'two')]
expected:
[('root', 'ERROR', 'three')]
actual:
[]""")):
log.check_present(
('root', 'INFO', 'one'),
('root', 'WARNING', 'two'),
('root', 'ERROR', 'three'),
)
def test_almost_same_order_doesnt_matter(self):
with LogCapture() as log:
root.info('one')
root.error('junk')
root.error('three')
root.error('junk')
with ShouldAssert(dedent("""\
ignored:
[('root', 'ERROR', 'junk'), ('root', 'ERROR', 'junk')]
same:
[('root', 'ERROR', 'three'), ('root', 'INFO', 'one')]
in expected but not actual:
[('root', 'WARNING', 'two')]""")):
log.check_present(
('root', 'ERROR', 'three'),
('root', 'INFO', 'one'),
('root', 'WARNING', 'two'),
order_matters=False
)
|