|
| 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