Last active
February 24, 2023 13:18
-
-
Save raimusyndrome/8052049 to your computer and use it in GitHub Desktop.
GoogleTestでよく使う用法集
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gtest/gtest.h> | |
// シンプルなテスト | |
TEST(TestCategory, TestName){ | |
// 真偽値判定 | |
ASSERT_TRUE(true); | |
ASSERT_FALSE(false); | |
EXPECT_TRUE(true); | |
EXPECT_FALSE(false); | |
// 数値判定 | |
ASSERT_EQ(1,1); | |
ASSERT_NE(2,5); | |
EXPECT_EQ(1,1); | |
EXPECT_NE(2,5); | |
// 文字列判定 | |
ASSERT_STREQ("arm","arm"); | |
ASSERT_STRNE("arm","ARM"); | |
EXPECT_STREQ("arm","arm"); | |
EXPECT_STRNE("arm","ARM"); | |
// 浮動小数点判定 | |
ASSERT_FLOAT_EQ(1.1, 1.2); | |
ASSERT_DOUBLE_EQ(23.35, 23.35); | |
EXPECT_FLOAT_EQ(1.1, 1.2); | |
EXPECT_DOUBLE_EQ(23.35, 23.35); | |
ASSERT_NEAR(1.0, 1.01, 0.01); | |
EXPECT_NEAR(1.0, 1.01, 0.01) | |
<< "data=" << ::testing::PrintToString(1.0); | |
} | |
// テストフィクスチャを利用したテスト | |
class SampleTestCase : public ::testing::Test | |
{ | |
protected: | |
// 各テストケースのの実行前に実施する処理を記述します。 | |
virtual void SetUp() { | |
// テストの前処理 | |
} | |
// 各テストケースのの実行後に実施する処理を記述します。 | |
virtual void TearDown(){ | |
// テストの後処理 | |
} | |
}; | |
// テスト本体 | |
TEST_F(SampleTestCase, TestName){ | |
} | |
// パラメータ化したテスト | |
typedef std::tr1::tuple<int, int> testParam; | |
class ParameteredTestCase : public SampleTestCase, public ::testing::WithParamInterface<testParam> | |
{ | |
}; | |
// テスト本体 | |
TEST_P(ParameteredTestCase, TestName){ | |
testParam param = GetParam(); | |
int i1 = std::tr1::get<0>(param); | |
int i2 = std::tr1::get<1>(param); | |
} | |
// テストパターン生成 | |
INSTANTIATE_TEST_CASE_P(CategoryName, ParameteredTestCase, | |
::testing::Combine(::testing::Values(0,1), ::testing::Range(1,3))); | |
const testParam PARAM_LIST[] = | |
{ | |
std::tr1::make_tuple( 10, 22 ), | |
std::tr1::make_tuple( 153, 111), | |
std::tr1::make_tuple( 44, 22 ), | |
}; | |
INSTANTIATE_TEST_CASE_P(Tuple, ParameteredTestCase, | |
::testing::ValuesIn(PARAM_LIST) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment