QTestLib:测试目标=应用程序时出现问题
我的单元测试项目中存在依赖关系问题。
我的项目结构如下:
MyProject
|---MyProject.pro
|---src
| |---src.pro
| |---ClassA.h
| |---ClassA.cpp
| |---ClassB.h
| |---ClassB.cpp
|
|---tests
|--tests.pro
|--testClassA.cpp
MyProject.pro:
TEMPLATE = subdirs
SUBDIRS = src tests
src.pro:
TEMPLATE = app
TARGET = someApp
HEADERS += classA.h classB.h
SOURCES += classA.cpp classB.cpp
可以说 ClassA 在 ClassA.cpp 中调用 ClassB:
void ClassA::someFunctionInClassA()
{
ClassB b;
}
现在我想对 ClassA 进行单元测试。我找到了两种方法来做到这一点。 第一个是将 src-Subproject 转换为库(TARGET = src.pro 中的 lib)。然后我将以下行添加到tests.pro中,一切都很好:
LIBS += ../libSrc.so
第二个是命名test.pro中ClassA使用的所有文件。这意味着我的tests.pro看起来像:
TARGET = testclassA
TEMPLATE = app
/*...*/
HEADERS += ../src/ClassA.h \
../src/ClassB.h
SOURCES += testClassA.cpp \
../src/ClassA.cpp \
../src/ClassB.cpp
我无法使用第一个解决方案(将src项目转换为lib),所以我必须使用第二个解决方案。即使我只想测试其中之一,是否真的有必要命名所有源/标头? 谢谢您的建议!
I have a problem with the dependencies in my unit testing project.
My project structure is as follows:
MyProject
|---MyProject.pro
|---src
| |---src.pro
| |---ClassA.h
| |---ClassA.cpp
| |---ClassB.h
| |---ClassB.cpp
|
|---tests
|--tests.pro
|--testClassA.cpp
MyProject.pro:
TEMPLATE = subdirs
SUBDIRS = src tests
src.pro:
TEMPLATE = app
TARGET = someApp
HEADERS += classA.h classB.h
SOURCES += classA.cpp classB.cpp
Lets say ClassA invokes ClassB in ClassA.cpp:
void ClassA::someFunctionInClassA()
{
ClassB b;
}
Now i want to unit test ClassA. I found two way to do so.
First one is to convert the src-Subproject to a library (TARGET = lib in src.pro). Then i add the following line to the tests.pro and its all fine:
LIBS += ../libSrc.so
The second one is to name ALL files that are used by ClassA in test.pro. This means that my tests.pro looks like:
TARGET = testclassA
TEMPLATE = app
/*...*/
HEADERS += ../src/ClassA.h \
../src/ClassB.h
SOURCES += testClassA.cpp \
../src/ClassA.cpp \
../src/ClassB.cpp
I can't use the first solution (converting the src-project into a lib), so i have to use the second one. Is it really necessary to name all sources/headers even if i want to test only one of them?
Thank you in advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能使用第一个解决方案意味着我不能对源代码进行太多更改。现在TARGET=app是什么,一定还是app。我唯一的任务是向项目添加单元测试。
我找到了一种解决方法,不显式命名所有类:
在测试中:
I can't use the first solution meant that i mustn't change too much about the source code. What is TARGET = app now, must remain an app. My only task is to add unit testing to the project.
I found a workaround to not name all the classes explicitly:
in tests.pro: