MacにGoogle Testをインストール

Google TestをMacにインストールしました。

githubにある公式の一般的なビルド手順の実施するだけで可能です。

あわせて読みたい
目次

cmakeをインストール

Google Testはcmakeでビルドするため、cmakeをインストールします。

作成したフォルダに移動します。

%brew install cmake

Google Testインストール

ビルド用のフォルダを作成

GoogleTestをgitから取得するために、適当なフォルダを作成します。

mkdir xxxx
cd ./xxxx

GoogleTestをGitからクローン

githubからGoogleTestをクローンします。

% git clone https://github.com/google/googletest.git -b v1.13.0

クローンしたリポジトリに移動します。


% cd googletest        # Main directory of the cloned repository.

GoogleTestをビルド

ビルド用のディレクトリを作成し、移動しcmakeを実行します。
GoogleTestのビルドスクリプトが生成されます。

% mkdir build          # Create a directory to hold the build output.
% cd build

% cmake ..
-- The C compiler identification is AppleClang 14.0.3.14030022
-- The CXX compiler identification is AppleClang 14.0.3.14030022
...
-- Configuring done (1.6s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/xxxx/bulid/googletest/build

ビルドを実行します。

% make
[ 12%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 25%] Linking CXX static library ../lib/libgtest.a
[ 25%] Built target gtest
[ 37%] Building CXX object googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 50%] Linking CXX static library ../lib/libgmock.a
[ 50%] Built target gmock
[ 62%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[ 75%] Linking CXX static library ../lib/libgmock_main.a
[ 75%] Built target gmock_main
[ 87%] Building CXX object googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library ../lib/libgtest_main.a
[100%] Built target gtest_main

インストールします。

usr/localにインストールされ、ライブラリやヘッダーファイルを読み込めるようになります。

% sudo make install
Password:
[ 25%] Built target gtest
[ 50%] Built target gmock
[ 75%] Built target gmock_main
[100%] Built target gtest_main
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/include
...
-- Installing: /usr/local/lib/libgtest.a
-- Installing: /usr/local/lib/libgtest_main.a
-- Installing: /usr/local/lib/pkgconfig/gtest.pc
-- Installing: /usr/local/lib/pkgconfig/gtest_main.pc

Google Testを使う

Google Testを使用するコードを作ってビルドします。

#include <gtest/gtest.h>

// 第1引数がテストケース名、第2引数がテスト名
TEST(Test, Function1Test) {
    ASSERT_TRUE(true);
}


gtest、gtest_mainのライブラリをリンクしてビルドします。

% clang++ -std=c++1y ./gtest.cpp -lgtest_main -lgtest

実行します。

% ./a.out
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from MyLibraryTest
[ RUN      ] MyLibraryTest.Function1Test
[       OK ] MyLibraryTest.Function1Test (0 ms)
[----------] 1 test from MyLibraryTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

コメント

コメントする

CAPTCHA


目次