[英]Using QT Unit Tests in a project - conflicting main(…) functions
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.
我正在Qt中开发应用程序,因此可以考虑使用其QTestLib框架。 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.
假设我有一个控制台应用程序,它只有一个main.cpp以及一个我要开发的类的cpp和h文件,例如MyClass。 I guess the right thing to do would be to create a corresponding test class MyClassTest with its h and cpp files.
我想正确的做法是使用h和cpp文件创建相应的测试类MyClassTest。 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)?
还是只为单元测试创建一个单独的项目,该项目就可以访问“主”项目中的文件(不确定该怎么做)?
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. 我尝试将Qt教程1中有关单元测试的文件合并到控制台项目中,但是问题在于它们使用了宏
QTEST_MAIN(TestQString)
,该宏扩展为main(...)
函数。 This causes conflicts with the main(...)
in the main.cpp. 这会导致与main.cpp中的
main(...)
发生冲突。 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. 如果我重写
QTEST_MAIN
宏(无论如何这是一个坏主意),我将禁用测试。 If I rename main(...)
function in the main.cpp, then the actual application never gets executed. 如果我在main.cpp中重命名
main(...)
函数,则实际的应用程序将永远不会执行。 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. 我猜应该有一种方法可以为该项目创建两个独立的可执行文件,一个正在运行单元测试,另一个正在运行该应用程序,但无法弄清楚该如何做。
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. 如果将应用程序项目和测试项目排列在并排目录中,则应该能够使用
test.pro
文件中的相对路径来引用应用程序项目中的代码单元。
I use the very handy test runner presented on this blog . 我使用此博客上介绍的非常方便的测试运行程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.