Qt Test class in subdir project can't find ui_mainwindow.h from production subdir project

  • Hi.

    I have set a project up to contain two subdir projects: one normal project and one unit test project. I'm trying to create a unit test for the MainWindow class in the normal project by creating a MainWindowTest class in the test project. MainWindow now only contains a QPushButton myButton widget, and I'd like to test its existence and behaviour in MainWindowTest. An example test function:

    void MainWindowTest::initTestCase()
        QVERIFY2(win.myButton, "myButton doesn't exist");

    This is the test class:

    #include <QtCore/QCoreApplication>
    #include "../SomeProject/mainwindow.h"
    class MainWindowTest : public QObject
    private slots:
        void initTestCase();
        void cleanupTestCase();
        void test_case1();
        MainWindow win;

    And this is the folder hierarchy:
    --> SomeProject
    -----> GUI_Tests
    -----> SomeProject

    In the header of MainWindow class I declared MainWindowTest as friend, so that I can access private elements (aka. the widgets) of it in the test class:

    #include "ui_mainwindow.h"
    class MainWindow : public QMainWindow, private Ui::MainWindow
        friend class MainWindowTest;
        explicit MainWindow(QWidget *parent = nullptr);

    The problem seems to be that the compiler cannot find the ui_mainwindow.h file as long as the GUI_Test subdir is part of the entire project:
    ~/SomeProject/SomeProject/mainwindow.h:5: error: ui_mainwindow.h: No such file or directory
    #include "ui_mainwindow.h"

    If on the other hand I remove the GUI_Test subdir in the main project .pro file (only SomeProject remains the sole project), everything seems to compile normally.

    The question then arises: how can I make the GUI_Test subproject aware of the ui_mainwindow.h header that will only be generated later by UIC?

    Here's what I tried so far to solve the issue:

    1. add GUI_Tests.depends = SomeProject to the main .pro file --> didn't work
    2. add #include ui_mainwindow.h to the test .cpp file --> didn't work
    3. included all the source files of SomeProject to the Test Class's .pro file --> didn't work

    As I understand this should be a simple thing to do right? Creating unit tests for your GUI classes and then running them with each compile. Am I missing something? Any suggestions? Should I approach my ultimate goal (writing tests and running them automatically) differently?

    Thanks in advance.

  • Lifetime Qt Champion

    @PusRob said in Qt Test class in subdir project can't find ui_mainwindow.h from production subdir project:


    Can you show its pro/pri file?
    I think you need to add your ui file to FORMS there, just like in SomeProject.

  • Hi.

    This is the autogenerated .pro file of the GUI_test subdir project. I also added the ui file to forms, but still no luck:

    QT += testlib
    QT += gui
    CONFIG += qt warn_on depend_includepath testcase
    TEMPLATE = app
    SOURCES +=  tst_mainwindowtest.cpp
    FORMS += mainwindow.ui

    I also tried with FORMS += ../SomeProject/mainwindow.ui, but still no luck.
    I don't understand what else it needs. Any ideas?

    EDIT: ok, if I add QT += widgets then the header gets generated, but now I get undefined reference errors.

Log in to reply