Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Introduction to GUI Qt Test???



  • Good day,
    I have created a subdir project that contains first a Qt GUI project of a currency converter (see picture below) and a second project, a QtTest to test the converter.
    72c0544a-9b48-493c-b3e9-2763e015af68-image.png
    Here is the .pro file of the subdir project:

    TEMPLATE = subdirs
    
    SUBDIRS += \
        CurrencyConverter \
        QtTest
    
    tests.subdirs = QtTest
    app.subdirs = CurrencyConverter
    
    QtTest.depends = CurrencyConverter
    
    

    and here is the .pro file of the Qt Test project

    QT += testlib gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    DEFINES += QT_DEPRECATED_WARNINGS
    
    CONFIG += qt warn_on depend_includepath testcase
    
    TEMPLATE = app
    INCLUDEPATH += ../CurrencyConverter
    
    SOURCES +=  tst_testconverter.cpp
    

    When I add the view of the currency project in my test case, I got an undefined reference of the constructor of the View. What am doing wrong?

    #include <QtTest>
    #include <QCoreApplication>
    #include "view.h"
    
    // add necessary includes here
    
    class testConverter : public QObject
    {
        Q_OBJECT
    
    public:
        testConverter();
        ~testConverter();
    
    private slots:
        void initTestCase();
        void cleanupTestCase();
        void test_case1();
    
    };
    
    testConverter::testConverter()
    {
    
    }
    
    testConverter::~testConverter()
    {
    
    }
    
    void testConverter::initTestCase()
    {
    
    }
    
    void testConverter::cleanupTestCase()
    {
    
    }
    
    void testConverter::test_case1()
    {
        View view; // the problem occurs here
    
    }
    
    QTEST_MAIN(testConverter)
    
    #include "tst_testconverter.moc"
    

  • Lifetime Qt Champion

    Hi
    But does the view constructor takes no arguments ?
    Normally it takes a parent.



  • @mrjj
    thanks for your reply, Here is the view:

    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class View; }
    QT_END_NAMESPACE
    
    class Controller;
    
    class View : public QMainWindow
    {
        Q_OBJECT
    
    public:
        View(QWidget *parent = nullptr);
        ~View();
        void setController(Controller *controller);
    
    public slots:
        void set_amount(const QString& val);
        void get_recieve();
    
    private:
        Ui::View *ui;
        Controller *m_controller;
        friend class testConverter;
    };
    

    and its cpp

    View::View(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::View)
        , m_controller(nullptr)
    {
        ui->setupUi(this);
        ui->amount_el->setText(QString::number(0.00));
    
        connect(ui->amount_el, SIGNAL(textChanged(const QString &)),
                this, SLOT(set_amount(const QString &)));
    
        connect(ui->convert_btn, SIGNAL(clicked()),
                this, SLOT(get_recieve()));
    }
    

    The parent by default is nullptr



  • The sub-project is not linked to View, so it does not know how to create a View, that's "undefined reference of the constructor" means.
    But I don't know what's the usual way to test an app's internal class...



  • I'm not familiar with Qt Test, I'm just trying if I can simulate mouse click event I could probably test the GUI with but I'm not sure if I'm in the right path


  • Lifetime Qt Champion

    Hi,

    @Bonnie is right. Your current issue is that you don't build the class along your test hence your error.

    You have several way of solving this:

    • make a library out of the components of your application and link against it. You app project will mainly be a main.cpp file creating the needed objects using that library as well.
    • stay as you are currently and build the files needed within the test sub project.


  • Hi @SGaist
    I have tried the first bullet by refactoring the code having an app as main project that just call my lib:

    #include "mainwindow.h"
    #include "currencyconverter.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
    {
        CurrencyConverter *currency = new CurrencyConverter(parent);
    
        setCentralWidget(currency);
    }
    

    and my main .pro file looks like this:

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++11
    
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    INCLUDEPATH += \
                  ../CurrencyConverter
    
    LIBS += \
                -L../CurrencyConverter
    
    SOURCES += \
        main.cpp \
        mainwindow.cpp
    
    HEADERS += \
        mainwindow.h
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    When compiling, I'm still getting undefined reference to CurrencyConverter at line where currency pointer is initiaze. the first bullet seems not working for me.
    for the second bullet, what you do you mean by "build the files needed within the test sub project." ho do you do that in .pro file?


  • Lifetime Qt Champion

    What OS are you on ?


Log in to reply