在项目中使用QT单元测试 - 冲突的主(...)函数 [英] Using QT Unit Tests in a project - conflicting main(...) functions

查看:884
本文介绍了在项目中使用QT单元测试 - 冲突的主(...)函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了我的第一步,使用单元测试开发我的应用程序的高尚路径,但它被证明是一个陡峭和粗糙的。我在Qt开发一个应用程序,所以想到达到他们的QTestLib框架。现在,我明白了如何进行单元测试,但我似乎不知道如何将单元测试合并到项目中。
说我有一个控制台应用程序,它只有一个main.cpp和一个cpp和h文件为我想开发的类,说MyClass。我想正确的做法是创建一个相应的测试类MyClassTest及其h和cpp文件。但我应该把它放在同一个项目?或者相反,创建一个单独的项目,用于单元测试,可以访问main项目中的文件(不知道我该怎么做)。

I set my first steps upon the noble path of using unit tests to develop my application, but it proves to be a steep and rough one. I'm developing an application in Qt, so thought to reach for their QTestLib framework. Now, I understand how to make unit tests, but I can't seem to figure out how to incorporate unit testing into a project. Say I have a console application which just has a main.cpp and a cpp and h file for a class I want to develop, say MyClass. I guess the right thing to do would be to create a corresponding test class MyClassTest with its h and cpp files. But should I put it into the same project? Or rather create a separate project just for unit tests that will have access to files in "main" project (not sure how would I do that yet)?

主应用程序和测试在同一个项目中,如何运行测试而不运行应用程序或其他方式?我尝试将文件从Qt的教程1单元测试合并到一个控制台项目,但它的问题是,他们使用一个宏 QTEST_MAIN(TestQString),扩展为 main(...)函数。这会导致与main.cpp中的 main(...)冲突。好吧,我可以重命名一个,但问题仍然存在 - 我该如何运行测试或应用程序本身?如果我重写 QTEST_MAIN 宏(这是一个坏主意),我禁用测试。如果我在main.cpp中重命名 main(...)函数,那么实际的应用程序永远不会被执行。我想应该有一种方法来为项目做两个单独的可执行文件,一个运行单元测试,另一个启动应用程序,但不能弄清楚如何去处理。

And if both main application and the test are in the same project, how do I run tests without running the application or the other way round? I tried incorporating files from Qt's Tutorial 1 on unit testing into a console project, but the problem with it is that they use a macro QTEST_MAIN(TestQString) which expands into a main(...) function. This causes conflicts with the main(...) in the main.cpp. Well, I can rename either one, but the problem still stands - how do I then run either the tests or the application itself? If I override QTEST_MAIN macro (which is a bad idea anyway) I disable the tests. If I rename main(...) function in the main.cpp, then the actual application never gets executed. I guess there should be a way to make two separate executables for the project, one running unit tests, and another launching the application, but can't figure out how to go about it.

推荐答案

确定为您的测试创建一个单独的项目。如果您在并行目录中安排应用程序项目和测试项目,那么您应该能够使用 test.pro file。

Definitely create a separate project for your tests. If you arrange the application project and test project in side-by-side directories, you should be able to reference your code units from your application project using relative paths in your test.pro file.

我使用此博客

这篇关于在项目中使用QT单元测试 - 冲突的主(...)函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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