「Google Test」の版間の差分

提供: C++入門
移動: 案内検索
行45: 行45:
  
 
{|class="wikitable"
 
{|class="wikitable"
+ "基本的なアサーション"
+
|+ 基本的なアサーション
 
!致命的なアサーション
 
!致命的なアサーション
 
!致命的ではないアサーション
 
!致命的ではないアサーション
行63: 行63:
  
 
{|class="wikitable"
 
{|class="wikitable"
+ "2つの値の比較"
+
|+ 2つの値の比較
 
!致命的なアサーション
 
!致命的なアサーション
 
!致命的ではないアサーション
 
!致命的ではないアサーション
行97: 行97:
  
 
{|class="wikitable"
 
{|class="wikitable"
+ "文字列の比較"
+
|+ 文字列の比較
 
!致命的なアサーション
 
!致命的なアサーション
 
!致命的ではないアサーション
 
!致命的ではないアサーション

2013年3月9日 (土) 13:07時点における版


Google Test は、 C++ 向けのユニットテストフレームワークです。

読み方

Google Test
ぐーぐる てすと

概要

Google C++ Testing Framework です。

赤と緑のカラー表示で、コンソールでも賑やかでステキです。

インストール

FreeBSDにインストールする場合

ports コレクションからインストールする場合

cd /usr/ports/devel/googletest
sudo make install clean

pkgコマンドでインストールする場合

sudo pkg install googletest

portmasterコマンドでインストールする場合

sudo portmaster -y -d /usr/ports/devel/googletest

ソースからビルド

svn checkout http://googletest.googlecode.com/svn/trunk/ googletest-read-only
cd googletest-read-only
cmake .
gmake

./libgtest.a ./libgtest_main.a

が作成されます。

アサーション

Google Test のアサーションは、マクロです。動作を調べるために、アサーションを利用します。

ASSERT_* バージョンが失敗した場合は、致命的な失敗となり、実行中の関数を中断します。 EXPECT_* バージョンが失敗した場合は、致命的ではない失敗となり、関数を中断しません。

基本的なアサーション

基本的なアサーション
致命的なアサーション 致命的ではないアサーション 検証内容
ASSERT_TRUE(condition); EXPECT_TRUE(condition); condition が true
ASSERT_FALSE(condition); EXPECT_FALSE(condition); condition が false

2つの値の比較

2つの値の比較
致命的なアサーション 致命的ではないアサーション 検証内容
ASSERT_EQ(expected, actual); EXPECT_EQ(expected, actual); expected == actual
ASSERT_NE(val1, val2); EXPECT_NE(val1, val2); val1 != val2
ASSERT_LT(val1, val2); EXPECT_LT(val1, val2); val1 < val2
ASSERT_LE(val1, val2); EXPECT_LE(val1, val2); val1 <= val2
ASSERT_GT(val1, val2); EXPECT_GT(val1, val2); val1 > val2
ASSERT_GE(val1, val2); EXPECT_GE(val1, val2); val1 >= val2

文字列の比較

文字列の比較
致命的なアサーション 致命的ではないアサーション 検証内容
ASSERT_STREQ(expected_str, actual_str); EXPECT_STREQ(expected_str, actual_str); 2つの C 文字列の内容が等しい
ASSERT_STRNE(str1, str2); EXPECT_STRNE(str1, str2); 2つの C 文字列の内容が等しくない
ASSERT_STRCASEEQ(expected_str, actual_str); EXPECT_STRCASEEQ(expected_str, actual_str); 大文字小文字を無視した場合,2つの C 文字列の内容が等しい
ASSERT_STRCASENE(str1, str2); EXPECT_STRCASENE(str1, str2); 大文字小文字を無視した場合,2つの C 文字列の内容が等しくない

main()関数を書く

#include "this/package/foo.h"
#include "gtest/gtest.h"
 
namespace {
 
// The fixture for testing class Foo.
class FooTest : public ::testing::Test {
 protected:
  // You can remove any or all of the following functions if its body
  // is empty.
 
  FooTest() {
    // You can do set-up work for each test here.
  }
 
  virtual ~FooTest() {
    // You can do clean-up work that doesn't throw exceptions here.
  }
 
  // If the constructor and destructor are not enough for setting up
  // and cleaning up each test, you can define the following methods:
 
  virtual void SetUp() {
    // Code here will be called immediately after the constructor (right
    // before each test).
  }
 
  virtual void TearDown() {
    // Code here will be called immediately after each test (right
    // before the destructor).
  }
 
  // Objects declared here can be used by all tests in the test case for Foo.
};
 
// Tests that the Foo::Bar() method does Abc.
TEST_F(FooTest, MethodBarDoesAbc) {
  const string input_filepath = "this/package/testdata/myinputfile.dat";
  const string output_filepath = "this/package/testdata/myoutputfile.dat";
  Foo f;
  EXPECT_EQ(0, f.Bar(input_filepath, output_filepath));
}
 
// Tests that Foo does Xyz.
TEST_F(FooTest, DoesXyz) {
  // Exercises the Xyz feature of Foo.
}
 
}  // namespace
 
int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}


ヘッダファイル

 

ソースコード

 


実行例

 

関連項目