Matthias
Matthias

Reputation: 387

Unit testing setup in Qt Creator

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?

Upvotes: 14

Views: 4551

Answers (1)

laalto
laalto

Reputation: 152787

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.

Upvotes: 4

Related Questions