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
- Catch2 is a light-weight unit-testing framework for C++
- The Tutorial
- The Reference Documentation
- The catch_ros2 package enables the use of Catch2 with ROS 2
Google Test (GTest)
Overview
- The most common C++ unit testing framework in ROS is Google Test.
- Google Test Primer
- Google Test Advanced (Not too advanced for you)
- ROS 2 GTest Tutorial
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 ingdb
- Commands that may be useful are
tui enable
to view a graphical interface with easier commandsrun
start running the program. Abbreviated withr
continue
continue running a paused program. Abbreviated withc
break file:line
orbreak function
to put a breakpoint at a line or a function. Abbreviated withb
next
go to the next line, skipping over functions. Abbreviated withn
step
go to the next line, jumping into functions. Abbreviated withs
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 inCMake
. See CMake Notes for how to set debugging mode
- Use the
- This tutorial helps you launch nodes the debugger