From bf156cf5544727e8f5fe23c6b1326616c35f48b1 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 11 Aug 2015 18:08:11 +0200 Subject: [PATCH] Mock out QApplication.allWidgets. This could return widgets which are still alive from previous tests, so it's not reliable. --- tests/stubs.py | 5 ++++- tests/utils/test_debug.py | 11 +++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/stubs.py b/tests/stubs.py index 3ca5341ae..a7d34133b 100644 --- a/tests/stubs.py +++ b/tests/stubs.py @@ -87,11 +87,14 @@ class FakeQApplication: """Stub to insert as QApplication module.""" - def __init__(self, style=None): + def __init__(self, style=None, all_widgets=None): self.instance = mock.Mock(return_value=self) + self.style = mock.Mock(spec=QCommonStyle) self.style().metaObject().className.return_value = style + self.allWidgets = lambda: all_widgets + class FakeUrl: diff --git a/tests/utils/test_debug.py b/tests/utils/test_debug.py index c5cadc41d..278f21ff1 100644 --- a/tests/utils/test_debug.py +++ b/tests/utils/test_debug.py @@ -216,7 +216,7 @@ class ExampleObject(QObject): return ''.format(self._num) -class ExampleWidget(QWidget): +class ExampleWidget(QObject): def __init__(self, num, parent=None): self._num = num @@ -226,12 +226,11 @@ class ExampleWidget(QWidget): return ''.format(self._num) -def test_get_all_objects(qtbot): +def test_get_all_objects(stubs, monkeypatch): # pylint: disable=unused-variable - w1 = ExampleWidget(1) - qtbot.add_widget(w1) - w2 = ExampleWidget(2) - qtbot.add_widget(w2) + widgets = [ExampleWidget(1), ExampleWidget(2)] + app = stubs.FakeQApplication(all_widgets=widgets) + monkeypatch.setattr(debug, 'QApplication', app) root = QObject() o1 = ExampleObject(1, root)