# Unittesting with ROS2 dasing

Hi there,

I'm working on a project. For this project i'm working with ROS2 dashing in C++ on a Jetson Nano. I've writen some code which works fine, but i would like to add some unit testing for some functions so that i can test them whenever i want to.

I've read online that i should be able to do so with gtest, but i can't get it to work. When i use "colcon test --verbose" i get the message " The test did not generate a result file" as shown below:

build/mtr_system_nodes/test_results/mtr_system_nodes/mtr_system_nodes-test.gtest.xml: 1 test, 0 errors, 1 failure, 0 skipped
- mtr_system_nodes mtr_system_nodes-test.gtest.missing_result
<<< failure message
The test did not generate a result file:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from AngleToSteepTest
[ RUN      ] AngleToSteepTest.HandlesTenDegreesInput
>>>


Does anyone have an idea why this could happen?

i'll share my test, cmake and the function to be tested below

unittest:

    TEST(AngleToSteepTest, HandlesTenDegreesInput)
{
mtr_messages::msg::Sensordata::SharedPtr msg;
angleDetection *a = new angleDetection();
msg->x = 10;
bool result = a->_angleToSteep(msg);
EXPECT_FALSE(result);
}


cmake:

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(mtr_messages REQUIRED)

ament_target_dependencies(message_publisher_node rclcpp std_msgs mtr_messages)

ament_target_dependencies(message_subscriber_node rclcpp std_msgs mtr_messages)

ament_target_dependencies(angle_detection_lib rclcpp std_msgs mtr_messages)

ament_target_dependencies(angle_detection_node rclcpp std_msgs mtr_messages)

target_include_directories(angle_detection_lib PUBLIC  ${PROJECT_SOURCE_DIR}/src) target_include_directories(angle_detection_node PUBLIC${PROJECT_SOURCE_DIR}/src)

install(TARGETS
message_publisher_node
message_subscriber_node
angle_detection_node
DESTINATION lib/${PROJECT_NAME}) if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) ament_lint_auto_find_test_dependencies() find_package(rclcpp REQUIRED) find_package(mtr_messages REQUIRED) ament_add_gtest(${PROJECT_NAME}-test src/angleDetectionNodeTests.cpp)
ament_target_dependencies(${PROJECT_NAME}-test rclcpp std_msgs mtr_messages) target_link_libraries(${PROJECT_NAME}-test angle_detection_lib)

endif()


Code to be tested:

bool angleDetection::_angleToSteep(const mtr_messages::msg::Sensordata::SharedPtr msg)
{
int x = (int)msg->x;
if (x >= 20 || x <= -20)
{
std::cout << "angle to steep the angle is:  " << msg->x << "degrees" << std::endl;
return true;
}
return false;
}

edit retag close merge delete