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. Qt Test class in subdir project can't find ui_mainwindow.h from production subdir project
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 653 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.
  • P Offline
    P Offline
    PusRob
    wrote on 13 Jun 2019, 14:31 last edited by PusRob
    #1

    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
    {
        Q_OBJECT
    
    public:
        MainWindowTest();
        ~MainWindowTest();
    
    private slots:
        void initTestCase();
        void cleanupTestCase();
        void test_case1();
    
    private:
        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
    {
        Q_OBJECT
    
        friend class MainWindowTest;
    public:
        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.

    J 1 Reply Last reply 14 Jun 2019, 04:29
    0
    • P PusRob
      13 Jun 2019, 14:31

      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
      {
          Q_OBJECT
      
      public:
          MainWindowTest();
          ~MainWindowTest();
      
      private slots:
          void initTestCase();
          void cleanupTestCase();
          void test_case1();
      
      private:
          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
      {
          Q_OBJECT
      
          friend class MainWindowTest;
      public:
          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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 14 Jun 2019, 04:29 last edited by
      #2

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

      GUI_Tests

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

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

      1 Reply Last reply
      1
      • P Offline
        P Offline
        PusRob
        wrote on 14 Jun 2019, 18:14 last edited by PusRob
        #3

        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.

        1 Reply Last reply
        0

        3/3

        14 Jun 2019, 18:14

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved