开发者

QTestLib: Problem testing a Target = app

开发者 https://www.devze.com 2023-03-17 04:47 出处:网络
I have a problem with the dependencies in my unit testing project. My project structure is as follows:

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-Subproj开发者_如何学Cect 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!


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:

SOURCES += $$files(../pathToCppDir/*.cpp)
HEADERS += $$files(../pathToHppDir/*.h)
SOURCES -= ../pathToCppDir/main.cpp          // excluding second main(){}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号