gtest

Last-modified: 2009-01-15 (木) 08:56:29

gtestとは

Googleのプロダクトで使われているオープンソースのテストフレームワーク。
C++で書かれていて、多数のプラットフォームで動作可能。

入手方法

こちら→ http://code.google.com/p/googletest/ から最新版を取得する。

使い方

ひとまず自分でビルドする。

  • Visual C++ なら、msvcフォルダのソリューションを使用する。
  • gnu make環境なら、makeフォルダのメイクファイルを使用する。
  • mac xcode環境なら、xcodeフォルダのプロジェクトファイルを使用する。

テストコードを書いてみる。

#include <gtest/gtest.h>
int tasu(int a, int b)
{
  return a - b;
}
int main(int argc, char** argv)
{
  testing::InitGoogleTest(&argc, argv);
  EXPECT_EQ(1 + 2, tasu(1, 2));
  RUN_ALL_TESTS();
  return 0;
}

結果を眺める

d:\code\test_gtest\main.cpp(11): error: Value of: tasu(1, 2)
  Actual: -1
Expected: 1 + 2
Which is: 3
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran.
[  PASSED  ] 0 tests.
[  FAILED  ] 0 tests, listed below:
 0 FAILED TESTS