pytest-dev / pytest-qt

pytest plugin for Qt (PyQt5/PyQt6 and PySide2/PySide6) application testing
https://pytest-qt.readthedocs.io
MIT License
410 stars 70 forks source link

How to use QAbstractItemModelTester? #530

Closed marcel-goldschen-ohm closed 1 year ago

marcel-goldschen-ohm commented 1 year ago

I've read the docs at https://doc.qt.io/qt-6/qabstractitemmodeltester.html and https://pytest-qt.readthedocs.io/en/latest/modeltester.html, but I still fail to understand how to implement QAbstractItemModelTester to test a custom QAbstractItemModel.

The following code/pseudocode (in my actual code I instantiate the model/view and add data properly) does not seem to do anything or else I am not properly configuring things to get the reports:

app = QApplication(sys.argv)
model = ... # this is a custom tree model derived from QAbstractItemModel
# add some data to model...
view = ... # custom tree view derived form QTreeView
view.setModel(model)
view.show()
tester = QAbstractItemModelTester(model, QAbstractItemModelTester.FailureReportingMode.Fatal)
app.exec()
# GOOD: at this point the model is displayed as expected
# BAD: now I manipulate the model in a way that causes a seg fault,
#    but I don't see how to get any info from QAbstractItemModelTester as to why the seg fault is occurring

I'm hoping to get some debug info as to why the seg fault is occurring (yes, I've already tried commenting tons of things in my code, but I still haven't been able to track it down, so I'm hoping to use QAbstractItemModelTester). However, I don't see where QAbstractItemModelTester might be logging any such info if it is even working at all. Probably I'm not understanding how to implement it correctly, so any help would be awesome as I find the docs mentioned above uninformative. Note, I am using VSCode if that matters.

Cheers

The-Compiler commented 1 year ago

I've never used QAbstractItemModelTester outside of pytest-qt, but at a glance, your code seems like it should work.

However, this isn't really all too related to pytest-qt so far. Did you try something like the example in the pytest-qt docs you linked to?

marcel-goldschen-ohm commented 1 year ago

Thanks for the quick response @The-Compiler.

It seems my code above did work as I eventually got some error messages when doing something else (they are so cryptic as to not be very useful, but at least it's working). For whatever reason, no error messages are reported during the segfault behavior that I was originally looking at, so I had no way of knowing if the tester was working or not. Perhaps some initialization message would be helpful for sanity's sake.

Cheers

The-Compiler commented 1 year ago

QAbstractItemModelTester is coming from Qt. All pytest-qt does is integrating it into pytest.

marcel-goldschen-ohm commented 1 year ago

I am starting to realize I have no idea how to use pytest. Do the docs have a complete example somewhere? All I see are tiny bits of code or floating functions that are not helping me understand how to set things up.

The-Compiler commented 1 year ago

The function in the docs is a complete example (minus the imports) which you would run with pytest. See the pytest docs as well.

marcel-goldschen-ohm commented 1 year ago

Yes, well that's part of the rub for a newbie. Anyway, I'll clearly have to read up on pytest. Thanks again @The-Compiler.

Cheers,

The-Compiler commented 1 year ago

I can see how this is confusing if you've never used pytest before - but I don't think it's the job of the pytest-qt documentation to explain how pytest works. I see that as a prerequisite, just like basic Qt or Python knowledge.

Out of curiosity, how did you find out about pytest-qt if it wasn't via pytest?

marcel-goldschen-ohm commented 1 year ago

Fair enough.

QAbstractItemModelTester came up during searches for debugging a custom tree model derived from QAbstractItemModel, which led to pytest-qt.