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. Introduction to GUI Qt Test???
QtWS25 Last Chance

Introduction to GUI Qt Test???

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 444 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.
  • MassiM Offline
    MassiM Offline
    Massi
    wrote on last edited by
    #1

    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"
    

    Software Design Engineer at Ford - Canada

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      MassiM 1 Reply Last reply
      0
      • mrjjM mrjj

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

        MassiM Offline
        MassiM Offline
        Massi
        wrote on last edited by
        #3

        @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

        Software Design Engineer at Ford - Canada

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by
          #4

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

          1 Reply Last reply
          1
          • MassiM Offline
            MassiM Offline
            Massi
            wrote on last edited by
            #5

            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

            Software Design Engineer at Ford - Canada

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • MassiM Offline
                MassiM Offline
                Massi
                wrote on last edited by
                #7

                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?

                Software Design Engineer at Ford - Canada

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  What OS are you on ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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