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

后端 未结 6 1511
温柔的废话
温柔的废话 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:31

    As per the solution you linked to, the way to accomplish testing two (or more) classes within a single Qt unit test project is to ensure that each class to be tested has a corresponding test class, and that you've created a custom int main that executes each test class.

    For example:

    class TestClassA : public QObject
    {
       Q_OBJECT
    public:
       TestClassA();
    
       ...
    
    private Q_SLOTS:
       void testCase1();
       ...
    };
    
    class TestClassB : public QObject
    {
       Q_OBJECT
    public:
       TestClassB();
    
       ...
    
    private Q_SLOTS:
       void testCase2();
       ...
    };
    
    void TestClassA::testCase1()
    {
       // Define test here.
    }
    
    void TestClassB::testCase2()
    {
       // Define test here.
    }
    
    // Additional tests defined here.
    
    // Note: This is equivalent to QTEST_APPLESS_MAIN for multiple test classes.
    int main(int argc, char** argv)
    {
       int status = 0;
       {
          TestClassA tc;
          status |= QTest::qExec(&tc, argc, argv);
       }
       {
          TestClassB tc;
          status |= QTest::qExec(&tc, argc, argv);
       }
       return status;
    }
    

    Obviously, the different test classes can be spread out over multiple translation units, then simply included in the translation unit with your int main. Don't forget to include the appropriate .moc files.

提交回复
热议问题