UP | HOME

Testing and Debugging in ROS and C++

Overview

Levels of ROS testing

Library-level unit tests

  • These tests are independent of ROS and are used for testing individual functions and libraries.
  • Each test should be small and (ideally) run quickly
  • Ideally these tests are designed to run every time you compile (or at least every time immediately prior to merging your code into the master)

Single Node Unit Tests

  • The goal is to validate the functionality of a single Node
  • Test its publishers, subscribers, and servers, and parameter usage
  • These tests should also be relatively small and self contained
  • They can be set up to run as part of the compilation process

Integration Test

These tests start multiple nodes and test how the whole system works together

  • These tests are made in the same way as single node unit tests, but launch more ROS nodes
  • Some integration tests can be done automatically as part of the compilation process
  • Some integration tests may require physical robot hardware

C++ Unit Testing Framework

There is no standard unit-testing framework for C++. We will use Catch2 (version 2) in this class, but these notes also discuss gtest.

Catch2

Overview

How To Use With ROS

  1. Make sure the catch_ros package is installed
    • Details about its usage are on its github page.
  2. Create a test directory in your package
  3. <depend> on catch_ros in package.xml
  4. Add catch_ros to the catkin REQUIRED COMPONENTS
Google Test (GTest)

Overview

Debugging

  • Using gdb can greatly help you track down issues in your program
    • Among other features, it enables you to step through the code, examine variables, and break on certain lines
  • use ros2 run --prefix "gdb --args" package node to launch the node in gdb
  • Commands that may be useful are
    • tui enable to view a graphical interface with easier commands
    • run start running the program. Abbreviated with r
    • continue continue running a paused program. Abbreviated with c
    • break file:line or break function to put a breakpoint at a line or a function. Abbreviated with b
    • next go to the next line, skipping over functions. Abbreviated with n
    • step go to the next line, jumping into functions. Abbreviated with s
    • print display the value of a variable or memory address
  • To use gdb most effectively, the code must be compiled with Debugging symbols enabled
    • Use the Debug build type in CMake. See CMake Notes for how to set debugging mode
  • This tutorial helps you launch nodes the debugger

Author: Matthew Elwin