Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I access the Ui elements of a Dialog from within a Unit Test?

How do I access the Ui elements of a Dialog from within a Unit Test?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scumpyt
    wrote on last edited by
    #1

    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:
    void TestMTSettings::testSettingsDialog2_1()
    {
    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);
    

    }@

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tzander
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        scumpyt
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tzander
          wrote on last edited by
          #4

          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>

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #5

            moved this thread to desktop forum

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved