Category: Courses

  • GTest Framework for ROS2 – Enterprise Courses

    GTest Framework for ROS2: A 4-Month Self-Study Course

    Course Syllabus

    Course Description

    This comprehensive 4-month self-study course is designed to equip learners with the knowledge and practical skills to effectively use the GTest Framework for robust unit testing in ROS2 applications. Whether you’re a motivated beginner or an intermediate developer looking to solidify your testing expertise, this course will guide you through the fundamentals of GTest, its integration with ROS2, and advanced testing techniques. Through engaging lessons, clear explanations, and hands-on examples, you will learn to write effective unit tests, identify and debug issues, and ensure the reliability and maintainability of your ROS2 robotic systems.

    Primary Learning Objectives

    Upon successful completion of this course, you will be able to:

    • Understand the principles of unit testing and its importance in robotics software development.
    • Master the fundamentals of the Google Test (GTest) framework.
    • Integrate GTest seamlessly into ROS2 packages using ament_cmake.
    • Write various types of unit tests for ROS2 nodes, services, and topics.
    • Implement best practices for effective and maintainable ROS2 unit tests.
    • Utilize advanced GTest features for complex testing scenarios.
    • Debug and troubleshoot common testing failures in ROS2 environments.
    • Develop a robust testing strategy for your ROS2 robotics projects.

    Necessary Materials

    • Computer with Ubuntu 20.04 (or later) installed.
    • ROS2 Foxy (or later) installed and configured.
    • C++ compiler (e.g., g++).
    • CMake (version 3.10 or later).
    • Git for version control.
    • An IDE or text editor (e.g., VS Code, Sublime Text).
    • Internet access for additional resources and documentation.

    Course Content: 14 Weekly Lessons


    Week 1-2: Foundations of Unit Testing and Introduction to GTest

    Lesson 1: Understanding Unit Testing in Robotics

    • Learning Objectives:
      • Define unit testing and explain its benefits in the context of robotics software.
      • Differentiate between unit, integration, and system tests.
      • Understand the concept of test-driven development (TDD) in robotics.
    • Key Vocabulary:
      • Unit Test: A software testing method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use.
      • Integration Test: A phase in software testing in which individual software modules are combined and tested as a group.
      • System Test: Testing conducted on a complete, integrated system to evaluate the system’s compliance with its specified requirements.
      • Test-Driven Development (TDD): A software development process relying on the repetition of a very short development cycle: requirements are turned into very specific test cases, then the software is improved to pass the new tests, only then is the new code refactored.
    • Content:

      Unit testing is a critical practice in modern software development, and its importance is amplified in robotics. In robotics, bugs can lead to unpredictable robot behavior, safety hazards, and significant financial losses. Unit tests allow developers to verify the smallest, independent parts of their code (units) in isolation. This early detection of defects reduces debugging time, improves code quality, and facilitates refactoring. We’ll explore how unit tests fit into a larger testing strategy that includes integration and system tests, and briefly touch upon the principles of Test-Driven Development (TDD) as a proactive approach to writing robust code.

    • Practical Hands-on Example:

      Set up your ROS2 workspace. Create a new ROS2 package named my_robot_pkg using ament_cmake and verify its structure.

    Lesson 2: Introduction to Google Test (GTest)

    • Learning Objectives:
      • Describe the core features and philosophy of the GTest framework.
      • Set up a basic GTest project and compile a simple test executable.
      • Understand basic GTest assertions for validating code behavior.
    • Key Vocabulary:
      • Assertion: A statement in a computer program that a programmer expects to be true at a certain point in the execution of the program.
      • Test Fixture: A set of conditions that are set up before running a test case and torn down afterwards.
      • Test Case: A specification of the inputs, execution conditions, testing procedure, and expected results that define a single test to be performed.
      • Test Suite: A collection of test cases that are intended to be used to test a software program to show that it has a specified set of behaviors.
    • Content:

      GTest is a powerful, open-source C++ test framework developed by Google. It provides a rich set of macros and functions to write various types of tests. Its key features include value-parameterized tests, type-parameterized tests, and a flexible test fixture mechanism. We will start by creating a simple C++ project, integrating GTest, and writing our first test. This will involve understanding basic assertions like ASSERT_EQ, EXPECT_TRUE, and ASSERT_NEAR, which are crucial for verifying expected outcomes.

    • Practical Hands-on Example:

      Create a new C++ project outside of your ROS2 workspace. Configure CMake to include GTest. Write a simple Calculator class with add, subtract, multiply, and divide methods. Write a GTest test file with a few test cases for the Calculator class, using ASSERT_EQ and EXPECT_TRUE. Compile and run your tests.


    Week 3-4: Integrating GTest with ROS2

    Lesson 3: ament_cmake and GTest Integration

    • Learning Objectives:
      • Understand how ament_cmake handles dependencies and build processes in ROS2.
      • Integrate GTest as a testing dependency within a ROS2 ament_cmake package.
      • Configure CMakeLists.txt for building and running GTest tests in ROS2.
    • Key Vocabulary:
      • ament_cmake: The CMake build system for ROS2 packages.
      • Package.xml: A manifest file that describes a ROS2 package.
      • find_package: CMake command to locate and include external packages.
      • add_test: CMake command to add a test to the build system.
    • Content:

      ROS2 uses the ament_cmake build system, which extends CMake for ROS2-specific functionalities. To use GTest within a ROS2 package, we need to correctly declare GTest as a dependency in package.xml and configure CMakeLists.txt to find and link against the GTest libraries. This lesson will walk you through the necessary steps to set up your ROS2 package for GTest, ensuring that your tests are properly discovered and executed by the ROS2 build system.

    • Practical Hands-on Example:

      In your my_robot_pkg created earlier, modify package.xml to add GTest as a test dependency. Then, modify CMakeLists.txt to:

      1. Find the GTest package.
      2. Add a new executable for your tests.
      3. Link your test executable against GTest libraries.
      4. Use ament_add_test to register your GTest executable as a ROS2 test.

      Write a very simple GTest test file that does nothing but print “Hello from ROS2 GTest!”. Build and run your ROS2 package tests using colcon test.

    Lesson 4: Basic Testing of ROS2 Nodes