3
5

More than 5 years have passed since last update.

備忘:CppUTestのインストールからコンパイルまで

Last updated at Posted at 2019-01-22

libCppUTest.a/libCppUTestExt.aの存在に気が付かず数日無駄にしたのでメモとして

  • 更新
    • 19/01/25 : CppUTestをGitHubから持ってくるように変更

環境

Ubuntu 18.04 x64

環境構築

コンパイラやらを適当にインストールする

bash
sudo apt install g++ build-essential git cmake

CppUTestのインストール

bash
# GitHubからソースをもってくる
git clone git://github.com/cpputest/cpputest.git
# CppUTestのビルド
cd cpputest_build
cmake ..
make
# CppUTestのインストール
make install

環境変数を設定する
.bashrcもしくは.bash_profileに下記を一文を追記

bash
# 環境にあわせてね
# 自分の環境では'/usr/local'にインストールされていた
export CPPUTEST_HOME=/usr/local

# 設定を反映させる。例だと.bashrc
source ~/.bashrc

これでインストール作業はおしまい
スクリプトとかあるらしいけどうまくいってない

適当なコードを書いてコンパイルする

適当なコード

HelloCppUTest.cpp
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTest/TestHarness.h>

TEST_GROUP(TestGroup1){};
TEST(TestGroup1, Test1)
{
    STRCMP_EQUAL("hello", "world");
}

TEST(TestGroup1, Test2)
{
    LONGS_EQUAL(1, 2);
}

int main(int argv, char** argc)
{
  return CommandLineTestRunner::RunAllTests(argv, argc);
}

コンパイルする

bash
g++ HelloCppUTest.cpp -o HelloCppUTest $CPPUTEST_HOME/lib/libCppUTest.a

実行結果

bash
HelloCppUTest.cpp:12: error: Failure in TEST(TestGroup1, Test2)
        expected <1 0x1>
        but was  <2 0x2>

.
HelloCppUTest.cpp:7: error: Failure in TEST(TestGroup1, Test1)
        expected <hello>
        but was  <world>
        difference starts at position 0 at: <          world     >
                                                       ^

.
Errors (2 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 0 ms)

コンパイルが通り2件のfailがあれば成功

CMakeでビルド

めんどくさいことはどんどん自動化ということでCMakeを使う
さっきのソースがビルドできるだけの`CMakeLists.txt

CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Project1)

file(GLOB SOURCES
    "./*.c" "./*.cpp"
)

add_executable(cmake_HelloCppUTest ${SOURCES})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)

# add cpputest library
target_link_libraries(cmake_HelloCppUTest $ENV{CPPUTEST_HOME}/lib/libCppUTest.a)
target_link_libraries(cmake_HelloCppUTest $ENV{CPPUTEST_HOME}/lib/libCppUTestExt.a)

ビルドする。
ディレクトリが汚れるのは嫌なのでビルド用のディレクトリを作成する。

bash
mkdir build && cd build
cmake ..
make

# 実行する
./bin/cmake_HelloCppUTest

おわり

参考

あとがき

TDDを学びたく『テスト駆動開発による組み込みプログラミング』を手に取るも環境構築に苦しむ。
静的だの動的ライブラリだのの理解が足りていないと自覚できたのでいい機会だった。

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5