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
|
import datetime
import pytest
from pytestqt.qt_compat import qt_api
@pytest.mark.parametrize("test_succeeds", [True, False])
@pytest.mark.parametrize("qt_log", [True, False])
def test_basic_logging(testdir, test_succeeds, qt_log):
"""
Test Qt logging capture output.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makepyfile(
"""
import sys
from pytestqt.qt_compat import qt_api
def to_unicode(s):
return s.decode('utf-8', 'replace') if isinstance(s, bytes) else s
def print_msg(msg_type, context, message):
sys.stderr.write(to_unicode(message) + '\\n')
qt_api.QtCore.qInstallMessageHandler(print_msg)
def test_types():
# qInfo is not exposed by the bindings yet (#225)
# qt_api.qInfo('this is an INFO message')
qt_api.qDebug('this is a DEBUG message')
qt_api.qWarning('this is a WARNING message')
qt_api.qCritical('this is a CRITICAL message')
assert {}
""".format(
test_succeeds
)
)
res = testdir.runpytest(*(["--no-qt-log"] if not qt_log else []))
if test_succeeds:
assert "Captured Qt messages" not in res.stdout.str()
assert "Captured stderr call" not in res.stdout.str()
else:
if qt_log:
res.stdout.fnmatch_lines(
[
"*-- Captured Qt messages --*",
# qInfo is not exposed by the bindings yet (#232)
# '*QtInfoMsg: this is an INFO message*',
"*QtDebugMsg: this is a DEBUG message*",
"*QtWarningMsg: this is a WARNING message*",
"*QtCriticalMsg: this is a CRITICAL message*",
]
)
else:
res.stdout.fnmatch_lines(
[
"*-- Captured stderr call --*",
# qInfo is not exposed by the bindings yet (#232)
# '*QtInfoMsg: this is an INFO message*',
# 'this is an INFO message*',
"this is a DEBUG message*",
"this is a WARNING message*",
"this is a CRITICAL message*",
]
)
def test_qinfo(qtlog):
"""Test INFO messages when we have means to do so. Should be temporary until bindings
catch up and expose qInfo (or at least QMessageLogger), then we should update
the other logging tests properly. #232
"""
if qt_api.is_pyside:
assert (
qt_api.qInfo is None
), "pyside2/6 does not expose qInfo. If it does, update this test."
return
qt_api.qInfo("this is an INFO message")
records = [(m.type, m.message.strip()) for m in qtlog.records]
assert records == [(qt_api.QtCore.QtMsgType.QtInfoMsg, "this is an INFO message")]
def test_qtlog_fixture(qtlog):
"""
Test qtlog fixture.
"""
# qInfo is not exposed by the bindings yet (#232)
qt_api.qDebug("this is a DEBUG message")
qt_api.qWarning("this is a WARNING message")
qt_api.qCritical("this is a CRITICAL message")
records = [(m.type, m.message.strip()) for m in qtlog.records]
assert records == [
(qt_api.QtCore.QtMsgType.QtDebugMsg, "this is a DEBUG message"),
(qt_api.QtCore.QtMsgType.QtWarningMsg, "this is a WARNING message"),
(qt_api.QtCore.QtMsgType.QtCriticalMsg, "this is a CRITICAL message"),
]
# `records` attribute is read-only
with pytest.raises(AttributeError):
qtlog.records = []
@pytest.mark.parametrize("arg", ["--no-qt-log", "--capture=no", "-s"])
def test_fixture_with_logging_disabled(testdir, arg):
"""
Test that qtlog fixture doesn't capture anything if logging is disabled
in the command line.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
def test_types(qtlog):
qt_api.qWarning('message')
assert qtlog.records == []
"""
)
res = testdir.runpytest(arg)
res.stdout.fnmatch_lines("*1 passed*")
@pytest.mark.parametrize("use_context_manager", [True, False])
def test_disable_qtlog_context_manager(testdir, use_context_manager):
"""
Test qtlog.disabled() context manager.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makeini(
"""
[pytest]
qt_log_level_fail = CRITICAL
"""
)
if use_context_manager:
code = "with qtlog.disabled():"
else:
code = "if 1:"
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
def test_1(qtlog):
{code}
qt_api.qCritical('message')
""".format(
code=code
)
)
res = testdir.inline_run()
passed = 1 if use_context_manager else 0
res.assertoutcome(passed=passed, failed=int(not passed))
@pytest.mark.parametrize("use_mark", [True, False])
def test_disable_qtlog_mark(testdir, use_mark):
"""
Test mark which disables logging capture for a test.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makeini(
"""
[pytest]
qt_log_level_fail = CRITICAL
"""
)
mark = "@pytest.mark.no_qt_log" if use_mark else ""
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
import pytest
{mark}
def test_1():
qt_api.qCritical('message')
""".format(
mark=mark
)
)
res = testdir.inline_run()
passed = 1 if use_mark else 0
res.assertoutcome(passed=passed, failed=int(not passed))
def test_logging_formatting(testdir):
"""
Test custom formatting for logging messages.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
def test_types():
qt_api.qWarning('this is a WARNING message')
assert 0
"""
)
f = "{rec.type_name} {rec.log_type_name} {rec.when:%Y-%m-%d}: {rec.message}"
res = testdir.runpytest(f"--qt-log-format={f}")
today = "{:%Y-%m-%d}".format(datetime.datetime.now())
res.stdout.fnmatch_lines(
[
"*-- Captured Qt messages --*",
f"QtWarningMsg WARNING {today}: this is a WARNING message*",
]
)
@pytest.mark.parametrize(
"level, expect_passes", [("DEBUG", 1), ("WARNING", 2), ("CRITICAL", 3), ("NO", 4)]
)
def test_logging_fails_tests(testdir, level, expect_passes):
"""
Test qt_log_level_fail ini option.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makeini(
"""
[pytest]
qt_log_level_fail = {level}
""".format(
level=level
)
)
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
def test_1():
qt_api.qDebug('this is a DEBUG message')
def test_2():
qt_api.qWarning('this is a WARNING message')
def test_3():
qt_api.qCritical('this is a CRITICAL message')
def test_4():
assert 1
"""
)
res = testdir.runpytest()
lines = []
if level != "NO":
lines.extend(
[
"*Failure: Qt messages with level {} or above emitted*".format(
level.upper()
),
"*-- Captured Qt messages --*",
]
)
lines.append(f"*{expect_passes} passed*")
res.stdout.fnmatch_lines(lines)
def test_logging_fails_tests_mark(testdir):
"""
Test mark overrides what's configured in the ini file.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makeini(
"""
[pytest]
qt_log_level_fail = CRITICAL
"""
)
testdir.makepyfile(
"""
from pytestqt.qt_compat import qWarning
import pytest
@pytest.mark.qt_log_level_fail('WARNING')
def test_1():
qWarning('message')
"""
)
res = testdir.inline_run()
res.assertoutcome(failed=1)
def test_logging_fails_ignore(testdir):
"""
Test qt_log_ignore config option.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makeini(
"""
[pytest]
qt_log_level_fail = CRITICAL
qt_log_ignore =
WM_DESTROY.*sent
WM_PAINT not handled
"""
)
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
import pytest
def test1():
qt_api.qCritical('a critical message')
def test2():
qt_api.qCritical('WM_DESTROY was sent')
def test3():
qt_api.qCritical('WM_DESTROY was sent')
assert 0
def test4():
qt_api.qCritical('WM_PAINT not handled')
qt_api.qCritical('another critical message')
"""
)
res = testdir.runpytest()
lines = [
# test1 fails because it has emitted a CRITICAL message and that message
# does not match any regex in qt_log_ignore
"*_ test1 _*",
"*Failure: Qt messages with level CRITICAL or above emitted*",
"*QtCriticalMsg: a critical message*",
# test2 succeeds because its message matches qt_log_ignore
# test3 fails because of an assert, but the ignored message should
# still appear in the failure message
"*_ test3 _*",
"*AssertionError*",
"*QtCriticalMsg: WM_DESTROY was sent*(IGNORED)*",
# test4 fails because one message is ignored but the other isn't
"*_ test4 _*",
"*Failure: Qt messages with level CRITICAL or above emitted*",
"*QtCriticalMsg: WM_PAINT not handled*(IGNORED)*",
"*QtCriticalMsg: another critical message*",
# summary
"*3 failed, 1 passed*",
]
res.stdout.fnmatch_lines(lines)
@pytest.mark.parametrize("message", ["match-global", "match-mark"])
@pytest.mark.parametrize("marker_args", ["'match-mark', extend=True", "'match-mark'"])
def test_logging_mark_with_extend(testdir, message, marker_args):
"""
Test qt_log_ignore mark with extend=True.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makeini(
"""
[pytest]
qt_log_level_fail = CRITICAL
qt_log_ignore = match-global
"""
)
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
import pytest
@pytest.mark.qt_log_ignore({marker_args})
def test1():
qt_api.qCritical('{message}')
""".format(
message=message, marker_args=marker_args
)
)
res = testdir.inline_run()
res.assertoutcome(passed=1, failed=0)
@pytest.mark.parametrize(
"message, error_expected", [("match-global", True), ("match-mark", False)]
)
def test_logging_mark_without_extend(testdir, message, error_expected):
"""
Test qt_log_ignore mark with extend=False.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makeini(
"""
[pytest]
qt_log_level_fail = CRITICAL
qt_log_ignore = match-global
"""
)
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
import pytest
@pytest.mark.qt_log_ignore('match-mark', extend=False)
def test1():
qt_api.qCritical('{message}')
""".format(
message=message
)
)
res = testdir.inline_run()
if error_expected:
res.assertoutcome(passed=0, failed=1)
else:
res.assertoutcome(passed=1, failed=0)
def test_logging_mark_with_invalid_argument(testdir):
"""
Test qt_log_ignore mark with invalid keyword argument.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makepyfile(
"""
import pytest
@pytest.mark.qt_log_ignore('match-mark', does_not_exist=True)
def test1():
pass
"""
)
res = testdir.runpytest()
lines = [
"*= ERRORS =*",
"*_ ERROR at setup of test1 _*",
"*ValueError: Invalid keyword arguments in {'does_not_exist': True} "
"for qt_log_ignore mark.",
# summary
"*= 1 error in*",
]
res.stdout.fnmatch_lines(lines)
@pytest.mark.parametrize("apply_mark", [True, False])
def test_logging_fails_ignore_mark_multiple(testdir, apply_mark):
"""
Make sure qt_log_ignore mark supports multiple arguments.
:type testdir: _pytest.pytester.TmpTestdir
"""
if apply_mark:
mark = '@pytest.mark.qt_log_ignore("WM_DESTROY", "WM_PAINT")'
else:
mark = ""
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
import pytest
@pytest.mark.qt_log_level_fail('CRITICAL')
{mark}
def test1():
qt_api.qCritical('WM_PAINT was sent')
""".format(
mark=mark
)
)
res = testdir.inline_run()
passed = 1 if apply_mark else 0
res.assertoutcome(passed=passed, failed=int(not passed))
def test_lineno_failure(testdir):
"""
Test that tests when failing because log messages were emitted report
the correct line number.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makeini(
"""
[pytest]
qt_log_level_fail = WARNING
"""
)
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
def test_foo():
assert foo() == 10
def foo():
qt_api.qWarning('this is a WARNING message')
return 10
"""
)
res = testdir.runpytest()
if qt_api.is_pyqt:
res.stdout.fnmatch_lines(
[
"*test_lineno_failure.py:2: Failure*",
"*test_lineno_failure.py:foo:5:*",
" QtWarningMsg: this is a WARNING message",
]
)
else:
res.stdout.fnmatch_lines(
[
"*test_lineno_failure.py:2: Failure*",
"QtWarningMsg: this is a WARNING message",
]
)
def test_context_none(testdir):
"""
Sometimes PyQt will emit a context with some/all attributes set as None
instead of appropriate file, function and line number.
Test that when this happens the plugin doesn't break, and it filters
out the context information.
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
def test_foo(request):
log_capture = request.node.qt_log_capture
context = log_capture._Context(None, None, 0, None)
log_capture._handle_with_context(qt_api.QtCore.QtMsgType.QtWarningMsg,
context, "WARNING message")
assert 0
"""
)
res = testdir.runpytest()
assert "*None:None:0:*" not in str(res.stdout)
res.stdout.fnmatch_lines(["QtWarningMsg: WARNING message"])
def test_logging_broken_makereport(testdir):
"""
Make sure logging's makereport hookwrapper doesn't hide exceptions.
See https://github.com/pytest-dev/pytest-qt/issues/98
:type testdir: _pytest.pytester.TmpTestdir
"""
testdir.makepyfile(
conftest="""
import pytest
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(call):
if call.when == 'call':
raise Exception("This should not be hidden")
yield
"""
)
p = testdir.makepyfile(
"""
def test_foo():
pass
"""
)
res = testdir.runpytest_subprocess(p)
res.stdout.fnmatch_lines(["*This should not be hidden*"])
|