Skip to content

Commit d94f8e1

Browse files
author
Shota Aoki
authored
GoogleTestによるユニットテスト環境を追加 (#9)
* googletest環境を追加 * Add test steps to github workflow * googletestのインストール方法を修正 * テスト失敗時に詳細を表示する * jointクラスの初期化テストを追加 * testディレクトリにもcpplintを適用する
1 parent af9d156 commit d94f8e1

File tree

6 files changed

+148
-2
lines changed

6 files changed

+148
-2
lines changed

.github/workflows/build_test.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions/checkout@v2
1212
- run: .tools/cpplint.bash
1313

14-
build:
14+
build_and_test:
1515
needs: lint
1616
strategy:
1717
fail-fast: false # 他のjobが失敗してもキャンセルしない
@@ -26,6 +26,15 @@ jobs:
2626
cd DynamixelSDK/c++/build/linux64
2727
make
2828
sudo make install
29+
- name: Install GoogleTest
30+
run: |
31+
cd ..
32+
curl -OL https://github.com/google/googletest/archive/release-1.11.0.tar.gz
33+
tar -xvf release-1.11.0.tar.gz
34+
mkdir googletest-release-1.11.0/build
35+
cd googletest-release-1.11.0/build
36+
cmake ..
37+
sudo make install
2938
- name: Install dependencies
3039
run: sudo apt install libyaml-cpp-dev
3140
- uses: actions/checkout@v2
@@ -34,3 +43,5 @@ jobs:
3443
- name: Build Samples
3544
run: |
3645
./samples/samples01/build_samples.bash
46+
- name: Test library
47+
run: ./rt_manipulators_lib/run_test_library.bash

.tools/cpplint.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LIB_DIR=$SCRIPT_DIR/../rt_manipulators_lib
77
SAMPLES01_DIR=$SCRIPT_DIR/../samples/samples01
88

99
function cpplint_check () {
10-
cpplint --filter=-build/c++11,-runtime/reference --linelength=100 --extensions=hpp,cpp $1/include/* $1/src/*
10+
cpplint --filter=-build/c++11,-runtime/reference --linelength=100 --extensions=hpp,cpp $1/include/* $1/src/* $1/test/*
1111
}
1212

1313
echo "cpplintを実行し、コードフォーマットをチェックします"

rt_manipulators_lib/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,38 @@ $ ./uninstall_library.bash
5959
## ライブラリの使い方
6060

6161
ライブラリの使い方は[サンプル集のREADME.md](../samples/README.md)を参照してください。
62+
63+
## ライブラリのテスト
64+
65+
### googletestのインストール
66+
67+
```sh
68+
$ mkdir ~/gtest
69+
$ cd ~/gtest
70+
$ curl -OL https://github.com/google/googletest/archive/release-1.11.0.tar.gz
71+
$ tar -xvf release-1.11.0.tar.gz
72+
$ mkdir googletest-release-1.11.0/build
73+
$ cd googletest-release-1.11.0/build
74+
$ cmake ..
75+
$ sudo make install
76+
```
77+
78+
### テストの実行
79+
80+
```sh
81+
$ ./run_test_library.bash
82+
ライブラリをテストします
83+
-- The C compiler identification is GNU 7.5.0
84+
-- The CXX compiler identification is GNU 7.5.0
85+
-- Check for working C compiler: /usr/bin/cc
86+
-- Check for working C compiler: /usr/bin/cc -- works
87+
-- Detecting C compiler ABI info
88+
...
89+
Start 1: JointTest.initialize_id
90+
1/1 Test #1: JointTest.initialize_id .......... Passed 0.00 sec
91+
92+
100% tests passed, 0 tests failed out of 1
93+
94+
Total Test time (real) = 0.00 sec
95+
ライブラリをテストしました
96+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
BUILD_DIR=build
6+
7+
echo "ライブラリをテストします"
8+
cd $(dirname $0)/test
9+
10+
if [ ! -d $BUILD_DIR ]; then
11+
mkdir $BUILD_DIR
12+
fi
13+
14+
cd $BUILD_DIR
15+
cmake ..
16+
make
17+
CTEST_OUTPUT_ON_FAILURE=1 ctest
18+
19+
echo "ライブラリをテストしました"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
# set the project name and version
4+
project(rt_manipulators_cpp_test VERSION 1.0)
5+
6+
# specify the C++ standard
7+
set(CMAKE_CXX_STANDARD 17)
8+
set(CMAKE_CXX_STANDARD_REQUIRED True)
9+
10+
enable_testing()
11+
12+
find_package(GTest REQUIRED)
13+
14+
include(GoogleTest)
15+
16+
add_executable(test_joint test_joint.cpp)
17+
target_link_libraries(test_joint
18+
GTest::GTest
19+
GTest::Main
20+
rt_manipulators_cpp
21+
)
22+
23+
gtest_discover_tests(test_joint)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2021 RT Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <memory>
16+
17+
#include "gtest/gtest.h"
18+
#include "rt_manipulators_cpp/joint.hpp"
19+
20+
class JointTestFixture : public ::testing::Test {
21+
protected:
22+
virtual void SetUp() {
23+
uint8_t id = 0;
24+
uint8_t operating_mode = 1;
25+
double min_position_limit = 2.0;
26+
double max_position_limit = 3.0;
27+
double current_limit_when_position_exceeds_limit = 4.0;
28+
test_joint = std::make_shared<joint::Joint>(
29+
id, operating_mode, max_position_limit, min_position_limit,
30+
current_limit_when_position_exceeds_limit);
31+
}
32+
33+
virtual void TearDown() {
34+
test_joint.reset();
35+
}
36+
37+
std::shared_ptr<joint::Joint> test_joint;
38+
};
39+
40+
TEST_F(JointTestFixture, initialize_id) {
41+
EXPECT_EQ(test_joint->id(), 0);
42+
}
43+
44+
TEST_F(JointTestFixture, initialize_operating_mode) {
45+
EXPECT_EQ(test_joint->operating_mode(), 1);
46+
}
47+
48+
TEST_F(JointTestFixture, initialize_min_position_limit) {
49+
EXPECT_DOUBLE_EQ(test_joint->min_position_limit(), 2.0);
50+
}
51+
52+
TEST_F(JointTestFixture, initialize_max_position_limit) {
53+
EXPECT_DOUBLE_EQ(test_joint->max_position_limit(), 3.0);
54+
}
55+
56+
TEST_F(JointTestFixture, initialize_current_limit) {
57+
EXPECT_DOUBLE_EQ(test_joint->current_limit_when_position_exceeds_limit(), 4.0);
58+
}

0 commit comments

Comments
 (0)