Skip to content

Commit 3a81838

Browse files
LukasWoodtliTSC21
authored andcommitted
Add support to build and run unit tests with catkin
1 parent c59d214 commit 3a81838

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ env:
33
- CCACHE_DIR=$HOME/.ccache
44
- CMAKE_BUILD="mkdir Build; cd Build; cmake ..; make -j$(nproc) -l$(nproc)"
55
- CMAKE_UNIT_TEST_BUILD="mkdir Build; cd Build; cmake -DENABLE_UNIT_TESTS=On ..; make -j$(nproc) -l$(nproc); make -j$(nproc) test"
6-
- CATKIN_BUILD="mkdir -p ~/catkin_ws/src; cd ~/catkin_ws; catkin init; ln -s ${TRAVIS_BUILD_DIR} src; catkin build -j$(nproc) -l$(nproc) -DBUILD_ROS_INTERFACE=ON"
6+
- CATKIN_BUILD="mkdir -p ~/catkin_ws/src; cd ~/catkin_ws; catkin init; ln -s ${TRAVIS_BUILD_DIR} src; catkin build -j$(nproc) -l$(nproc) -DBUILD_ROS_INTERFACE=ON; cd build/mavlink_sitl_gazebo/; catkin run_tests"
77
- KINETIC="source /opt/ros/kinetic/setup.bash; ${CATKIN_BUILD}"
88
- MELODIC="source /opt/ros/melodic/setup.bash; ${CATKIN_BUILD}"
99

README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,23 @@ sudo cp *.a /usr/lib
135135

136136
Building the tests on an other platform than Linux is not yet supported.
137137

138-
For building the tests, the flag `ENABLE_UNIT_TESTS` needs to be provided to cmake.
138+
When writing test it’s important to be careful which API functions of Gazebo are called. As no Gazebo server is running during the tests some functions can produce undefined behaviour (e.g. segfaults).
139+
140+
141+
### catkin
142+
143+
With catkin the test are enabled by defult.
144+
145+
```bash
146+
# After setting up the catkin workspace
147+
catkin build -j4 -l4 -DBUILD_ROS_INTERFACE=ON
148+
cd build/mavlink_sitl_gazebo/
149+
catkin run_tests
150+
```
151+
152+
### Plain CMake
153+
154+
For building the tests with plain CMake, the flag `ENABLE_UNIT_TESTS` needs to be provided.
139155

140156
```bash
141157
mkdir build && cd build
@@ -148,8 +164,6 @@ Then build and run the tests:
148164
make && make test
149165
```
150166

151-
When writing test it’s important to be careful which API functions of Gazebo are called. As no Gazebo server is running during the tests some functions can produce undefined behaviour (e.g. segfaults).
152-
153167
## Packaging
154168

155169
### Deb

package.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<!-- Use run_depend for packages you need at runtime: -->
3737
<!-- <run_depend>message_runtime</run_depend> -->
3838
<!-- Use test_depend for packages you need only for testing: -->
39-
<!-- <test_depend>gtest</test_depend> -->
39+
<test_depend>gtest</test_depend>
4040
<buildtool_depend>catkin</buildtool_depend>
4141
<buildtool_depend>gazebo_ros</buildtool_depend>
4242
<build_depend>eigen</build_depend>

unit_tests/CMakeLists.txt

+31-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11

2-
if(ENABLE_UNIT_TESTS)
32

4-
# Gimbal controller plugin
5-
add_executable(gazebo_gimbal_controller_plugin_test gazebo_gimbal_controller_plugin_test.cpp)
3+
if(ENABLE_UNIT_TESTS OR CATKIN_ENABLE_TESTING)
64

7-
target_link_libraries(gazebo_gimbal_controller_plugin_test
8-
PRIVATE gazebo_gimbal_controller_plugin
9-
${GTEST_BOTH_LIBRARIES}
10-
${CMAKE_THREAD_LIBS_INIT})
5+
# Uniform way to add unit tests. This works with catkin and with plain CMake.
6+
function(add_unit_test target) # add sources as ARGN
117

12-
add_test(gazebo_gimbal_controller_plugin_test gazebo_gimbal_controller_plugin_test)
8+
if(ENABLE_UNIT_TESTS)
139

14-
# GPS plugin
15-
add_executable(gazebo_gps_plugin_test gazebo_gps_plugin_test.cpp)
10+
# Plain CMake to add google test
11+
add_executable(${target} ${ARGN})
12+
add_test(${target} ${target})
13+
14+
elseif(CATKIN_ENABLE_TESTING)
15+
16+
# Catkin google test facilities
17+
catkin_add_gtest(${target} ${ARGN})
18+
19+
endif(ENABLE_UNIT_TESTS)
20+
21+
# Link the gtest libraries in any case
22+
target_link_libraries(${target} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
23+
24+
endfunction(add_unit_test)
1625

17-
target_link_libraries(gazebo_gps_plugin_test
18-
PRIVATE gazebo_gps_plugin
19-
${GTEST_BOTH_LIBRARIES}
20-
${CMAKE_THREAD_LIBS_INIT})
2126

22-
add_test(gazebo_gps_plugin_test gazebo_gps_plugin_test)
27+
# Add the tests
28+
29+
30+
# Gimbal controller plugin
31+
add_unit_test(gazebo_gimbal_controller_plugin_test gazebo_gimbal_controller_plugin_test.cpp)
32+
target_link_libraries(gazebo_gimbal_controller_plugin_test gazebo_gimbal_controller_plugin)
33+
34+
# GPS plugin
35+
add_unit_test(gazebo_gps_plugin_test gazebo_gps_plugin_test.cpp)
36+
target_link_libraries(gazebo_gps_plugin_test gazebo_gps_plugin)
37+
2338

24-
endif(ENABLE_UNIT_TESTS)
39+
endif(ENABLE_UNIT_TESTS OR CATKIN_ENABLE_TESTING)

0 commit comments

Comments
 (0)