[Solved] Odd problems with QTest on new installation

  • Hi guys!

    I am new to Qt. And to this forum as well, of course. Have been playing around with it a little bit and quite like it so far. Now I try to get my head around some of the nice 'services' one get used to in modern system. Right now I am trying out the Unit testing functionality / QTest. But there I find some odd behavior I can not easily describe, but I will try.

    First, I read an example like this:

    #include <QtWidgets>
    #include <QtTest/QtTest>

    class TestGui: public QObject

    private slots:
    void testGui();

    void TestGui::testGui()
    QLineEdit lineEdit;

    QTest::keyClicks(&lineEdit, "hello world");
    QCOMPARE(lineEdit.text(), QString("hello world"));


    The #include <QtTest/QtTest> does not really work for me, but <QTest> does. Along goes the <QtWidgets> where my system likes <QWidgets> only, otherwise it will complain about this missing path.
    Also, my QTest object doesn't know any keyClicks function, have been playing around with the includes alot but couldn't get this to work.

    Searching further on for some testing of the gui I came across a different example:

    QTestEventList events1, events2;
    events1.addKeyClick(Qt::Key_W, Qt::AltModifier);
    events1.simulate(win); // didn't type 'hello'

    events2.simulate(le); // did type 'world'

    But now the events on my system don't know any simulate function. The setting up of the event list (I only used events2) works fine, but the application via simulate is just not possible...

    At this point I wonder if there is some general problem with versions, integration, or something else in my system? I installed the system only recently, it's Qt Creator 3.1.1 (open source), Based on Qt 5.2.1 from May 16, 2014, 04:17:40.

    Does that ring a bell for anyone who could give me a hint what's wrong in the whole?

    Thanks a lot,

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should also add on which OS you are getting these issues and how you installed Qt

  • If you want to include all the QtTest framework, then you want to do:

    #include <QtTest>

    Doing #include <QtTest/QtTest> wouldn't find the include, at least not in a normal Qt install.

    If however you just want to include the QTest namespace than simply:

    #include <QTest>

    would do that for you.

    Also make sure in your project file, whatever.pro that you add:

    QT += testlib

    Try those things and see if keyClicks(...) works for you. It should after that.

  • Hi, thanks for the replies!

    SGaist, I am working on Windows 7 Professional Service Pack 1 (Corporate environment).

    ambershark, I tried your hints again, but it still won't work properly:

    D:\DataSource\Qt\ADNW_Service\ServiceToolsSharedQt\Test\ServiceToolsSharedQt_Test\testautostatemachine.cpp:19: Fehler: 'keyClicks' is not a member of 'QTest'
    QTest::keyClicks( &lineEdit, "hello world" );

    And also:

    D:\DataSource\Qt\ADNW_Service\ServiceToolsSharedQt\Test\ServiceToolsSharedQt_Test\testautostatemachine.cpp:26: Fehler: 'class QTestEventList' has no member named 'simulate'
    events2.simulate( &lineEdit ); // did type 'world'

    My .pro file looks like this:

    Project created by QtCreator 2014-06-30T14:14:29


    QT += core testlib

    TARGET = ServiceToolsSharedQt_Test
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp

    HEADERS +=

    At least I understood that both (QtTest & QTest) seem to be two different testing frameworks, ot at least two different implementations, is that true? Still, it didn't help me, to solve the described issue.

    Any more ideas about this effect?


  • No, QtTest and QTest are part of the same framework. When you do things like #include <QtTest> or #include <QtXml> you are basically including all of that framework.

    For normal code this is a bad idea as it will seriously impact build times. Good practice is to include what you actually need, so in this case for you it would just be #include <QTest>. Or for instance, sticking with QtXml example above, if you wanted just QXmlReader you would #include <QXmlReader> not the whole <QtXml>.

    As for your problem....

    I think, based on your pro file, that it is because you are using core Qt only. Since keyClicks is a GUI thing you need to change your project file at have this:

    QT += testlib core gui widgets

    By adding gui and widgets you should get the keyClicks support you need.

    Let me know if that doesn't work.

  • Wow, that works, thanks a lot, ambershark! Still I am a little surprised: I mean, the system knew the object (QTest) and would also show me it's members. But not the keyClicks(). Now all I changed is the library list as you advised and now the new members show up. Obviously, the same happens with simulate() and the EventList... From my perspective the system would know about the object or it would not, but this is like knowing only half of the object? I think I lack the understanding of some concept behind here?

    Maybe that goes together with my mixing of the paths. From java I know a similar package concept, that you could include the path (<QtTest/*> ???) or a single object (<QtTest/QtTest>), but then <QTest> would be something totally different. How would the system know that <QTest> references back to a specific object of the package <QtTest>?


  • No problem, glad you're all sorted out. :)

    If you look at the Qt source code you will see things like this:

    class QTestKeyClicksEvent: public QTestEvent
    inline QTestKeyClicksEvent(const QString &keys, Qt::KeyboardModifiers modifiers, int delay)
    : _keys(keys), _modifiers(modifiers), _delay(delay) {}
    inline QTestEvent *clone() const { return new QTestKeyClicksEvent(*this); }

    #ifdef QT_WIDGETS_LIB
    inline void simulate(QWidget *w)
    QTest::keyClicks(w, _keys, _modifiers, _delay);

    QString _keys;
    Qt::KeyboardModifiers _modifiers;
    int _delay;

    I just grabbed a random one (first that I saw). Anyway, in that you can see that the function is only defined if there is widget support in the project. So that is how it has some functionality from including qtest but not all of it.

    Qt actually does this a lot. One of the weirdest ones I saw was needing QT += gui for QRegExp. I have no idea why regular expression support would need the gui, lol.

  • Alright, thanks a lot for the answers, they indeed cleared the topic for me!


  • No problem. Make sure to rename your thread with [SOLVED] at the front for future people coming through. :)

Log in to reply