QTest GUI getting started



  • I am trying to figure out how to use QTest to test my gui application, so I set up a simple program like the one in this posting Qt test empty window. (Using Qt 5.7 on Ubuntu)

    When I try to run it, I get the message tst_mytest.cpp:33: undefined reference to `MyMainWindow::MyMainWindow(QWidget)'*.

    This should be really simple but I can't figure out what it is.

    Thanks,
    Jas

    //tst_mytest.cpp
    
    #include <QString>
    #include <QtTest>
    #include <QCoreApplication>
    #include "../MyApp/mymainwindow.h"
    #include "../MyApp/ui_mymainwindow.h"
    
    #include <QDebug>
    #include <QPushButton>
    
    class MyTest : public QObject
    {
        Q_OBJECT
    
    public:
        MyTest();
    
    private Q_SLOTS:
        void initTestCase();
        void cleanupTestCase();
        void testCase1();
    
    private:
        MyMainWindow* MyMainWindow_ ;
    
    };
    
    MyTest::MyTest()
    {
    }
    
    void MyTest::initTestCase()
    {
        MyMainWindow_ = new MyMainWindow;
        MyMainWindow_->show();
    
    }
    
    void MyTest::cleanupTestCase()
    {
    }
    
    void MyTest::testCase1()
    {
        QVERIFY2(true, "Failure");
    }
    
    QTEST_MAIN(MyTest)
    
    #include "tst_mytest.moc"
    
    //MyTest.pro
    
    #-------------------------------------------------
    #
    # Project created by QtCreator 2017-11-06T10:12:26
    #
    #-------------------------------------------------
    
    QT       += widgets testlib core
    
    TARGET = tst_mytest
    CONFIG   += console
    CONFIG   -= app_bundle
    
    TEMPLATE = app
    
    
    SOURCES += tst_mytest.cpp
    DEFINES += SRCDIR=\\\"$$PWD/\\\"
    
    INCLUDEPATH += $$PWD/../MyApp
    
    //mymainwindow.cpp
    
    #include "mymainwindow.h"
    #include "ui_mymainwindow.h"
    
    MyMainWindow::MyMainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MyMainWindow)
    {
        ui->setupUi(this);
    }
    
    MyMainWindow::~MyMainWindow()
    {
        delete ui;
    }
    
    void MyMainWindow::on_pushButton_clicked()
    {
        if (ui->checkBox->isChecked())
            ui->checkBox->setChecked(false);
        else
            ui->checkBox->setChecked(true);
    }
    
    //mymainwindow.h
    
    #ifndef MYMAINWINDOW_H
    #define MYMAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MyMainWindow;
    }
    
    class MyMainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MyMainWindow(QWidget *parent = 0);
        ~MyMainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MyMainWindow *ui;
    };
    
    #endif // MYMAINWINDOW_H
    
    //MyApp.pro
    
    #ifndef MYMAINWINDOW_H
    #define MYMAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MyMainWindow;
    }
    
    class MyMainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MyMainWindow(QWidget *parent = 0);
        ~MyMainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MyMainWindow *ui;
    };
    
    #endif // MYMAINWINDOW_H
    
    //MySolution.pro
    
    TEMPLATE = subdirs  
    SUBDIRS += MyApp \
        MyTest
    
    


  • @jastmc said in QTest GUI getting started:

    MyMainWindow_ = new MyMainWindow;

    I should have said the error occurs on the line on the line

    MyMainWindow_ = new MyMainWindow;
    


  • [EDIT: please ignore this post, it's irrelevant and wrong. Sorry!]

    @jastmc
    Since the example you quote uses:

    mainWindow_ = new MainWindow();
    

    why do you write

    MyMainWindow_ = new MyMainWindow;
    

    and then ask why it doesn't behave same?


  • Qt Champions 2016

    @jastmc said in QTest GUI getting started:

    MyMainWindow_ = new MyMainWindow;

    undefined reference to `MyMainWindow::MyMainWindow(QWidget)'*.

    It says it dont know your constructor.

    I would completely delete the build folder, and run qmake + rebuild all.

    If error persists, please check that MyMainWindow.cpp is actual included into the .pro file
    so it gets linked to the tst_mytest project.


  • Lifetime Qt Champion

    Hi,

    In your test, you only build the test sources itself. What about the class you want to test ?



  • @JNBarchan said in QTest GUI getting started:

    @jastmc
    Since the example you quote uses:

    mainWindow_ = new MainWindow();
    

    why do you write

    MyMainWindow_ = new MyMainWindow;
    

    and then ask why it doesn't behave same?

    Whoops? For my own edification, does stinky C++ allow you to omit the () in new Something and call a default constructor? parameterless constructor? complain if multiple constructors? or what??



  • @SGaist OK. How do I make sure the class to be tested is built first?

    Regards
    Jas


  • Moderators

    @jastmc Add it to SOURCES and HEADERS in your pro file.


  • Qt Champions 2016

    @JNBarchan
    yes the new operator/compiler can construct an object if it can find
    a default constructor (ctor). The ctor is a special function and and that is why () can be omitted even one often consider it just a function call.

    https://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new



  • @jsulm Thanks, that was part of it.

    I then had to add

    INCLUDEPATH += $$PWD/../MyApp/ \
                    ../../build-MySolution-Desktop_Qt_5_7_0_GCC_64bit-Debug/MyApp/ \
    

    to the MyTest.pro file because the next message was that it could not find the ui_mymainwindow.h file. So that solved it.

    Regards,
    Jas


  • Lifetime Qt Champion

    You are missing the FORMS variable in your .pro file.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.