How do I access the Ui elements of a Dialog from within a Unit Test?
I have written my own dialog (derived from QDialog), and I want to UnitTest it with QTestLib, but I don't know how to get access to the various Ui elements within my UnitTest.
I am new to the QTestLib framework and Qt4, but have used Qt3 a lot, and other UnitTesting frameworks as well, so perhaps I am just missing something obvious, or perhaps there is a completely different way to approach this. Any suggestions would be very welcome.
@// Here is the Unit Test code that fails:
SettingsDialog2* sd = new SettingsDialog2(); // Create the dialog
MultTesterSettings mts; // Create the non-Gui parameter model holding the
// various parameters default values.
mts.setDigits(3,5); // Manually set a couple of the parameters. sd->setPM(&mts); // Set the parameter model into the dialog. // This sets all of the various elements of the // dialog based on the values in the parameter model. // At this point, I would like to check that the various elements have been set correctly, // with code something like the following, but I can't seem to get access to the Ui elements // under my dialog. The Ui namespace is always private, of course. Ui::SettingsDialog2 *ui = sd->ui; QVERIFY(ui->startDigitSpinBox()->value() == 3); QVERIFY(ui->endDigitSpinBox()->value() == 5);
if you want to test the internal state of a certain class, either a UI or any other class, you need to make it possible by changing the code.
The easiest solution is to make the internal stuff protected and inside your unit test you inherit from the SettingsDialog and add new public getters which then return the stuff that the SettingsDialog has marked protected. This new class will only ever be used in the test, so it won't break encapsulation.
I'm not sure about needing to add new public getters/setters. The spirit of UnitTesting should be less intrusive than this, IMHO, however the idea of inheriting from SettingsDialog is an interesting one.
Thanks a bunch for the help!!!
The new public getters setters are done in a new class that inherits from your class, and that inheriting is done inside your unit test only so I don't think this is something that is too intrusive. The normal class will not have the public getters/setters.
Another method is to use
QObject::findChildren<T> or QObject::findChild<T>
moved this thread to desktop forum