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. QTest problem
Qt 6.11 is out! See what's new in the release blog

QTest problem

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.0k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    Creatorczyk
    wrote on last edited by
    #1

    Hi,
    I want make simply unit test for my class Section:

    class Section : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Section(SectionSettings& sectionSettings, QWidget *parent = nullptr);
    
        SectionSettings getParameters(){return sectionSettings;}
    
        ~Section();
    };
    

    My test pro file:

    QT += testlib
    QT += gui
    QT += widgets
    QT += core
    
    CONFIG += qt warn_on depend_includepath testcase
    
    TEMPLATE = app
    
    SOURCES += \ 
        testsection.cpp
        ../../GreenhouseApp/src/SectionWidget/section.cpp
    
    HEADERS += \
        testsection.h
        ../../GreenhouseApp/src/SectionWidget/section.h
    

    Header file:

    #include <QtTest>
    #include <QCoreApplication>
    
    #include "../../GreenhouseApp/src/SectionWidget/section.h"
    
    class TestSection : public QObject
    {
        Q_OBJECT
    
    private slots:
        void testConstructor();
    };
    

    Source file:

    #include "testsection.h"
    
    void TestSection::testConstructor()
    {
        SectionSettings settings;
        Section section(settings);
    }
    
    QTEST_MAIN(TestSection)
    

    But when i try complie test i get error :

    error: undefined reference to `Section::Section(SectionSettings&, QWidget*)'
    error: undefined reference to `Section::Section(SectionSettings&, QWidget*)'
    error: undefined reference to `Section::~Section()'
    

    What am I doing wrong? The path to section.h is correct

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

      Hi,

      Where is SectionSettings defined ?

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

      C 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Where is SectionSettings defined ?

        C Offline
        C Offline
        Creatorczyk
        wrote on last edited by
        #3

        @SGaist In the other file "settingsdialog.h" which is included in "section.h"

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

          And the implementation is in the corresponding .cpp that you do not include in your test project ?

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

          C 1 Reply Last reply
          0
          • SGaistS SGaist

            And the implementation is in the corresponding .cpp that you do not include in your test project ?

            C Offline
            C Offline
            Creatorczyk
            wrote on last edited by
            #5

            @SGaist I added this file to .pro file and test's header file and it doesn't change anything. I still have the same error.

            But i move code from function to test header file:

            #include <QtTest>
            #include <QtWidgets>
            #include <QCoreApplication>
            
            #include "../../GreenhouseApp/src/SectionWidget/section.h"
            #include "../../GreenhouseApp/src/SectionWidget/settingsdialog.h"
            
            class TestSection : public QObject
            {
                Q_OBJECT
            
            public:
                SectionSettings settings;
                Section section(settings);
            
            private slots:
                void testConstructor();
            };
            

            and I received an error:

            error: ‘settings’ is not a type
            

            My struct SectionSettings definition looks like:

            struct SectionSettings{
                quint8 sectionID;
                QString section_name;
                QString picture_path;
                quint8 temperature_expected;
                quint8 humidity_expected;
                quint8 insolation_expected;
                quint8 time_hours;
                quint8 time_minutes;
            
                SectionSettings(quint8 id = 0,
                                QString name = "Section",
                                QString path = "",
                                quint8 temp = 0,
                                quint8 hum = 0,
                                quint8 ins = 0,
                                quint8 hours = 0,
                                quint8 minutes = 0)
                     :sectionID(id),
                      section_name(name),
                      picture_path(path),
                      temperature_expected(temp),
                      humidity_expected(hum),
                      insolation_expected(ins),
                      time_hours(hours),
                      time_minutes(minutes){}
            
                bool operator ==(const SectionSettings &s){
                    if((this->sectionID == s.sectionID) &&
                            (this->section_name == s.section_name) &&
                            (this->picture_path == s.picture_path) &&
                            (this->temperature_expected == s.temperature_expected) &&
                            (this->humidity_expected == s.humidity_expected) &&
                            (this->insolation_expected == s.insolation_expected) &&
                            (this->time_hours == s.time_hours) &&
                            (this->time_minutes == s.time_minutes)){
                        return true;
                    }else{
                        return false;
                    }
                }
            
            };
            
            jsulmJ 1 Reply Last reply
            0
            • C Creatorczyk

              @SGaist I added this file to .pro file and test's header file and it doesn't change anything. I still have the same error.

              But i move code from function to test header file:

              #include <QtTest>
              #include <QtWidgets>
              #include <QCoreApplication>
              
              #include "../../GreenhouseApp/src/SectionWidget/section.h"
              #include "../../GreenhouseApp/src/SectionWidget/settingsdialog.h"
              
              class TestSection : public QObject
              {
                  Q_OBJECT
              
              public:
                  SectionSettings settings;
                  Section section(settings);
              
              private slots:
                  void testConstructor();
              };
              

              and I received an error:

              error: ‘settings’ is not a type
              

              My struct SectionSettings definition looks like:

              struct SectionSettings{
                  quint8 sectionID;
                  QString section_name;
                  QString picture_path;
                  quint8 temperature_expected;
                  quint8 humidity_expected;
                  quint8 insolation_expected;
                  quint8 time_hours;
                  quint8 time_minutes;
              
                  SectionSettings(quint8 id = 0,
                                  QString name = "Section",
                                  QString path = "",
                                  quint8 temp = 0,
                                  quint8 hum = 0,
                                  quint8 ins = 0,
                                  quint8 hours = 0,
                                  quint8 minutes = 0)
                       :sectionID(id),
                        section_name(name),
                        picture_path(path),
                        temperature_expected(temp),
                        humidity_expected(hum),
                        insolation_expected(ins),
                        time_hours(hours),
                        time_minutes(minutes){}
              
                  bool operator ==(const SectionSettings &s){
                      if((this->sectionID == s.sectionID) &&
                              (this->section_name == s.section_name) &&
                              (this->picture_path == s.picture_path) &&
                              (this->temperature_expected == s.temperature_expected) &&
                              (this->humidity_expected == s.humidity_expected) &&
                              (this->insolation_expected == s.insolation_expected) &&
                              (this->time_hours == s.time_hours) &&
                              (this->time_minutes == s.time_minutes)){
                          return true;
                      }else{
                          return false;
                      }
                  }
              
              };
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Creatorczyk said in QTest problem:

              error: ‘settings’ is not a type

              Of course it's not - it is a member variable, do you mean:

              Section section(SectionSettings);
              

              ?

              Can you show your current pro file for the test project?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 2 Replies Last reply
              1
              • jsulmJ jsulm

                @Creatorczyk said in QTest problem:

                error: ‘settings’ is not a type

                Of course it's not - it is a member variable, do you mean:

                Section section(SectionSettings);
                

                ?

                Can you show your current pro file for the test project?

                C Offline
                C Offline
                Creatorczyk
                wrote on last edited by
                #7

                @jsulm

                My .pro file:

                QT += testlib
                QT += gui
                QT += widgets
                
                CONFIG += qt warn_on depend_includepath testcase
                
                TEMPLATE = app
                
                SOURCES += \ 
                    testsection.cpp
                    ../../GreenhouseApp/src/SectionWidget/section.cpp
                
                HEADERS += \
                    testsection.h
                    ../../GreenhouseApp/src/SectionWidget/section.h
                
                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @Creatorczyk said in QTest problem:

                  error: ‘settings’ is not a type

                  Of course it's not - it is a member variable, do you mean:

                  Section section(SectionSettings);
                  

                  ?

                  Can you show your current pro file for the test project?

                  C Offline
                  C Offline
                  Creatorczyk
                  wrote on last edited by
                  #8

                  @jsulm I know what I made a mistake in:

                  Section section(settings);
                  

                  the compiler thinks it's a function declaration and not an object creation, and reports that the variable type is missing

                  But I still don't know why creating a Section object in the method "testConstructor()" I gets an error

                  void TestSection::testConstructor()
                  {
                      SectionSettings settings;
                      Section sections(settings);
                  }
                  

                  In function TestSection::testConstructor()': error: undefined reference to Section::Section(SectionSettings&, QWidget*)'
                  error: undefined reference to `Section::~Section()'

                  jsulmJ 1 Reply Last reply
                  0
                  • C Creatorczyk

                    @jsulm I know what I made a mistake in:

                    Section section(settings);
                    

                    the compiler thinks it's a function declaration and not an object creation, and reports that the variable type is missing

                    But I still don't know why creating a Section object in the method "testConstructor()" I gets an error

                    void TestSection::testConstructor()
                    {
                        SectionSettings settings;
                        Section sections(settings);
                    }
                    

                    In function TestSection::testConstructor()': error: undefined reference to Section::Section(SectionSettings&, QWidget*)'
                    error: undefined reference to `Section::~Section()'

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Creatorczyk said in QTest problem:

                    TestSection::testConstructor()': error: undefined reference to Section::Section(SectionSettings&, QWidget*)'
                    error: undefined reference to `Section::~Section()'

                    Apparently the linker can't find Section constructor and destructor definitions.
                    The reason is probably that your pro file is wrong. It should be

                    SOURCES += \ 
                        testsection.cpp \
                        ../../GreenhouseApp/src/SectionWidget/section.cpp
                    
                    HEADERS += \
                        testsection.h \
                        ../../GreenhouseApp/src/SectionWidget/section.h
                    

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    C 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @Creatorczyk said in QTest problem:

                      TestSection::testConstructor()': error: undefined reference to Section::Section(SectionSettings&, QWidget*)'
                      error: undefined reference to `Section::~Section()'

                      Apparently the linker can't find Section constructor and destructor definitions.
                      The reason is probably that your pro file is wrong. It should be

                      SOURCES += \ 
                          testsection.cpp \
                          ../../GreenhouseApp/src/SectionWidget/section.cpp
                      
                      HEADERS += \
                          testsection.h \
                          ../../GreenhouseApp/src/SectionWidget/section.h
                      
                      C Offline
                      C Offline
                      Creatorczyk
                      wrote on last edited by
                      #10

                      @jsulm Thanks, I solved the problem

                      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