testingcmakegoogletestros2colcon

colcon test do not run/find ros2 Google test


The issue

Writing a ROS2 library package and its associated Google tests, after compilation, output library and relative Gtest program are stored has expected within /build and /install folders. Within those folders, I am able to run tests by launching them from command line but not by using colcon test command. Any idea on the issue ?

Additional ressources

1. Consecutive test run outputs from /build, /install folders and colcon test command:

enter image description here

2. /src folder composition :

src/c_pc2_iterator/
├── CMakeLists.txt
├── include
│   └── c_pc2_iterator
│       └── point_cloud2_iterator.h
├── LICENSE
├── package.xml
├── README.md
├── src
│   └── point_cloud2_iterator.c
└── test
    └── test_pc2_c_iterator.cpp

3. src/c_pc2_iterator/CMakeList.txt file :

cmake_minimum_required(VERSION 3.8)
project(c_pc2_iterator)

# Set Default C++17 standard for compiler
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

# Set Default C11 standard for compiler
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 11)
  set(CMAKE_C_STANDARD_REQUIRED ON)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(std_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)

add_library(${PROJECT_NAME}_library SHARED
  src/point_cloud2_iterator.c
  )
  
ament_target_dependencies(${PROJECT_NAME}_library std_msgs sensor_msgs ${c_typesupport_target})
target_include_directories(${PROJECT_NAME}_library PUBLIC
  "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
  "$<INSTALL_INTERFACE:include>")

ament_export_targets(export_${PROJECT_NAME}_library HAS_LIBRARY_TARGET)
ament_export_dependencies(std_msgs sensor_msgs ${c_typesupport_target})

install(DIRECTORY include/
  DESTINATION include
  )

install(
  TARGETS ${PROJECT_NAME}_library
  EXPORT export_${PROJECT_NAME}_library
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib
  RUNTIME DESTINATION bin
  INCLUDES DESTINATION include
  )

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  find_package(ament_cmake_gtest REQUIRED)
  find_package(launch_testing_ament_cmake)
  ament_add_gtest(${PROJECT_NAME}_test
    test/test_pc2_c_iterator.cpp)
  target_include_directories(${PROJECT_NAME}_test PUBLIC
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
    "$<INSTALL_INTERFACE:include>"
    )

  ament_target_dependencies(${PROJECT_NAME}_test std_msgs sensor_msgs)
  target_link_libraries(${PROJECT_NAME}_test ${PROJECT_NAME}_library)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  install(TARGETS
    ${PROJECT_NAME}_test
    DESTINATION test/${PROJECT_NAME})

  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

Solution

  • Within BUILD_TESTING section of src/c_pc2_iterator/CmakeList.txt, comment lines :

    1. set(ament_cmake_cpplint_FOUND TRUE)
    2. ament_lint_auto_find_test_dependencies()

    The original answer is from this post.