ROS2 GTest - Passing instance of node to functions

asked 2020-04-02 09:07:04 -0500

kk2105 gravatar image

updated 2020-04-03 03:22:53 -0500

Hi Guys,

I am trying to run GTest to validate the correctness of the defined functions.

Below is the main code snippet with function TestFunction_1() and TestFunction_2() which needs to be tested using GTest.

class TestNode : public rclcpp::Node {
explicit TestNode(const rclcpp::NodeOptions &options)
  : Node("test_node", options), status_(this, 10) {
   RCLCPP_INFO(this->get_logger(), "Starting %s.", this->get_name());

  rclcpp::Time TestFunction_1();
  rclcpp::Time TestFunction_2(this);

 rclcpp::Time TestFunction_1() {
     rclcpp::Time time_now_1 = this->now();
     return time_now_1;

 rclcpp:Time TestFunction_2(rclcpp::Node *node_obj) {
     rclcpp::Time time_now_2 = node_obj->now(); 
     return time_now_2;

Below is the code snippet from the gtest code.

TEST(Test, test_case_1) {
    rclcpp::Time time1 = TestFunction_1();
    EXPECT_EQ(time1, standard_value);

TEST(Test, test_case_2) {
    rclcpp::Node *node_object;
    rclcpp::Time time2 = TestFunction_2(node_object);
    EXPECT_EQ(time2, standard_value);

As shown in the main code I am using / passing the this to get the required data.
However in gtest code if I create an object of rclcpp::Node it is not as same as in the main code.
So the GTest may not work as I expected.

Could you guys please suggest how to deal with this problem.

Note : Ignore the EXPECT_EQ of rclcpp::Time types. That is just for problem statement (demonstration) purpose.

Thank you,

edit retag flag offensive close merge delete