Qt:如何组织多个班级的单元测试? [英] Qt: How to organize Unit Test with more than one class?

查看:29
本文介绍了Qt:如何组织多个班级的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Qt 单元测试(子)项目,它为我生成一个类(主要由 QTEST_APPLESS_MAIN 生成).我可以从 Qt Creator 中作为控制台应用程序启动它.

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. 如果这些类只有测试"槽(private Q_SLOTS),则不会调用这些方法,而只会调用具有QTEST_APPLESS_MAIN的类的方法
  2. 由于只能有一个 main(..),所以我不能将 QTEST_APPLESS_MAIN 与项目中的多个类一起使用(对吗?)
  3. 当然,我可以使用包含 main 的一个类手动连接"(附加)类中的插槽,但这非常乏味.
  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?

附注:在"在项目中使用 QT 单元测试 - 主冲突(...) 函数 " a 博客提到了,但是,我无法下载描述解决方案的 zip.

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 单元测试项目中完成测试两个(或更多)类的方法是确保每个类被测试有一个相应的测试类,并且您已经创建了一个自定义 int main 来执行每个测试类.

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.

例如:

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;
}

显然,不同的测试类可以分布在多个翻译单元中,然后简单地包含在带有 int main 的翻译单元中.不要忘记包含适当的 .moc 文件.

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.

这篇关于Qt:如何组织多个班级的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆