Skip to content

Commit

Permalink
Add support to build and run unit tests with catkin
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasWoodtli authored and TSC21 committed Jun 5, 2019
1 parent c59d214 commit 3a81838
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ env:
- CCACHE_DIR=$HOME/.ccache
- CMAKE_BUILD="mkdir Build; cd Build; cmake ..; make -j$(nproc) -l$(nproc)"
- CMAKE_UNIT_TEST_BUILD="mkdir Build; cd Build; cmake -DENABLE_UNIT_TESTS=On ..; make -j$(nproc) -l$(nproc); make -j$(nproc) test"
- 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"
- 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"
- KINETIC="source /opt/ros/kinetic/setup.bash; ${CATKIN_BUILD}"
- MELODIC="source /opt/ros/melodic/setup.bash; ${CATKIN_BUILD}"

Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,23 @@ sudo cp *.a /usr/lib

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

For building the tests, the flag `ENABLE_UNIT_TESTS` needs to be provided to cmake.
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).


### catkin

With catkin the test are enabled by defult.

```bash
# After setting up the catkin workspace
catkin build -j4 -l4 -DBUILD_ROS_INTERFACE=ON
cd build/mavlink_sitl_gazebo/
catkin run_tests
```

### Plain CMake

For building the tests with plain CMake, the flag `ENABLE_UNIT_TESTS` needs to be provided.

```bash
mkdir build && cd build
Expand All @@ -148,8 +164,6 @@ Then build and run the tests:
make && make test
```

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).

## Packaging

### Deb
Expand Down
2 changes: 1 addition & 1 deletion package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<!-- Use run_depend for packages you need at runtime: -->
<!-- <run_depend>message_runtime</run_depend> -->
<!-- Use test_depend for packages you need only for testing: -->
<!-- <test_depend>gtest</test_depend> -->
<test_depend>gtest</test_depend>
<buildtool_depend>catkin</buildtool_depend>
<buildtool_depend>gazebo_ros</buildtool_depend>
<build_depend>eigen</build_depend>
Expand Down
47 changes: 31 additions & 16 deletions unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@

if(ENABLE_UNIT_TESTS)

# Gimbal controller plugin
add_executable(gazebo_gimbal_controller_plugin_test gazebo_gimbal_controller_plugin_test.cpp)
if(ENABLE_UNIT_TESTS OR CATKIN_ENABLE_TESTING)

target_link_libraries(gazebo_gimbal_controller_plugin_test
PRIVATE gazebo_gimbal_controller_plugin
${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT})
# Uniform way to add unit tests. This works with catkin and with plain CMake.
function(add_unit_test target) # add sources as ARGN

add_test(gazebo_gimbal_controller_plugin_test gazebo_gimbal_controller_plugin_test)
if(ENABLE_UNIT_TESTS)

# GPS plugin
add_executable(gazebo_gps_plugin_test gazebo_gps_plugin_test.cpp)
# Plain CMake to add google test
add_executable(${target} ${ARGN})
add_test(${target} ${target})

elseif(CATKIN_ENABLE_TESTING)

# Catkin google test facilities
catkin_add_gtest(${target} ${ARGN})

endif(ENABLE_UNIT_TESTS)

# Link the gtest libraries in any case
target_link_libraries(${target} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

endfunction(add_unit_test)

target_link_libraries(gazebo_gps_plugin_test
PRIVATE gazebo_gps_plugin
${GTEST_BOTH_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT})

add_test(gazebo_gps_plugin_test gazebo_gps_plugin_test)
# Add the tests


# Gimbal controller plugin
add_unit_test(gazebo_gimbal_controller_plugin_test gazebo_gimbal_controller_plugin_test.cpp)
target_link_libraries(gazebo_gimbal_controller_plugin_test gazebo_gimbal_controller_plugin)

# GPS plugin
add_unit_test(gazebo_gps_plugin_test gazebo_gps_plugin_test.cpp)
target_link_libraries(gazebo_gps_plugin_test gazebo_gps_plugin)


endif(ENABLE_UNIT_TESTS)
endif(ENABLE_UNIT_TESTS OR CATKIN_ENABLE_TESTING)

0 comments on commit 3a81838

Please sign in to comment.