エンジニアのソフトウェア的愛情

または私は如何にして心配するのを止めてプログラムを・愛する・ようになったか

googletestに手を出す

シンプルで扱いやすいと耳にして、早速ダウンロード。
GitHub - google/googletest: Google Test

サンプルコードを眺めているところですが、確かに簡単そう。試してみます。

最初のテスト

#include <gtest/gtest.h>

// テストされる関数
int add(int x, int y)
{
    return x + y;
}

// テストする関数: テストケース名 AddTest / テスト名 Add
TEST(AddTest, Add)
{
    EXPECT_EQ(1, add(1, 0));
    EXPECT_EQ(1, add(0, 1));
    EXPECT_EQ(2, add(1, 1));
}

// テストの実施
int main(int argc, char* argv[])
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

コンパイル

$ g++ -ansi -Wall gtest_test.cpp -lgtest

実行結果。

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from AddTest
[ RUN      ] AddTest.Add
[       OK ] AddTest.Add
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran.
[  PASSED  ] 1 test.

扱いやすくてなんかいい感じです。


わたしの環境はMac OS X (Tiger PPC)ですが、今のところ問題なく動いてます。