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 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
|
import weakref
import pytest
from pytestqt import qt_compat
from pytestqt.qt_compat import qt_api
def test_basics(qtbot):
"""
Basic test that works more like a sanity check to ensure we are setting up a QApplication
properly and are able to display a simple event_recorder.
"""
assert qt_api.QtWidgets.QApplication.instance() is not None
widget = qt_api.QtWidgets.QWidget()
qtbot.addWidget(widget)
widget.setWindowTitle("W1")
widget.show()
assert widget.isVisible()
assert widget.windowTitle() == "W1"
def test_qapp_default_name(qapp):
assert qapp.applicationName() == "pytest-qt-qapp"
def test_qapp_name(testdir):
testdir.makepyfile(
"""
def test_name(qapp):
assert qapp.applicationName() == "frobnicator"
"""
)
testdir.makeini(
"""
[pytest]
qt_qapp_name = frobnicator
"""
)
res = testdir.runpytest_subprocess()
res.stdout.fnmatch_lines("*1 passed*")
def test_qapp_cls(testdir):
testdir.makepyfile(
app="""
from pytestqt.qt_compat import qt_api
# Gets run before the plugin via conftest.py
qt_api.set_qt_api(None)
class CustomQApp(qt_api.QtWidgets.QApplication):
pass
"""
)
testdir.makeconftest(
"""
import pytest
from app import CustomQApp
@pytest.fixture(scope="session")
def qapp_cls():
return CustomQApp
"""
)
testdir.makepyfile(
"""
from app import CustomQApp
def test_cls(qapp):
assert isinstance(qapp, CustomQApp)
"""
)
res = testdir.runpytest_subprocess()
res.stdout.fnmatch_lines("*1 passed*")
def test_qapp_reuse_existing(testdir):
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
app_instance = qt_api.QtWidgets.QApplication([])
def test_instances(qapp):
assert qapp is app_instance
assert qapp is qt_api.QtWidgets.QApplication.instance()
"""
)
res = testdir.runpytest_subprocess()
res.stdout.fnmatch_lines("*1 passed*")
def test_qapp_reuse_wrong_type(testdir):
testdir.makeconftest(
"""
import pytest
from pytestqt.qt_compat import qt_api
# Gets run before the plugin
qt_api.set_qt_api(None)
class CustomQApp(qt_api.QtWidgets.QApplication):
pass
@pytest.fixture(scope="session")
def qapp_cls():
return CustomQApp
"""
)
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
app_instance = qt_api.QtWidgets.QApplication([])
def test_wrong_type(qapp):
pass
"""
)
res = testdir.runpytest_subprocess()
res.stdout.fnmatch_lines(
"*Existing QApplication <*.QtWidgets.QApplication* at 0x*> is not an "
"instance of qapp_cls: <class 'conftest.CustomQApp'>"
)
def test_key_events(qtbot, event_recorder):
"""
Basic key events test.
"""
def extract(key_event):
return (
key_event.type(),
qt_api.QtCore.Qt.Key(key_event.key()),
key_event.text(),
)
event_recorder.registerEvent(qt_api.QtGui.QKeyEvent, extract)
qtbot.keyPress(event_recorder, "a")
assert event_recorder.event_data == (
qt_api.QtCore.QEvent.Type.KeyPress,
qt_api.QtCore.Qt.Key.Key_A,
"a",
)
qtbot.keyRelease(event_recorder, "a")
assert event_recorder.event_data == (
qt_api.QtCore.QEvent.Type.KeyRelease,
qt_api.QtCore.Qt.Key.Key_A,
"a",
)
def test_mouse_events(qtbot, event_recorder):
"""
Basic mouse events test.
"""
def extract(mouse_event):
return (mouse_event.type(), mouse_event.button(), mouse_event.modifiers())
event_recorder.registerEvent(qt_api.QtGui.QMouseEvent, extract)
qtbot.mousePress(event_recorder, qt_api.QtCore.Qt.MouseButton.LeftButton)
assert event_recorder.event_data == (
qt_api.QtCore.QEvent.Type.MouseButtonPress,
qt_api.QtCore.Qt.MouseButton.LeftButton,
qt_api.QtCore.Qt.KeyboardModifier.NoModifier,
)
qtbot.mousePress(
event_recorder,
qt_api.QtCore.Qt.MouseButton.RightButton,
qt_api.QtCore.Qt.KeyboardModifier.AltModifier,
)
assert event_recorder.event_data == (
qt_api.QtCore.QEvent.Type.MouseButtonPress,
qt_api.QtCore.Qt.MouseButton.RightButton,
qt_api.QtCore.Qt.KeyboardModifier.AltModifier,
)
def test_stop(qtbot, timer):
"""
Test qtbot.stop()
"""
widget = qt_api.QtWidgets.QWidget()
qtbot.addWidget(widget)
with qtbot.waitExposed(widget):
widget.show()
timer.single_shot_callback(widget.close, 0)
qtbot.stop()
@pytest.mark.parametrize('show', [False])
@pytest.mark.parametrize("method_name", ["waitExposed", "waitActive"])
def test_wait_window(show, method_name, qtbot):
"""
Using one of the wait-widget methods should not raise anything if the widget
is properly displayed, otherwise should raise a TimeoutError.
"""
method = getattr(qtbot, method_name)
widget = qt_api.QtWidgets.QWidget()
qtbot.add_widget(widget)
if show:
with method(widget, timeout=1000):
widget.show()
else:
with pytest.raises(qtbot.TimeoutError):
with method(widget, timeout=100):
pass
@pytest.mark.parametrize("show", [True, False])
def test_wait_for_window_shown(qtbot, show):
widget = qt_api.QtWidgets.QWidget()
qtbot.add_widget(widget)
if show:
widget.show()
with pytest.deprecated_call(match="waitForWindowShown is deprecated"):
shown = qtbot.waitForWindowShown(widget)
assert shown == show
@pytest.mark.parametrize("method_name", ["waitExposed", "waitActive"])
def test_wait_window_propagates_other_exception(method_name, qtbot):
"""
Exceptions raised inside the with-statement of wait-widget methods should
propagate properly.
"""
method = getattr(qtbot, method_name)
widget = qt_api.QtWidgets.QWidget()
qtbot.add_widget(widget)
with pytest.raises(ValueError, match="some other error"):
with method(widget, timeout=100):
widget.show()
raise ValueError("some other error")
def test_widget_kept_as_weakref(qtbot):
"""
Test if the widget is kept as a weak reference in QtBot
"""
widget = qt_api.QtWidgets.QWidget()
qtbot.add_widget(widget)
widget = weakref.ref(widget)
assert widget() is None
def test_event_processing_before_and_after_teardown(testdir):
"""
Make sure events are processed before and after fixtures are torn down.
The test works by creating a session object which pops() one of its events
whenever a processEvents() occurs. Fixture and tests append values
to the event list but expect the list to have been processed (by the pop())
at each point of interest.
https://github.com/pytest-dev/pytest-qt/issues/67
"""
testdir.makepyfile(
"""
from pytestqt.qt_compat import qt_api
import pytest
@pytest.fixture(scope='session')
def events_queue(qapp):
class EventsQueue(qt_api.QtCore.QObject):
def __init__(self):
qt_api.QtCore.QObject.__init__(self)
self.events = []
def pop_later(self):
qapp.postEvent(self, qt_api.QtCore.QEvent(qt_api.QtCore.QEvent.Type.User))
def event(self, ev):
if ev.type() == qt_api.QtCore.QEvent.Type.User:
self.events.pop(-1)
return qt_api.QtCore.QObject.event(self, ev)
return EventsQueue()
@pytest.fixture
def fix(events_queue, qapp):
assert events_queue.events == []
yield
assert events_queue.events == []
events_queue.events.append('fixture teardown')
events_queue.pop_later()
@pytest.mark.parametrize('i', range(3))
def test_events(events_queue, fix, i):
assert events_queue.events == []
events_queue.events.append('test event')
events_queue.pop_later()
"""
)
res = testdir.runpytest()
res.stdout.fnmatch_lines(["*3 passed in*"])
def test_header(testdir):
testdir.makeconftest(
"""
from pytestqt import qt_compat
from pytestqt.qt_compat import qt_api
def mock_get_versions():
return qt_compat.VersionTuple('PyQtAPI', '1.0', '2.5', '3.5')
assert hasattr(qt_api, 'get_versions')
qt_api.get_versions = mock_get_versions
"""
)
res = testdir.runpytest()
res.stdout.fnmatch_lines(
["*test session starts*", "PyQtAPI 1.0 -- Qt runtime 2.5 -- Qt compiled 3.5"]
)
def test_qvariant(tmpdir):
"""Test that QVariant works in the same way across all supported Qt bindings."""
settings = qt_api.QtCore.QSettings(
str(tmpdir / "foo.ini"), qt_api.QtCore.QSettings.Format.IniFormat
)
settings.setValue("int", 42)
settings.setValue("str", "Hello")
settings.setValue("empty", None)
assert settings.value("int") == 42
assert settings.value("str") == "Hello"
assert settings.value("empty") is None
def test_widgets_closed_before_fixtures(testdir):
"""
Ensure widgets added by "qtbot.add_widget" are closed before all other
fixtures are teardown. (#106).
"""
testdir.makepyfile(
"""
import pytest
from pytestqt.qt_compat import qt_api
class Widget(qt_api.QtWidgets.QWidget):
closed = False
def closeEvent(self, e):
e.accept()
self.closed = True
@pytest.fixture
def widget(qtbot):
w = Widget()
qtbot.add_widget(w)
yield w
assert w.closed
def test_foo(widget):
pass
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*= 1 passed in *"])
def test_qtbot_wait(qtbot, stop_watch):
stop_watch.start()
qtbot.wait(250)
stop_watch.stop()
assert stop_watch.elapsed >= 220
@pytest.fixture
def event_recorder(qtbot):
class EventRecorder(qt_api.QtWidgets.QWidget):
"""
Widget that records some kind of events sent to it.
When this event_recorder receives a registered event (by calling `registerEvent`), it will call
the associated *extract* function and hold the return value from the function in the
`event_data` member.
"""
def __init__(self):
qt_api.QtWidgets.QWidget.__init__(self)
self._event_types = {}
self.event_data = None
def registerEvent(self, event_type, extract_func):
self._event_types[event_type] = extract_func
def event(self, ev):
for event_type, extract_func in self._event_types.items():
if isinstance(ev, event_type):
self.event_data = extract_func(ev)
return True
return False
widget = EventRecorder()
qtbot.addWidget(widget)
return widget
@pytest.mark.parametrize(
"value, expected",
[
(True, True),
(False, False),
("True", True),
("False", False),
("true", True),
("false", False),
],
)
def test_parse_ini_boolean_valid(value, expected):
import pytestqt.qtbot
assert pytestqt.qtbot._parse_ini_boolean(value) == expected
def test_parse_ini_boolean_invalid():
import pytestqt.qtbot
with pytest.raises(ValueError):
pytestqt.qtbot._parse_ini_boolean("foo")
@pytest.mark.parametrize("option_api", ["pyqt5", "pyqt6", "pyside2", "pyside6"])
def test_qt_api_ini_config(testdir, monkeypatch, option_api):
"""
Test qt_api ini option handling.
"""
from pytestqt.qt_compat import qt_api
monkeypatch.delenv("PYTEST_QT_API", raising=False)
testdir.makeini(
"""
[pytest]
qt_api={option_api}
""".format(
option_api=option_api
)
)
testdir.makepyfile(
"""
import pytest
def test_foo(qtbot):
pass
"""
)
result = testdir.runpytest_subprocess()
if qt_api.pytest_qt_api == option_api:
result.stdout.fnmatch_lines(["* 1 passed in *"])
else:
try:
ModuleNotFoundError
except NameError:
# Python < 3.6
result.stderr.fnmatch_lines(["*ImportError:*"])
else:
# Python >= 3.6
result.stderr.fnmatch_lines(["*ModuleNotFoundError:*"])
@pytest.mark.parametrize("envvar", ["pyqt5", "pyqt6", "pyside2", "pyside6"])
def test_qt_api_ini_config_with_envvar(testdir, monkeypatch, envvar):
"""ensure environment variable wins over config value if both are present"""
testdir.makeini(
"""
[pytest]
qt_api={option_api}
""".format(
option_api="piecute"
)
)
monkeypatch.setenv("PYTEST_QT_API", envvar)
testdir.makepyfile(
"""
import pytest
def test_foo(qtbot):
pass
"""
)
result = testdir.runpytest_subprocess()
if qt_api.pytest_qt_api == envvar:
result.stdout.fnmatch_lines(["* 1 passed in *"])
else:
try:
ModuleNotFoundError
except NameError:
# Python < 3.6
result.stderr.fnmatch_lines(["*ImportError:*"])
else:
# Python >= 3.6
result.stderr.fnmatch_lines(["*ModuleNotFoundError:*"])
def test_invalid_qt_api_envvar(testdir, monkeypatch):
"""
Make sure the error message with an invalid PYQTEST_QT_API is correct.
"""
testdir.makepyfile(
"""
import pytest
def test_foo(qtbot):
pass
"""
)
monkeypatch.setenv("PYTEST_QT_API", "piecute")
result = testdir.runpytest_subprocess()
result.stderr.fnmatch_lines(
["* Invalid value for $PYTEST_QT_API: piecute, expected one of *"]
)
def test_qapp_args(testdir):
"""
Test customizing of QApplication arguments.
"""
testdir.makeconftest(
"""
import pytest
@pytest.fixture(scope='session')
def qapp_args():
return ['prog_name', '--test-arg']
"""
)
testdir.makepyfile(
"""
def test_args(qapp):
assert '--test-arg' in list(qapp.arguments())
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(["*= 1 passed in *"])
def test_qapp_args_default(testdir):
"""
Test QApplication default arguments.
"""
testdir.makepyfile(
"""
def test_args(qapp):
args = qapp.arguments()
assert args[0] == 'pytest-qt-qapp'
"""
)
result = testdir.runpytest_subprocess()
result.stdout.fnmatch_lines(["*= 1 passed in *"])
def test_importerror(monkeypatch):
def _fake_import(name, *args):
raise ModuleNotFoundError(f"Failed to import {name}")
def _fake_is_library_loaded(name, *args):
return False
monkeypatch.delenv("PYTEST_QT_API", raising=False)
monkeypatch.setattr(qt_compat, "_import", _fake_import)
monkeypatch.setattr(qt_compat, "_is_library_loaded", _fake_is_library_loaded)
expected = (
"pytest-qt requires either PySide2, PySide6, PyQt5 or PyQt6 installed.\n"
" PyQt5.QtCore: Failed to import PyQt5.QtCore\n"
" PyQt6.QtCore: Failed to import PyQt6.QtCore\n"
" PySide2.QtCore: Failed to import PySide2.QtCore\n"
" PySide6.QtCore: Failed to import PySide6.QtCore"
)
with pytest.raises(pytest.UsageError, match=expected):
qt_api.set_qt_api(api=None)
@pytest.mark.parametrize(
"option_api, backend",
[
("pyqt5", "PyQt5"),
("pyqt6", "PyQt6"),
("pyside2", "PySide2"),
("pyside6", "PySide6"),
],
)
def test_already_loaded_backend(monkeypatch, option_api, backend):
import builtins
class Mock:
pass
qtcore = Mock()
for method_name in (
"qInstallMessageHandler",
"qDebug",
"qWarning",
"qCritical",
"qFatal",
):
setattr(qtcore, method_name, lambda *_: None)
if backend in ("PyQt5", "PyQt6"):
pyqt_version = 0x050B00 if backend == "PyQt5" else 0x060000
qtcore.PYQT_VERSION = pyqt_version + 1
qtcore.pyqtSignal = object()
qtcore.pyqtSlot = object()
qtcore.pyqtProperty = object()
else:
qtcore.Signal = object()
qtcore.Slot = object()
qtcore.Property = object()
qtwidgets = Mock()
qapplication = Mock()
qapplication.instance = lambda *_: None
qtwidgets.QApplication = qapplication
qbackend = Mock()
qbackend.QtCore = qtcore
qbackend.QtGui = object()
qbackend.QtTest = object()
qbackend.QtWidgets = qtwidgets
import_orig = builtins.__import__
def _fake_import(name, *args, **kwargs):
if name == backend:
return qbackend
return import_orig(name, *args, **kwargs)
def _fake_is_library_loaded(name, *args):
return name == backend
monkeypatch.delenv("PYTEST_QT_API", raising=False)
monkeypatch.setattr(qt_compat, "_is_library_loaded", _fake_is_library_loaded)
monkeypatch.setattr(builtins, "__import__", _fake_import)
qt_api.set_qt_api(api=None)
assert qt_api.pytest_qt_api == option_api
def test_before_close_func(testdir):
"""
Test the `before_close_func` argument of qtbot.addWidget.
"""
import sys
testdir.makepyfile(
"""
import sys
import pytest
from pytestqt.qt_compat import qt_api
def widget_closed(w):
assert w.some_id == 'my id'
sys.pytest_qt_widget_closed = True
@pytest.fixture
def widget(qtbot):
w = qt_api.QtWidgets.QWidget()
w.some_id = 'my id'
qtbot.add_widget(w, before_close_func=widget_closed)
return w
def test_foo(widget):
pass
"""
)
result = testdir.runpytest_inprocess()
result.stdout.fnmatch_lines(["*= 1 passed in *"])
assert sys.pytest_qt_widget_closed
def test_addwidget_typeerror(testdir, qtbot):
"""
Make sure addWidget catches type errors early.
"""
obj = qt_api.QtCore.QObject()
with pytest.raises(TypeError):
qtbot.addWidget(obj)
|