ros test 基本是基于gtest.
如何编写测试模块
cmakelists.txt
if (CATKIN_ENABLE_TESTING)catkin_add_gtest(test_mongo_roscpp test/test_mongo_ros.cpp)target_link_libraries(test_mongo_roscpp warehouse_ros)
endif ()
CATKIN_ENABLE_TESTING 是catkin 专门对test 内容的编译选项开关, 可以通过catkin_make -DCATKIN_ENABLE_TESTING 进行编译选项控制
package.xml
<!-- gtest依赖项声明方式1 -->
<test_depend>gtest</test_depend><!-- gtest依赖项声明方式1 -->
<build_export_depend>gtest</build_export_depend>
test 文件所在目录
build/PKG_NAME/
如何编译
test 模块有两个关键词, tests, run_tests
tests 指仅编译
run_tests 指编译+执行run
test 好像是执行ctest,一般不用
方式1: make
cd build ## build 目录存放test 内容。这一层编译所有包
make tests -j8 -l8 ## only build
make run_tests -j8 -l8 ## build + run
更高粒度,进入目录
cd build/foo
make run_tests_<TAB><TAB>
方式2:catkin_make
Running unit tests — catkin 0.5.90 documentation
Unit Testing — ROS Training For Industry 0.1 documentation
cd YOUR_WS
catkin_make tests ## onlly build
catkin_make tests <YOUR_PKG_NAME> ## only build specific pkg test
catkin_make run_tests ## build + run
catkin_make run_tests <YOUR_PKG_NAME> ## build + run specific pkg test
catkin_make run_tests_pkgname
catkin_make run_tests_pkgname_gtest_mytest
catkin_make run_tests_<TAB><TAB>
测试框架tips
https://google.github.io/googletest/reference/assertions.html
Google包装了一系列EXPECT_*
和ASSERT_*
的宏,区别是:
① EXPECT_*
失败时,测试用例继续往下执行;
② ASSERT_*
失败时,直接在当前函数中返回,当前函数中ASSERT_*
后面的语句将不会执行;
测试用例结束后,总结test 结果
catkin_test_results build/test_results