環境
この記事は以下の環境で動いています。
| 項目 | 値 |
|---|---|
| CPU | Core i5-8250U |
| Ubuntu | 22.04 |
| ROS2 | Humble |
| Qt | 5.15.3 |
| SQLite | 3.37.2 |
概要
ROS2にはcolcon testという強力なテストツールがあります。このツールでコードの不備を指摘することが出来ます。なかなか全機能OKの状態を維持するのは大変ですが、実行することでコードをクリーンな状態で保てます。
コードスタイルチェッカー
colcon testでは以下の8項目に対応しています。
| pkg名 | 対象 | 説明 |
|---|---|---|
| ament_cmake_copyright | c++/python/cmake | ライセンスなどの記載の確認 |
| ament_cmake_cppcheck | c++ | 論理エラー(バッファオーバーフロー等)の検知 |
| ament_cmake_cpplint | c++ | コードスタイルチェッカー |
| ament_cmake_flake8 | python | コードスタイルチェッカー |
| ament_cmake_lint_cmake | cmake | コードスタイルチェッカー |
| ament_cmake_pep257 | python | pep257スタイルチェッカー |
| ament_cmake_uncrustify | c++ | uncrustifyスタイルチェッカー |
| ament_cmake_xmllint | xml | コードスタイルチェッカー |
unit_test
colcon testではgtestを使ったunit_testの実行に対応しています。
ソースコード
c++
今回は単純な足し算をするライブラリを含むpackageを作成します。これにたいしてコードスタイルチェッカーの適用ととgtestの記載をします。
コードチェッカーをパスするためにライセンスの記載なども行っています。
// Copyright 2024 project-srs.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
#pragma once
namespace test_lecture
{
int add(const int a, const int b);
}
// Copyright 2024 project-srs.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
#include <test_lecture/calculator.hpp>
int test_lecture::add(const int a, const int b)
{
return a + b;
}
package.xml
テスト依存を記載します。ここでは今回のコードチェックで使う内容とgtestの依存を書きます。
<package format="3">
<buildtool_depend>ament_cmake</buildtool_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>ament_cmake_copyright</test_depend>
<test_depend>ament_cmake_cppcheck</test_depend>
<test_depend>ament_cmake_cpplint</test_depend>
<test_depend>ament_cmake_lint_cmake</test_depend>
<test_depend>ament_cmake_uncrustify</test_depend>
<test_depend>ament_cmake_xmllint</test_depend>
<test_depend>ament_cmake_gtest</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
CmakeLists
ライブラリのビルドに関連する部分です。
include_directories(include)
add_library(${PROJECT_NAME} SHARED
src/calculator.cpp
)
ament_target_dependencies(${PROJECT_NAME}
rclcpp
)
install(
DIRECTORY include/
DESTINATION include
)
ament_export_dependencies(rclcpp)
ament_export_libraries(${PROJECT_NAME})
ament_export_include_directories(include)
ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET)
install(
TARGETS ${PROJECT_NAME}
EXPORT export_${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
find_package(ament_cmake_gtest REQUIRED)
ament_add_gtest(${PROJECT_NAME}_test
test/calculator_test.cpp
)
target_include_directories(${PROJECT_NAME}_test PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(${PROJECT_NAME}_test
${PROJECT_NAME}
)
endif()
-
BUILD_TESTINGは通常のcolcon buildではtrueになっています。 -
find_package(ament_lint_auto REQUIRED)でpackage.xmlに記載したtest関連の依存をロードします。ament_lint_auto_find_test_dependencies()を記載することでcolcon test時にコードチェック系の実行をします。- cmakeのテンプレートである
set(ament_cmake_copyright_FOUND TRUE)はament_cmake_copyrightでの、set(ament_cmake_cpplint_FOUND TRUE)はament_cmake_cpplintでのコピーライトのチェックを無効にするオプションです。コピーライトのチェックの判定が厳しいので無効にすることが多いと思われます。
- cmakeのテンプレートである
-
ament_add_gtest()でgtestの実行の追加が出来ます。colcon buildではgtestのビルドはされますが、テストの実行はされません。
ビルド
source /opt/ros/humble/setup.bash
cd ros2_ws
colcon build
テストの実行
source /opt/ros/humble/setup.bash
cd ros2_ws
colcon test
-
colcon test --packages-select test_lectureの様にpackageを指定してテスト実行もできます。 - テストfailがあるpackageは出力で表示されます。
- failの内容は
ros2_ws/log/test_{日時}/{package名}/stdout.txtやros2_ws/build/{package名}/Testing/{日時}/Test.xmlで確認できます。
補足
- version2.7の
cppcheckは実行が遅いという問題があるようで、デフォルトではcppcheckがスキップするようになっています。export AMENT_CPPCHECK_ALLOW_SLOW_VERSIONS=1の環境変数をセットするとスキップせずに実行します。 -
cppcheckは拡張子がh/cファイルはc言語として、hpp/cppファイルはc++言語として解釈されます。
コードフォーマットのかけ方
ament_cmake_flake8
pip install blackでインストール、black ./でカレントディレクトリ以下の対象ファイルでフォーマットを適用します。
ament_cmake_uncrustify
ament_uncrustify --reformatを実行することで、カレントディレクトリ以下の対象ファイルでフォーマットを適用します。