Mock out QApplication.allWidgets.
This could return widgets which are still alive from previous tests, so it's not reliable.
This commit is contained in:
parent
5bd55cb38b
commit
bf156cf554
@ -87,11 +87,14 @@ class FakeQApplication:
|
|||||||
|
|
||||||
"""Stub to insert as QApplication module."""
|
"""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.instance = mock.Mock(return_value=self)
|
||||||
|
|
||||||
self.style = mock.Mock(spec=QCommonStyle)
|
self.style = mock.Mock(spec=QCommonStyle)
|
||||||
self.style().metaObject().className.return_value = style
|
self.style().metaObject().className.return_value = style
|
||||||
|
|
||||||
|
self.allWidgets = lambda: all_widgets
|
||||||
|
|
||||||
|
|
||||||
class FakeUrl:
|
class FakeUrl:
|
||||||
|
|
||||||
|
@ -216,7 +216,7 @@ class ExampleObject(QObject):
|
|||||||
return '<ExampleObject {}>'.format(self._num)
|
return '<ExampleObject {}>'.format(self._num)
|
||||||
|
|
||||||
|
|
||||||
class ExampleWidget(QWidget):
|
class ExampleWidget(QObject):
|
||||||
|
|
||||||
def __init__(self, num, parent=None):
|
def __init__(self, num, parent=None):
|
||||||
self._num = num
|
self._num = num
|
||||||
@ -226,12 +226,11 @@ class ExampleWidget(QWidget):
|
|||||||
return '<ExampleWidget {}>'.format(self._num)
|
return '<ExampleWidget {}>'.format(self._num)
|
||||||
|
|
||||||
|
|
||||||
def test_get_all_objects(qtbot):
|
def test_get_all_objects(stubs, monkeypatch):
|
||||||
# pylint: disable=unused-variable
|
# pylint: disable=unused-variable
|
||||||
w1 = ExampleWidget(1)
|
widgets = [ExampleWidget(1), ExampleWidget(2)]
|
||||||
qtbot.add_widget(w1)
|
app = stubs.FakeQApplication(all_widgets=widgets)
|
||||||
w2 = ExampleWidget(2)
|
monkeypatch.setattr(debug, 'QApplication', app)
|
||||||
qtbot.add_widget(w2)
|
|
||||||
|
|
||||||
root = QObject()
|
root = QObject()
|
||||||
o1 = ExampleObject(1, root)
|
o1 = ExampleObject(1, root)
|
||||||
|
Loading…
Reference in New Issue
Block a user