WSLにgoogle testを入れてみる

WSLにgoogle testを入れてみる

ふと思い立って試してみたので、メモとして残しておきます。

インストール

ソースコードをDL&展開

wget https://github.com/google/googletest/archive/release-1.8.0.tar.gz
tar zxvf release-1.8.0.tar.gz
cd googletest

ビルド

cmakeが必要なので、インストールします。

sudo apt install cmake
mkdir build
cd build
cmake ..
sudo make && sudo make install

成功していれば、ls /usr/local/libとした際に

libgtest.a  libgtest_main.a

が表示されるはずです。

使ってみる

適当なコードを用意

サンプルとして超適当なコードを用意します。
gtest_sample.cpp

#include <iostream>
#include "gtest_sample.h"

bool sample_function(const uint8_t input, uint8_t &output) {
    std::cout << "called sample_function" << std::endl;
    output = input;
    return true;
}

int main() {
    uint8_t input = 16;
    uint8_t output = 0;
    bool ret = sample_function(input, output);
    std::cout  <<  "output: "  <<  static_cast<uint16_t>(output) <<  std::endl;
    std::cout  <<  "ret   : "  <<  static_cast<uint16_t>(ret) <<  std::endl;
    return 0;
}

gtest_sample.h

#include  <stdint.h>

bool  sample_function(const  uint8_t  input, uint8_t  &output);

ビルドして実行

g++ gtest_sample.cpp -o gtest_sample.exe
./gtest_sample.exe
called sample_function
output: 16
ret   : 1

とりあえず動くようです。

適当なテストコードを用意

今度は超適当なテストコードを用意します。

#include "gtest/gtest.h"
#include "gtest_sample.h"

class SampleTest : public ::testing::Test {
protected:
    virtual void SetUp() {
    }
    virtual void TearDown() {
    }
};

// inputとして3を入れると、outputを3にしてtrueを返すことを確認
TEST_F(SampleTest, test_no_01) {
    uint8_t input = 3;
    uint8_t output = 0;
    bool ret = sample_function(input, output);
    std::cout << "test start!" << std::endl;

    // evaluation
    EXPECT_EQ(3, output);
    EXPECT_EQ(true, ret);
}

ビルド

google testをビルドする際には、テスト対象のオブジェクトをリンクする必要があります。
また、main関数はgoogle testが用意してくれているものが使えるため、自分のものは不要です。
そこで、

  • gtest_sample.cppのmain関数をコメントアウト
  • 実行ファイルでなくオブジェクトファイルを作成(ビルド時に-cオプションを付与)

します。

g++ gtest_sample.cpp -c -o gtest_sample.o

google testのビルドはそのオブジェクトファイルを使って行います。

g++ gtest_sample_test.cpp gtest_sample.o -lgtest_main -lgtest -lpthread -o test.exe

コードを修正してビルドして…を繰り返すことを考えると、makefileを作った方がよさそうです。

テスト実行

テストを実行すると、こんな感じでログが出力されます。
std::coutの標準出力も入っています。

./test.exe
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from SampleTest
[ RUN      ] SampleTest.test_no_01
called sample_function
test start!
[       OK ] SampleTest.test_no_01 (1 ms)
[----------] 1 test from SampleTest (6 ms total)

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

なお、テストが失敗する場合はこのようになります。
設定した期待値と実際の値の両方を教えてくれています。

./test.exe 
Running main() from gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from SampleTest
[ RUN      ] SampleTest.test_no_01
called sample_function
test start!
gtest_sample_test.cpp:55: Failure
      Expected: true
To be equal to: ret
      Which is: false
[  FAILED  ] SampleTest.test_no_01 (2 ms)
[----------] 1 test from SampleTest (2 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (5 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] SampleTest.test_no_01

 1 FAILED TEST

まとめ

WSL環境にgoogle testを導入し、サンプルを使って実行してみました。
(実は、地味にmain関数絡みでハマりました・・・)
makekefileやgoogle mockもいずれ試してみたいところです。

Written with StackEdit.

コメント

このブログの人気の投稿

Cっぽいコードでgoogle testとgoogle mockを使ってみる

Cっぽいコードでgtestとgmockを使ってみる その2