Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

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

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 console app.

Q: How would I add additional classes as test cases to this particular project.

  1. If these classes only have "test" slots (private Q_SLOTS), the methods are not called, but just the ones of the class with QTEST_APPLESS_MAIN
  2. Since there can be only one main(..), I cannot use QTEST_APPLESS_MAIN with more than one class in the project (is that correct?)
  3. Of course, I can manually "wire" the slots in the (additional) classes with the one class containing the main, but this is very tedious.

So what is the best way to run unit test over several classes in a unit test project?

PS: In " Using QT Unit Tests in a project - conflicting main(...) functions " a Blog is mentioned, however, I cannot download the zip describing the solution.

Qt Unit Test subproject

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...