Qt Creator 中的单元测试设置

发布于 10-21 20:47 字数 317 浏览 7 评论 0原文

我使用 Qt Creator 和 qmake 来创建我的项目和测试项目 (QTest-tests)。我现在有自己运行的测试项目,但我很难理解如何测试正常项目中的类。我可以将项目中的 .h 文件包含在测试用例中,但这当然还不够。

它会抛出我的链接器错误,因为它没有代码或库来查找该类的代码。有没有一种简单的方法告诉 qmake 查找特定文件夹中的所有源文件以加载它们?或者,设置此类测试项目的正确方法是什么,而无需将每个源文件也添加到测试项目或将应用程序编译到要包含的库中?

I am using Qt Creator with qmake to create my project and my test project (QTest-tests). I have the test project running on its own now, but I have a hard time to understand how to test classes from my normal project. I can include the .h files from my project in the test cases, but that's of course not enough.

It throws my linker errors since it does have not the code or a library to look up the code for the class. Is there a simple way to tell qmake to look up all source files in a specific folder to load them? Or what is the correct way to set up such a test project without having to add each source file also to the test project or compiling your application into an library to include?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月2024-10-28 20:47:47

是否有一种简单的方法来告诉 QMake 查找特定文件夹中的所有源文件以加载它们?

qmake 支持通配符,因此您可以

SOURCES += *.cpp
HEADERS += *.h

在项目文件中使用类似的内容。

另请确保将 MOC_DIRRCC_DIR 调整为指向源文件夹以外的其他位置。您不想将生成的源文件包含在 SOURCES 中。

或者设置此类测试项目的正确方法是什么,而无需将每个源文件也添加到测试项目或将应用程序编译到要包含的库中

我更喜欢将自动化测试分为两个类:

  • 单元测试测试单个组件(通常位于源文件中)。我模拟了对系统其他部分的依赖。每个单元测试都是一个自己的 Qt 项目,仅包含被测试的源文件 + 测试代码 + 模拟代码。

  • 集成测试在没有模拟代码的情况下测试整个组件。通常它是作为库构建的应用程序引擎部分,然后链接到测试项目。

Is there a simple way to tell QMake to look up all source files in a specific folder to load them?

qmake supports wildcards so you can have something like

SOURCES += *.cpp
HEADERS += *.h

in your project file.

Also make sure to adjust MOC_DIR and RCC_DIR to point somewhere else than the source folder. You don't want to include generated source files in your SOURCES.

Or what is the correct way to set up such an test project without having to add each source file also to the test project or compiling your app into an lib to include

I prefer to separate my automated tests into two classes:

  • Unit tests testing single components (usually living in a source file). I mock away dependencies to other parts of the system. Each unit test is a Qt project of its own and only includes the source files being tested + test code + mock code.

  • Integration tests testing components as whole without mock code. Usually it's the application engine part built as a library and then linked to the test project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文