Run unit-test with custom interfaces

asked 2020-10-07 09:58:32 -0500

alsora gravatar image

updated 2020-10-07 10:13:38 -0500

Hi, I have a package with the following characteristics: it creates custom interfaces, it creates a library and it contains unit-tests for that library. The library provides a node that uses the generated interfaces and the unit-tests need to instantiate that node.

I can run the unit-tests using the colcon test command, which produces the following log

test 1
    Start 1: test_fancy_stuff

1: Test command: /usr/bin/python3 "-u" "/opt/ros/foxy/share/ament_cmake_test/cmake/" "/root/ws/build/fancy_pkg/test_results/fancy_pkg/test_fancy_stuff.gtest.xml" "--package-name" "fancy_pkg" "--output-file" "/root/ws/build/fancy_pkg/ament_cmake_gtest/test_fancy_stuff.txt" "--command" "/root/ws/build/fancy_pkg/test_fancy_stuff" "--gtest_output=xml:/root/ws/build/fancy_pkg/test_results/fancy_pkg/test_fancy_stuff.gtest.xml"
1: Test timeout computed to be: 60
1: -- invoking following command in '/root/ws/build/fancy_pkg':
1:  - /root/ws/build/fancy_pkg/test_fancy_stuff --gtest_output=xml:/root/ws/build/fancy_pkg/test_results/fancy_pkg/test_fancy_stuff.gtest.xml
1: Running main() from /opt/ros/foxy/src/gtest_vendor/src/
1: [==========] Running 1 test from 1 test case.
1: [----------] Global test environment set-up.
1: [----------] 1 test from FancyTest
1: [ RUN      ] FancyTest.stuff
1: [INFO] [1602082277.983172124] [fancy_node]: Node created!!
1: [       OK ] FancyTest.stuff (17 ms)
1: [----------] 1 test from FancyTest (17 ms total)
1: [----------] Global test environment tear-down
1: [==========] 1 test from 1 test case ran. (17 ms total)
1: [  PASSED  ] 1 test.
1: -- return code 0
1: -- inject classname prefix into gtest result file '/root/ws/build/fancy_pkg/test_results/fancy_pkg/test_fancy_stuff.gtest.xml'
1: -- verify result file '/root/ws/build/fancy_pkg/test_results/fancy_pkg/test_fancy_stuff.gtest.xml'
1/1 Test #1: test_fancy_stuff .................   Passed    0.11 sec

However, I would like to be able to run the test also without using the colcon utility. For example using standard ctest. However this fails with the following exception

Failed to find library 'fancy_pkg__rosidl_typesupport_fastrtps_cpp'
unknown file: Failure
C++ exception with description "could not create publisher: type support not from this implementation, at /tmp/binarydeb/ros-foxy-rmw-fastrtps-cpp-1.2.0/src/publisher.cpp:75, at /tmp/binarydeb/ros-foxy-rcl-1.1.7/src/rcl/publisher.c:172" thrown in the test body.

This looks like it's coming from the find_library utility (see here) which search for the message library in the LD_LIBRARY_PATH.

How can I tell the unit-test that the interfaces are generated from the same package that I'm testing, so that it should be able to find them in the same place where it finds the library? Is there something special that colcon test is doing under the hood? I really would prefer not have to use the source scripts for this use-case.

Thank you

For completeness, this is my CMakeLists.txt file

cmake_minimum_required(VERSION 3.5)

# Default to C++14

  add_compile_options(-Wall -Wextra -Wpedantic)

find_package(ament_cmake REQUIRED)
find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)
find_package(rclcpp REQUIRED)

  DEPENDENCIES builtin_interfaces

add_library(fancy_lib SHARED src/fancy_node.cpp)
target_include_directories(fancy_lib PUBLIC ...
edit retag flag offensive close merge delete


After enabling debug logs, I see that the full ctest command used by colcon test includes the changes to LD_LIBRARY_PATH. My question remains as I feel that this is unnecessary for this case.

[0.402s] DEBUG:colcon.colcon_core.event_handler.log_command:Invoking command in '/root/ws/build/fancy_pkg': AMENT_PREFIX_PATH=/root/ws/install/fancy_pkg:${AMENT_PREFIX_PATH} CMAKE_PREFIX_PATH=/root/ws/install/fancy_pkg LD_LIBRARY_PATH=/root/ws/install/fancy_pkg/lib:${LD_LIBRARY_PATH} PYTHONPATH=/root/ws/install/fancy_pkg/lib/python3.8/site-packages:${PYTHONPATH} /usr/bin/ctest -C Release -D ExperimentalTest --no-compress-output -V --force-new-ctest-process
alsora gravatar image alsora  ( 2020-10-07 12:06:44 -0500 )edit