Skip to content

Commit f024fd7

Browse files
authored
Merge 1518ce5 into a29570d
2 parents a29570d + 1518ce5 commit f024fd7

File tree

9 files changed

+1069
-2
lines changed

9 files changed

+1069
-2
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ New
66

77
* From this version onward, ``pytest-qt`` is licensed under the MIT license (`#134`_).
88

9+
* New ``qtmodeltester`` fixture to test ``QAbstractItemModel`` subclasses.
10+
Thanks `@The-Compiler`_ for the initiative and port of the original C++ code
11+
for ModelTester (`#63`_).
12+
913
.. _#134: https://github.com/pytest-dev/pytest-qt/issues/134
14+
.. _#63: https://github.com/pytest-dev/pytest-qt/pull/63
1015

1116

1217
Breaking changes

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pytest-qt
2121
logging
2222
signals
2323
virtual_methods
24+
modeltester
2425
app_exit
2526
reference
2627
changelog

docs/modeltester.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Model Tester
2+
============
3+
4+
.. versionadded:: 1.6
5+
6+
``pytest-qt`` includes a fixture that helps testing
7+
`QAbstractItemModel`_ implementations. The implementation is copied
8+
from the C++ code as described on the `Qt Wiki <http://wiki.qt.io/Model_Test>`_,
9+
and it continuously checks a model as it changes, helping to verify the state
10+
and catching many common errors the moment they show up.
11+
12+
Some of the conditions caught include:
13+
14+
* Verifying X number of rows have been inserted in the correct place after the signal ``rowsAboutToBeInserted()`` says X rows will be inserted.
15+
* The parent of the first index of the first row is a ``QModelIndex()``
16+
* Calling ``index()`` twice in a row with the same values will return the same ``QModelIndex``
17+
* If ``rowCount()`` says there are X number of rows, model test will verify that is true.
18+
* Many possible off by one bugs
19+
* ``hasChildren()`` returns true if ``rowCount()`` is greater then zero.
20+
* and many more...
21+
22+
To use it, create a instance of your model implementation, fill it with some
23+
items and call ``qtmodeltester.check``:
24+
25+
.. code-block:: python
26+
27+
def test_standard_item_model(qtmodeltester):
28+
model = QStandardItemModel()
29+
items = [QStandardItem(str(i)) for i in range(4)]
30+
model.setItem(0, 0, items[0])
31+
model.setItem(0, 1, items[1])
32+
model.setItem(1, 0, items[2])
33+
model.setItem(1, 1, items[3])
34+
qtmodeltester.check(model)
35+
36+
If the tester finds a problem the test will fail with an assert pinpointing
37+
the issue.
38+
39+
The following attribute that may influence the outcome of the check depending
40+
on your model implementation:
41+
42+
* ``data_display_may_return_none`` (default: ``False``): While you can
43+
technically return ``None`` (or an invalid ``QVariant``) from ``data()``
44+
for ``QtCore.Qt.DisplayRole``, this usually is a sign of
45+
a bug in your implementation. Set this variable to ``True`` if this really
46+
is OK in your model.
47+
48+
The source code was ported from `modeltest.cpp`_ by `Florian Bruhin`_, many
49+
thanks!
50+
51+
.. _modeltest.cpp: http://code.qt.io/cgit/qt/qtbase.git/tree/tests/auto/other/modeltest/modeltest.cpp
52+
53+
.. _Florian Bruhin: https://github.com/The-Compiler
54+
55+
.. _QAbstractItemModel: http://doc.qt.io/qt-5/qabstractitemmodel.html

0 commit comments

Comments
 (0)