简体   繁体   English

大型Qt项目的单元测试

[英]Unit test large Qt project

I have a fairly large Qt GUI project, which unfortunately lacks proper unit tests. 我有一个相当大的Qt GUI项目,不幸的是,它缺少适当的单元测试。

I'm planning to have a test project with a test source file for every class. 我打算为每个课程建立一个带有测试源文件的测试项目。

I digged through a lot of Qt's official docs, however there are some questions left: 我浏览了许多Qt的官方文档,但是还存在一些问题:

  • How can I create a test suite? 如何创建测试套件? It seems all docs and tutorials cover only writing one test, but how do I bundle my unit tests efficiently? 似乎所有文档和教程仅涵盖编写一项测试,但是如何有效捆绑单元测试?
  • How can I integrate the test execution into Qt Creator? 如何将测试执行集成到Qt Creator中? Maybe running the tests as post-build step would be interesting? 也许在构建后的步骤中运行测试会很有趣?
  • Should I add all normal source files to the test project or is there a way to create a static lib? 我应该将所有常规源文件添加到测试项目中还是可以创建静态库?

Any pointers to tutorials covering hint testing of a larger Qt project are highly appreciated. 任何指向涵盖较大Qt项目的提示测试的教程的指针都将受到高度赞赏。

I did not have a lot of time to do perfect unit testing in Qt. 我没有很多时间在Qt中进行完善的单元测试。 I did, however, figure out how to get the main plumbing working. 但是,我确实弄清楚了如何使主管道正常工作。 I hope that this can help you, although you could probably do a better job than I did by studying the Writing Unit Tests guide . 我希望这可以为您提供帮助,尽管您可能会比学习“ 写作单元测试”指南做得更好。

How can I create a test suite? 如何创建测试套件? It seems all docs and tutorials cover only writing one test, but how do I bundle my unit tests efficiently? 似乎所有文档和教程仅涵盖编写一项测试,但是如何有效捆绑单元测试?

You can organize "suites" by collecting unit tests (which are slots on objects) into objects and composing those objects. 您可以通过将单元测试(对象上的插槽)收集到对象中并组成这些对象来组织“套件”。

test_example.pro test_example.pro

TARGET = test_example
TEMPLATE = app
CONFIG += testcase
QT += testlib core

... INCLUDEPATH, SOURCES, HEADERS, etc...

main.cpp main.cpp中

#include <QtTest/QtTest>
#include "MainTest.h"

// This macro introduces an event loop
QTEST_MAIN(MainTest)

MainTest.h MainTest.h

class MainTest : public QObject
{
  Q_OBJECT

private slots:

  // Each slot is a unit test
  void init()
  {
  }

  // Create a "test suite" by bundling tests into objects
  void runFirstSuite()
  {
    FirstSuiteUiTest first;
    first.test();  // runs 10 tests

    FirstSuiteIoTest second;
    second.test();  // runs 999 tests

    ...
  }

  void SecondSuite() 
  {
    QFAIL("This suite is not really a suite, and it always fails");
  }
};

You can also organize tests into physical file structures. 您还可以将测试组织到物理文件结构中。 I used a subdirs project as my root .pro file so I could build artifacts (dynamic and static archives, binaries) and run many collections of unit tests. 我使用subdirs项目作为根.pro文件,因此可以构建构件(动态和静态档案,二进制文件)并运行许多单元测试集合。

TEMPLATE = subdirs
CONFIG += ordered

SUBDIRS += src/core
SUBDIRS += src/some_dll
SUBDIRS += src/test/awesome_core_test_1 #Holds many suites
SUBDIRS += src/test/awesome_core_test_2 #Holds many other suites!

core.depends = core
awesome_core_test_1.depends = core
awesome_core_test_2.depends = core

So by organizing unit tests into object compositions and physical files, you can some granularity of organization. 因此,通过将单元测试组织到对象组成和物理文件中,可以提高组织的粒度。 There may be better or different ways, but I am not sure. 可能有更好的方法或不同的方法,但是我不确定。

How can I integrate the test execution into Qt Creator? 如何将测试执行集成到Qt Creator中? Maybe running the tests as post-build step would be interesting? 也许在构建后的步骤中运行测试会很有趣?

Add CONFIG += testcase into your .pro file. CONFIG += testcase添加到您的.pro文件中。 This creates a build target. 这将创建一个构建目标。 Invoke the build and tests by running make check 通过运行make check调用构建和测试

Should I add all normal source files to the test project or is there a way to create a static lib? 我应该将所有常规源文件添加到测试项目中还是可以创建静态库?

You can do either, just like in any other project. 就像在其他任何项目中一样,您可以执行任何操作。

How can I create a test suite? 如何创建测试套件? It seems all docs and tutorials cover only writing one test, but how do I bundle my unit tests efficiently? 似乎所有文档和教程仅涵盖编写一项测试,但是如何有效捆绑单元测试?

In "Qt unit test class" each private slot is a unit test. 在“ Qt单元测试类”中,每个专用插槽都是一个单元测试。 Not exists other standard ways for adding new tests, but exist non documentary qmake feature. 不存在其他用于添加新测试的标准方法,但是存在非记录qmake功能。 You can read about in this forum thread 您可以在此论坛主题中阅读

How can I integrate the test execution into Qt Creator? 如何将测试执行集成到Qt Creator中? Maybe running the tests as post-build step would be interesting? 也许在构建后的步骤中运行测试会很有趣?

You can add CONFIG+=testcase test project and this will generate additional make target - check. 您可以添加CONFIG + = testcase测试项目,这将生成其他生成目标-检查。 Of course, you can add this build step anywhere. 当然,您可以在任何地方添加此构建步骤。 Also, you can read about here - Writing_Unit_Tests 另外,您可以在此处阅读有关内容-Writing_Unit_Tests

Should I add all normal source files to the test project or is there a way to create a static lib? 我应该将所有常规源文件添加到测试项目中还是可以创建静态库?

You can create static or dynamic lib with sources and link them to test project, or you can add sources to test project like here 您可以创建静态或动态的lib用代码,将其链接到测试项目,也可以添加源测试项目像这里

By the way, you can watch example of unit testing with Qt here 顺便说一下,您可以在这里观看使用Qt进行单元测试的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM