📜  c++ unittest in ros - C++ (1)

📅  最后修改于: 2023-12-03 14:39:53.551000             🧑  作者: Mango

C++ Unit tests in ROS

Introduction

When developing complex robotic software systems, it is essential to have a robust testing framework in place to ensure that the system is functioning as expected. ROS provides an excellent framework for creating and managing tests for your project.

In this guide, we'll explore how to create and run unit tests for C++ code in ROS. Specifically, we'll be using the GTest framework.

Prerequisites

Before getting started, you'll need to have the following installed:

  • ROS Kinetic or later
  • GTest

To install GTest on Ubuntu, run the following command:

sudo apt-get install libgtest-dev
Creating a new test package

The first step in creating unit tests for your C++ code is to create a new ROS package specifically for those tests.

cd ~/catkin_ws/src  # Navigate to your ROS workspace's source directory
catkin_create_pkg my_package_tests roscpp my_package

This will create a new package named my_package_tests that depends on roscpp and my_package. Note that my_package is the name of the package containing the C++ code you want to test.

Writing unit tests

Now that we've created a new package for our tests, let's create a new file to define our unit tests. In this example, we'll create a file called my_test.cpp in the src directory of our my_package_tests package.

Here's an example test suite that tests whether a function that adds two numbers is working correctly:

#include <ros/ros.h>
#include <gtest/gtest.h>
#include <my_package/my_code.h>

TEST(MyCodeTests, AddTest)
{
    EXPECT_EQ(add(2, 2), 4);
    EXPECT_EQ(add(0, 0), 0);
    EXPECT_EQ(add(-1, 1), 0);
}

int main(int argc, char **argv)
{
    testing::InitGoogleTest(&argc, argv);
    ros::init(argc, argv, "my_code_tests");
    return RUN_ALL_TESTS();
}

In this example, we're including the necessary ROS and GTest headers, as well as the header for the C++ code we want to test (my_package/my_code.h). We define a test suite called MyCodeTests and a single test called AddTest. In this test, we're calling the add function with various inputs and using GTest's EXPECT_EQ macro to verify that the output is correct.

We also define a main function that initializes ROS and runs all of the tests using RUN_ALL_TESTS.

Building and running the tests

To build our test package, navigate to the root of your workspace and run catkin_make.

cd ~/catkin_ws
catkin_make

Once the build is complete, you can run the tests using the rostest command.

rostest my_package_tests my_test

This will run the my_test test suite and report any failures or errors.

Conclusion

In this guide, we've seen how to create a new test package in ROS and write unit tests for C++ code using the GTest framework. By using a robust testing framework like GTest and integrating it with ROS, we can ensure that our robotic software systems are working as expected and catch bugs early in the development process.