Skip to content

Commit afa709d

Browse files
author
timlinux
committedNov 14, 2009
Updated coding documentation to reflect Martins ADD_QGIS_TEST macro for unit testing
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12108 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

1 file changed

+90
-56
lines changed

1 file changed

+90
-56
lines changed
 

‎CODING

Lines changed: 90 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ Developers guide for QGIS
5454
3.1. The QGIS testing framework - an overview
5555
3.2. Creating a unit test
5656
3.3. Adding your unit test to CMakeLists.txt
57-
3.4. Building your unit test
58-
3.5. Run your tests
57+
3.4. The ADD_QGIS_TEST macro explained
58+
3.5. Building your unit test
59+
3.6. Run your tests
5960
4. HIG (Human Interface Guidelines)
6061
5. Authors
6162

@@ -1136,28 +1137,50 @@ line is the include for the MOC generated sources. You should replace
11361137

11371138
Adding your unit test to the build system is simply a matter of editing the
11381139
CMakeLists.txt in the test directory, cloning one of the existing test blocks,
1139-
and then search and replacing your test class name into it. For example:
1140+
and then replacing your test class name into it. For example:
11401141

11411142

1142-
#
11431143
# QgsRasterLayer test
1144-
#
1145-
SET(qgis_rasterlayertest_SRCS testqgsrasterlayer.cpp)
1146-
SET(qgis_rasterlayertest_MOC_CPPS testqgsrasterlayer.cpp)
1147-
QT4_WRAP_CPP(qgis_rasterlayertest_MOC_SRCS ${qgis_rasterlayertest_MOC_CPPS})
1148-
ADD_CUSTOM_TARGET(qgis_rasterlayertestmoc ALL DEPENDS ${qgis_rasterlayertest_MOC_SRCS})
1149-
ADD_EXECUTABLE(qgis_rasterlayertest ${qgis_rasterlayertest_SRCS})
1150-
ADD_DEPENDENCIES(qgis_rasterlayertest qgis_rasterlayertestmoc)
1151-
TARGET_LINK_LIBRARIES(qgis_rasterlayertest ${QT_LIBRARIES} qgis_core)
1152-
INSTALL(TARGETS qgis_rasterlayertest RUNTIME DESTINATION ${QGIS_BIN_DIR})
1153-
ADD_TEST(qgis_rasterlayertest ${QGIS_BIN_DIR}/qgis_rasterlayertest)
1144+
ADD_QGIS_TEST(rasterlayertest testqgsrasterlayer.cpp)
11541145

11551146

1156-
I'll run through these lines briefly to explain what they do, but if you are
1157-
not interested, just clone the block, search and replace e.g.
11581147

1148+
3.4. The ADD_QGIS_TEST macro explained
1149+
======================================
11591150

1160-
:'<,'>s/rasterlayer/mynewtest/g
1151+
I'll run through these lines briefly to explain what they do, but if you are
1152+
not interested, just do the step explained in the above section and section.
1153+
1154+
1155+
MACRO (ADD_QGIS_TEST testname testsrc)
1156+
SET(qgis_${testname}_SRCS ${testsrc} ${util_SRCS})
1157+
SET(qgis_${testname}_MOC_CPPS ${testsrc})
1158+
QT4_WRAP_CPP(qgis_${testname}_MOC_SRCS ${qgis_${testname}_MOC_CPPS})
1159+
ADD_CUSTOM_TARGET(qgis_${testname}moc ALL DEPENDS ${qgis_${testname}_MOC_SRCS})
1160+
ADD_EXECUTABLE(qgis_${testname} ${qgis_${testname}_SRCS})
1161+
ADD_DEPENDENCIES(qgis_${testname} qgis_${testname}moc)
1162+
TARGET_LINK_LIBRARIES(qgis_${testname} ${QT_LIBRARIES} qgis_core)
1163+
SET_TARGET_PROPERTIES(qgis_${testname}
1164+
PROPERTIES
1165+
# skip the full RPATH for the build tree
1166+
SKIP_BUILD_RPATH TRUE
1167+
# when building, use the install RPATH already
1168+
# (so it doesn't need to relink when installing)
1169+
BUILD_WITH_INSTALL_RPATH TRUE
1170+
# the RPATH to be used when installing
1171+
INSTALL_RPATH ${QGIS_LIB_DIR}
1172+
# add the automatically determined parts of the RPATH
1173+
# which point to directories outside the build tree to the install RPATH
1174+
INSTALL_RPATH_USE_LINK_PATH true)
1175+
IF (APPLE)
1176+
# For Mac OS X, the executable must be at the root of the bundle's executable folder
1177+
INSTALL(TARGETS qgis_${testname} RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
1178+
ADD_TEST(qgis_${testname} ${CMAKE_INSTALL_PREFIX}/qgis_${testname})
1179+
ELSE (APPLE)
1180+
INSTALL(TARGETS qgis_${testname} RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
1181+
ADD_TEST(qgis_${testname} ${CMAKE_INSTALL_PREFIX}/bin/qgis_${testname})
1182+
ENDIF (APPLE)
1183+
ENDMACRO (ADD_QGIS_TEST)
11611184

11621185

11631186
Lets look a little more in detail at the individual lines. First we define the
@@ -1166,16 +1189,16 @@ methodology I described above where class declaration and definition are in the
11661189
same file) its a simple statement:
11671190

11681191

1169-
SET(qgis_rasterlayertest_SRCS testqgsrasterlayer.cpp)
1192+
SET(qgis_${testname}_SRCS ${testsrc} ${util_SRCS})
11701193

11711194

11721195
Since our test class needs to be run through the Qt meta object compiler (moc)
11731196
we need to provide a couple of lines to make that happen too:
11741197

11751198

1176-
SET(qgis_rasterlayertest_MOC_CPPS testqgsrasterlayer.cpp)
1177-
QT4_WRAP_CPP(qgis_rasterlayertest_MOC_SRCS ${qgis_rasterlayertest_MOC_CPPS})
1178-
ADD_CUSTOM_TARGET(qgis_rasterlayertestmoc ALL DEPENDS ${qgis_rasterlayertest_MOC_SRCS})
1199+
SET(qgis_${testname}_MOC_CPPS ${testsrc})
1200+
QT4_WRAP_CPP(qgis_${testname}_MOC_SRCS ${qgis_${testname}_MOC_CPPS})
1201+
ADD_CUSTOM_TARGET(qgis_${testname}moc ALL DEPENDS ${qgis_${testname}_MOC_SRCS})
11791202

11801203

11811204
Next we tell cmake that it must make an executeable from the test class.
@@ -1185,8 +1208,8 @@ included the moc outputs directly into our test class, so that will give it
11851208
executeable:
11861209

11871210

1188-
ADD_EXECUTABLE(qgis_rasterlayertest ${qgis_rasterlayertest_SRCS})
1189-
ADD_DEPENDENCIES(qgis_rasterlayertest qgis_rasterlayertestmoc)
1211+
ADD_EXECUTABLE(qgis_${testname} ${qgis_${testname}_SRCS})
1212+
ADD_DEPENDENCIES(qgis_${testname} qgis_${testname}moc)
11901213

11911214

11921215
Next we need to specify any library dependencies. At the moment classes have
@@ -1196,38 +1219,49 @@ only. Of course you also need to link to the relevant qgis libraries as
11961219
required by your unit test.
11971220

11981221

1199-
TARGET_LINK_LIBRARIES(qgis_rasterlayertest ${QT_LIBRARIES} qgis_core)
1200-
1201-
1202-
Next I tell cmake to the same place as the qgis binaries itself. This is
1203-
something I plan to remove in the future so that the tests can run directly
1204-
from inside the source tree.
1205-
1206-
1207-
INSTALL(TARGETS qgis_rasterlayertest RUNTIME DESTINATION ${QGIS_BIN_DIR})
1208-
1209-
1210-
Finally here is where the best magic happens - we register the class with
1211-
ctest. If you recall in the overview I gave in the beginning of this section we
1212-
are using both QtTest and CTest together. To recap, QtTest adds a main
1213-
method to your test unit and handles calling your test methods within the
1214-
class. It also provides some macros like QVERIFY that you can use as to test
1215-
for failure of the tests using conditions. The output from a QtTest unit test
1216-
is an executeable which you can run from the command line. However when you
1217-
have a suite of tests and you want to run each executeable in turn, and
1218-
better yet integrate running tests into the build process, the CTest is
1219-
what we use. The next line registers the unit test with CMake / CTest.
1220-
1221-
1222-
ADD_TEST(qgis_rasterlayertest ${QGIS_BIN_DIR}/qgis_rasterlayertest)
1223-
1224-
1225-
The last thing I should add is that if your test requires optional parts of the
1226-
build process (e.g. Postgresql support, GSL libs, GRASS etc.), you should take
1227-
care to enclose you test block inside a IF () block in the CMakeLists.txt file.
1228-
1229-
1230-
3.4. Building your unit test
1222+
TARGET_LINK_LIBRARIES(qgis_${testname} ${QT_LIBRARIES} qgis_core)
1223+
1224+
1225+
Next I tell cmake to install the tests to the same place as the qgis binaries
1226+
itself. This is something I plan to remove in the future so that the tests can
1227+
run directly from inside the source tree.
1228+
1229+
1230+
SET_TARGET_PROPERTIES(qgis_${testname}
1231+
PROPERTIES
1232+
# skip the full RPATH for the build tree
1233+
SKIP_BUILD_RPATH TRUE
1234+
# when building, use the install RPATH already
1235+
# (so it doesn't need to relink when installing)
1236+
BUILD_WITH_INSTALL_RPATH TRUE
1237+
# the RPATH to be used when installing
1238+
INSTALL_RPATH ${QGIS_LIB_DIR}
1239+
# add the automatically determined parts of the RPATH
1240+
# which point to directories outside the build tree to the install RPATH
1241+
INSTALL_RPATH_USE_LINK_PATH true)
1242+
IF (APPLE)
1243+
# For Mac OS X, the executable must be at the root of the bundle's executable folder
1244+
INSTALL(TARGETS qgis_${testname} RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
1245+
ADD_TEST(qgis_${testname} ${CMAKE_INSTALL_PREFIX}/qgis_${testname})
1246+
ELSE (APPLE)
1247+
INSTALL(TARGETS qgis_${testname} RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
1248+
ADD_TEST(qgis_${testname} ${CMAKE_INSTALL_PREFIX}/bin/qgis_${testname})
1249+
ENDIF (APPLE)
1250+
1251+
1252+
Finally the above uses ADD_TEST to register the test with cmake / ctest . Here
1253+
is where the best magic happens - we register the class with ctest. If you
1254+
recall in the overview I gave in the beginning of this section we are using
1255+
both QtTest and CTest together. To recap, QtTest adds a main method to your
1256+
test unit and handles calling your test methods within the class. It also
1257+
provides some macros like QVERIFY that you can use as to test for failure of
1258+
the tests using conditions. The output from a QtTest unit test is an
1259+
executeable which you can run from the command line. However when you have a
1260+
suite of tests and you want to run each executeable in turn, and better yet
1261+
integrate running tests into the build process, the CTest is what we use.
1262+
1263+
1264+
3.5. Building your unit test
12311265
============================
12321266

12331267
To build the unit test you need only to make sure that ENABLE_TESTS=true in the
@@ -1240,7 +1274,7 @@ cmake configuration. There are two ways to do this:
12401274
Other than that, just build QGIS as per normal and the tests should build too.
12411275

12421276

1243-
3.5. Run your tests
1277+
3.6. Run your tests
12441278
===================
12451279

12461280
The simplest way to run the tests is as part of your normal build process:

0 commit comments

Comments
 (0)
Please sign in to comment.