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

QTest problem



  • 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


  • Lifetime Qt Champion

    Hi,

    Where is SectionSettings defined ?



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


  • Lifetime Qt Champion

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



  • @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;
            }
        }
    
    };
    

  • Qt Champions 2019

    @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?



  • @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
    


  • @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 toSection::Section(SectionSettings&, QWidget*)'
    error: undefined reference to `Section::~Section()'


  • Qt Champions 2019

    @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
    


  • @jsulm Thanks, I solved the problem


Log in to reply