I came across lots of similar posts, but none of them helped me.
I have a project which has an app and test. Both App and test depends on a common lib (which is a submodule of the app). Here is my project structure.
|
|_ _Application.pro
|_ _App/app.pro
|_ _lib/lib.pro
|_ _TestSuite/
|_ _Test.pro
#Application.pro
TEMPLATE = subdirs
SUBDIRS += app \
lib
#subdirs
lib.file = $${PWD}/lib/lib.pro
app.file = $${PWD}/App/app.pro
#dependecies
app.depends = lib
QMAKE_CLEAN += $${OUT_PWD}/Makefile*
#Test.pro
TEMPLATE = subdirs
SUBDIRS += Test \
lib
#subdirs
lib.file = $${PWD}/../lib/lib.pro
app.file = $${PWD}/TestSuite/Test.pro
#dependecies
Test.depends = lib
QMAKE_CLEAN += $${OUT_PWD}/Makefile*
The application compiles fine, but when I run make on test it throws No rule to make target lib.pro needed by Test.pro Stop error.
If I copy the lib files under TestSuite then it works perfectly. So is it mandatory that subdirs should a subdirectory of the current application?
Although I can't seem to find it in the documentation at the moment, I at least found a bug report that states that .pro files using the subdirs template should not use directories that point upward in the directory structure. Your subdirs .pro file should be the root of your directory tree.
|
|_ _Application.pro
|_ _Test.pro
|_ _App/
|_ _app.pro
|_ _lib/
|_ _lib.pro
EDIT: Here's the relevant documentation stating that:
It is strongly recommended that you specify only paths in the current project's parent directory or its subdirectories.
Related
I've got a top-level .pro file with subdirs template
TEMPLATE = subdirs
SUBDIRS += \
common/tests
common/tests contains .pro file, but qmake couldn't find it:
Could not find .pro file for sub dir "common/tests" in "/home/o.antonyan/Documents/mt3/main/src/common/tests"
But it's there:
ls /home/o.antonyan/Documents/mt3/main/src/common/tests
api interfaces Makefile tests_common.pro.user ui
dataformat.cpp local_enviroment.cpp tests_common.pro types
If I move this pro file to parent directory and modify topp-level qmake SUBDIRS += common then it works. The problem is only when subdirs contain relative path more than 1 level deep.
QMake version 3.0 Using Qt version 5.4.1 on GNU/Linux x86, Qt compiled from sources
From the docs:
It is recommended that the project file in each subdirectory has the same base name as the subdirectory itself, because that makes it possible to omit the file name. For example, if the subdirectory is called myapp, the project file in that directory should be called myapp.pro.
Try renaming tests_common.pro to tests.pro.
I had the same problem and found this solution for the top level .pro file:
TEMPLATE = subdirs
tests_common.file = tests/tests_common.pro
SUBDIRS += tests_common
But now I would prefere the answer of svlasov.
I am building a Qt application which consists of several components (including plugins). Each sub project contains several classes.
I want to use the "normal" TDD workflow in my development process - namely;
I write a new function/method of a class in a module as a test
Compile the test (which should fail) for that module
Modify the source to correct the error (by adding the func/method etc)
Write test cases for the new function/method
Modify the src code to fix tests that fail
Each sub project will contain a src/ and test/ folders which will hold the source files and unittests respectively
This is what the project directory structure looks like:
myapp
|
|-myapp.pro
|
|--module1/
| |-src/
| |-test/
|
|--module2/
| |-src/
| |-test/
|
.
.
|--moduleN/
| |-src/
| |-test/
How can I setup QCreator so that I can build a subproject or its unittest from the QCreator GUI?
You can make a subdirs project and add the subprojects to its .pro file :
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
module1 \
module2 \
...
moduleN \
myapp
You should bring the subprojects that others depend on, first in the list. Also notice that the name of the .pro file of the subproject should be the same as it's folder name. This way the subprojects are detected and listed in the Projects pane.
The subprojects module1, module2 ... moduleN could be libraries which have TEMPLATE = lib in their .pro file and myapp should be app containing TEMPLATE = app in the .pro file.
You can use the libraries in each subproject by linking it to the subproject. This can be done by right clicking on the subproject and choosing Add Library and then Internal Library. When you select one library from the list of subprojects, the linking configurations are added to the .pro automatically. It will be like :
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../module1/release/ -lmodule1
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../module1/debug/ -lmodule1
else:unix: LIBS += -L$$OUT_PWD/../Base/ -lmodule1
INCLUDEPATH += $$PWD/../module1
DEPENDPATH += $$PWD/../module1
You should use subdirs template in .pro file.
I believe, you can start from following link (at the bottom of linked page).
I got my Qt project and I'm using Qt Creator. I want to unit-test all my code.
However I'm quite new at QTestLib framework but everyone recommended it for testing Qt-based source. Now I'm a little confused how to structure test project with app project.
Can I put all source and testing code in same project? If so, how could I manage them? I didn't find any option that let me start app or start test in one project.
If I put app source and testing code in separate projects, the testing project will reference app project, that's not quite convenient.
For lots for classes required to be tested, how do I manage testing code?
How do you guys manage testing code in such a situation? Thanks.
First structure source like below:
MyApp
MyAppUnitTest
Under MyApp project, use a MyAppSrc.pri to locate source files:
SOURCES += \
../../../framework/src/myapp.cpp \
../../../framework/src/mycontrol.cpp
HEADERS += \
../../../framework/inc/myapp.h \
../../../framework/inc/mycontrol.h
INCLUDEPATH += ../../../framework/extlibs
Include this .pri in MyApp.pro like:
include(MyAppSrc.pri)
Then structure the testing project exactly like the main project, with one extra include in MyAppUnitTest.pro:
include(MyAppUnitTestSrc.pri)
include(../MyApp/MyAppSrc.pri)
I use this approach: http://xilexio.org/?p=125
Namely, place a test config in the single .pro file that builds everything. File hierarchy:
myproject.pro
src/
Example1.cpp
Example2.cpp
Example1.h
Example2.h
test/
ExampleTest.cpp
ExampleTest.h
myproject.pro file:
QT += #needed modules
CONFIG += qt c++11
HEADERS += \
src/Example1.h \
src/Example2.h
SOURCES += \
src/Example1.h \
src/Example2.h
test{
message(Configuring test build...)
TEMPLATE = app
TARGET = myapptests
QT += testlib
HEADERS += \
test/ExampleTest.h
SOURCES += \
test/ExampleTest.cpp
}
else{
TEMPLATE = lib
TARGET = myapp
CONFIG += plugin
TARGET = $$qtLibraryTarget($$TARGET)
}
In my example, I'm building a plugin library, but the method should work for an app as well. In the case of an app, it is likely that SOURCES -= src/main.cpp is needed under the else clause, plugin libraries don't have it. If this is not done, the main() of the app will clash with the main() of the unit tests.
ExampleTest.cpp looks like the following:
#include "ExampleTest.h"
void ExampleTest::exampleTest(){
//Do the tests
}
QTEST_MAIN(ExampleTest)
ExampleTest.h looks like the following:
#include <QtTest/QtTest>
class ExampleTest : public QObject {
Q_OBJECT
private slots:
void exampleTest();
};
To build the project tests, in a separate directory than the regular build, run:
qmake path/to/myproject.pro "CONFIG += test"
I like the other answers but I would like to also give some feedback how we do this at the company I currently work for:
Create a subdirs project (this will be the top-level project that will manage ALL including your library project or whatever you want to test)
+-----MyProject (top-level subdirs)
Add your library projects as a sub-project
+-----MyProject (top-level subdirs)
|
+-----Library (library project, UI project etc.)
Add another subdirs projects (for the tests)
+-----MyProject (top-level subdirs)
|
+-----Library (library project, UI project etc.)
|
+-----Tests (subdirs for tests)
Create a QUnitTest project an add it to the testing subdirs project
+-----MyProject (subdirs)
|
+-----Library (library project, UI project etc.)
|
+-----Tests (subdirs for tests)
|
+----- TestA (QUnitTest project for testing feature A)
Add as many tests as you see fit
...
|
+-----Tests (subdirs for test)
|
+----- TestA (QUnitTest project for testing feature A)
|
+----- TestB (QUnitTest project for testing feature B)
|
+----- TestC (QUnitTest project for testing feature C)
|
...
|
+----- TestZ (QUnitTest project for testing feature Z)
If you need to group test in groups you can also use subdirs to do that. subdirs also ensures creating of real directories in your file system. If you want to avoid too much subdirsing you can group the tests in folders that you have created on your own in your filesystem inside the Tests project folder.
Beside that I would also recommend to add a subdirs for template projects.
+-----MyProject (subdirs)
|
+-----Library (library project, UI project etc.)
|
+-----Tests (subdirs for tests)
| |
| ...
|
+-----Templates (subdirs for template projects
|
+----- TemplateA (template project for feature A)
|
+----- TemplateB (template project for feature B)
|
+----- TemplateAB (template project for feature A and B together)
|
...
|
+----- TemplateZ (template project for feature Z)
This is of course based on your library's functionality. With template projects I mean custom widgets etc. that link against your library and expose selectively (or all) of its functionality in the way it's supposed to appear to the user. For example if you have a library that manages various camera devices you can create a template project for each camera device thus allowing for the users of your library to just copy-paste the specific template project and expand it or at least see how the integration of your library is supposed to happen in general. This allows reducing the documentation and at the same time giving nice self-contained examples that should reduce the development time that is otherwise spent in figuring out how the integration and usage of the library works (you can say it's sort of a set of Hello World projects :)). Last but not least you can outline solutions for different use-cases.
I use Qt Creator by CMake instead of qmake to build my Qt project.
Basically I have two folders:
src
tests
Each test is a program in itself testing a class. The app to be tested is compiled as a library.. You compile all your sources in the folder src as a library.
// ClassTest.cpp
#include "ClassTest.h"
#include "Class2Test.h" // Class of the app
#include <QtTest/QtTest>
ClassTest::ClassTest( QObject* parent )
: QObject(parent)
{
}
QTEST_MAIN( ClassTest )
#include "ClassTest.moc"
You just have to link your lib to your test executable.
Example:
in the src folder CMakeLists.txt example
add_library( MyAPP
SHARED
Class2Test.cpp
)
target_link_libraries( MyAPP
${QT_LIBRARIES}
)
in the tests folder CMakeLists.txt example, for each test.
qt4_automoc( ${test_src} )
add_executable( ${test_name} ${test_src} )
target_link_libraries( ${test_name}
MyAPP
${QT_LIBRARIES}
${QT_QTTEST_LIBRARY}
)
It is still in the same project but you can add a flag to let the user compile the test or not. It is clean because the app stays untouched and it allows you to test each class of your app.
I have the following directory structure:
- project
- test.pro
- test2
- test2.pro
test.pro looks like this:
# ...
SUBDIRS = test2
The problem is that when I run:
qmake test.pro
make
...it only builds test and not test2.
How come test2 isn't getting built as well? What do I have to do to tell one Qt project to build another one first?
Adding to SUBDIRS has no effect for any TEMPLATE other than subdirs, and you cannot have multiple TEMPLATEs in a single .pro file. In other words, you can't have a single .pro file to both build some binaries and invoke some subdirs projects.
You need one top-level .pro file which only contains subdirs. For example, your test.pro could be:
TEMPLATE = subdirs
SUBDIRS = test1 test2
... and you would then have subdirectories for test1 and test2.
If you don't want to reorganize your code into a subdirectory, you can also put the names of .pro files (instead of directory names) into SUBDIRS. For example, your test.pro could be:
TEMPLATE = subdirs
SUBDIRS = test1.pro test2
... where test1.pro may exist in the same directory as test.pro, and have the usual TEMPLATE=app stuff.
I have a main pro file for my application and I would like to tell qmake to also compile a separate lib at the same time as the application. The lib also has a pro file in its directory. Is this possible?
Put the lib and the app in separate subdirectories and make your top-level .pro file to use the SUBDIRS template:
TEMPLATE = subdirs
SUBDIRS = lib app
app.depends = lib