ROS_Test - RicoJia/notes GitHub Wiki

========================================================================

ROS unit testing

========================================================================

  1. testing: you have three levels of testing:

    1. rosunit (unit test, same functionality as gtest)
    2. google gtest
    3. rostest (launch file that launches multiple nodes)
  2. gtest for ROS

    1. Basic procedure:
    • In test.cpp
    TEST(TestSuite, testCase2)
    {
      const tf2::Stamped<Eigen::Vector3d> v(Eigen::Vector3d(1,2,3), ros::Time(5), "test");
      tf2::Stamped<Eigen::Vector3d> v1;
      geometry_msgs::PointStamped p1;
      tf2::convert(v, p1);
      tf2::convert(p1, v1);
      EXPECT_EQ(v, v1);
    }
    int main(int argc, char **argv){
     testing::InitGoogleTest(&argc, argv);
     return RUN_ALL_TESTS();
    }
    • In CMakelist.txt
    if(CATKIN_ENABLE_TESTING)
     catkin_add_gtest(tf2_eigen-test test/tf2_eigen-test.cpp)
     target_link_libraries(tf2_eigen-test ${catkin_LIBRARIES} ${GTEST_LIBRARIES})
    endif()
    
    • In package.xml
     <test_depend>rosunit</test_depend>
    
    • Run test with
      1. catkin_make
        • ./bin/test/utest
        • or ```catkin_make run_tests``, or
        • or catkin_make run_tests<TAB><TAB> to find the package to run.
      2. catkin build
      catkin build -v -i --make-args tests -- PKG_NAME
      catkin build -v -i --make-args test -- PKG_NAME
      catkin_test_results --verbose build  #(need this to view results)
      
    1. TestSuite is CamelCased testFunction is camelCased.

    2. std::cout should work!

    3. Test expressions Use ASSERT when we stop the test when a test fail. Use EXPECT so we can still get values out and continue the test.

    • add custom message ASSERT_EQ(ans.x, vec.x)<< "Talker add x's failed"<<.ans.x;
    • string comparison EXPECT_STREQ(str1,str2);
    • near EXPECT_NEAR(val1, val2, abs_error);

    Internal Working: Print from rostest node: rostest is running on a randomly assigned port, and you don't know which port they're running. A: so after catkin_make run_all_tests, which compiles and launches the rostest. test, you can do $rostest test_file -r, then use rosconsole

========================================================================

GDB

======================================================================== https://web.eecs.umich.edu/~sugih/pointers/gdbQS.html

Basic procedure:

  1. catkin_make -DCMAKE_BUILD_TYPE=Debug

    • if there's no extra compile mode, use this to debug. (in gdb, it should be g++ -g something.cc -o something)
  2. rosrun --prefix 'gdb --args' nuslam landmarks

  3. rosparam load dump.yaml namespace for loading the yaml file.

  4. if your hit run, the program will stop at where it crashes. or hit ctrl+c, where it will stop somewhere.

  5. if you see some system function, turn on tui: tui enable, hit up

  6. Keep Gazebo and Rviz running, and run the launch file separately.

  7. Use a launchfile. add launch-prefix="gdb --args" output="screen"

  8. GDB commands

    1. bt: backtrace.

    2. p is print a. print eigen vectors and values. print *X.data()@Length_X where X is the eigen variable and Length_X is the product of its rows and columns

    3. you can do p mu.operator(something) you can run an operation.

    4. n is next line, it will skip an entire function!!

    5. b linenum: sets break point. in ros, do b namespace::class::function:linenum

    6. info b: see breakpoints

    7. delete breakpoint_num: delete a breakpoint

    8. s is step

    9. c is continue

    10. 牛逼: f is frame, which is the level of the function stack you're in. so you can go back to the previous frame and print the variables there!!!!

========================================================================

Python ROS Test - unittest

========================================================================

  1. rostest is 100% compatible with roslaunch
    • One difference is all nodes are shutdown automatically, once tests are done
    • are tests launched one at a time?
⚠️ **GitHub.com Fallback** ⚠️