Qt: How to organize Unit Test with more than one class?

后端 未结 6 1530
温柔的废话
温柔的废话 2020-12-29 03:10

I have a Qt Unit test (sub)project, which generates me one class (with the main generated by QTEST_APPLESS_MAIN).I can start this from within Qt Creator as cons

6条回答
  •  情话喂你
    2020-12-29 03:19

    Based in the accepted answer and if you are using C++11 you could be interested in a solution using lambdas. It avoids you write the same code everytime. Although you can replace the lambda with a function, I think a lambda is cleaner.

    #include 
    
    #include "test1.h"
    #include "test2.h"
    
    
    int main(int argc, char** argv)
    {
       int status = 0;
       auto ASSERT_TEST = [&status, argc, argv](QObject* obj) {
         status |= QTest::qExec(obj, argc, argv);
         delete obj;
       };
    
       ASSERT_TEST(new Test1());
       ASSERT_TEST(new Test2());
    
       return status;
    }
    
    #ifndef TEST1_H
    #define TEST1_H
    

    Sample test

    #include 
    
    class Test1 : public QObject
    {
        Q_OBJECT
    
      private Q_SLOTS:
        void testCase1();
    };
    

提交回复
热议问题