Google TestでFixtureを継承したとき、派生クラスでSetUp/TearDownメソッドをoverrideしない場合は基底クラスのものが呼ばれる。
という確認。
以下、ソースコードと実行結果。
Source code
fixture.h
#ifndef __FIXTURE_TEMPLATE_H__
#define __FIXTURE_TEMPLATE_H__
#include "gtest/gtest.h"
class FixtureBase : public ::testing::Test
{
public:
static void SetUpTestCase();
static void TearDownTestCase();
protected:
virtual void SetUp();
virtual void TearDown();
};
class FixtureWithoutOverride : public FixtureBase
{
};
class FixtureWithOverride : public FixtureBase
{
protected:
virtual void SetUp();
virtual void TearDown();
};
#endif
fixture.cpp
#include "fixture_template.h"
#include "lib/gtestio.h"
void FixtureBase::SetUpTestCase()
{
gtestprintf("FixtureBase::SetUpTestCase\n");
}
void FixtureBase::TearDownTestCase()
{
gtestprintf("FixtureBase::TearDownTestCase\n");
}
void FixtureBase::SetUp()
{
gtestprintf("FixtureBase::SetUp\n");
}
void FixtureBase::TearDown()
{
gtestprintf("FixtureBase::TearDown\n");
}
void FixtureWithOverride::SetUp()
{
gtestprintf("FixtureWithOverride::SetUp\n");
}
void FixtureWithOverride::TearDown()
{
gtestprintf("FixtureWithOverride::TearDown\n");
}
test.cpp
#include "fixture_template.h"
#include "lib/gtestio.h"
#include "gtest/gtest.h"
using ::testing::UnitTest;
using ::testing::TestInfo;
TEST_F(FixtureBase, Test0)
{
const TestInfo *const test_info = UnitTest::GetInstance()->current_test_info();
gtestprintf("Run: %s %s\n", test_info->name(), test_info->test_case_name());
}
TEST_F(FixtureBase, Test1)
{
const TestInfo *const test_info = UnitTest::GetInstance()->current_test_info();
gtestprintf("Run: %s %s\n", test_info->name(), test_info->test_case_name());
}
TEST_F(FixtureWithoutOverride, Test0)
{
const TestInfo *const test_info = UnitTest::GetInstance()->current_test_info();
gtestprintf("Run: %s %s\n", test_info->name(), test_info->test_case_name());
}
TEST_F(FixtureWithoutOverride, Test1)
{
const TestInfo *const test_info = UnitTest::GetInstance()->current_test_info();
gtestprintf("Run: %s %s\n", test_info->name(), test_info->test_case_name());
}
TEST_F(FixtureWithOverride, Test0)
{
const TestInfo *const test_info = UnitTest::GetInstance()->current_test_info();
gtestprintf("Run: %s %s\n", test_info->name(), test_info->test_case_name());
}
TEST_F(FixtureWithOverride, Test1)
{
const TestInfo *const test_info = UnitTest::GetInstance()->current_test_info();
gtestprintf("Run: %s %s\n", test_info->name(), test_info->test_case_name());
}
Output
[==========] Running 6 tests from 3 test cases.
[----------] Global test environment set-up.
[----------] 2 tests from FixtureBase
[ ] FixtureBase::SetUpTestCase
[ RUN ] FixtureBase.Test0
[ ] FixtureBase::SetUp
[ ] Run: Test0 FixtureBase
[ ] FixtureBase::TearDown
[ OK ] FixtureBase.Test0 (8 ms)
[ RUN ] FixtureBase.Test1
[ ] FixtureBase::SetUp
[ ] Run: Test1 FixtureBase
[ ] FixtureBase::TearDown
[ OK ] FixtureBase.Test1 (10 ms)
[ ] FixtureBase::TearDownTestCase
[----------] 2 tests from FixtureBase (23 ms total)
[----------] 2 tests from FixtureWithoutOverride
[ ] FixtureBase::SetUpTestCase
[ RUN ] FixtureWithoutOverride.Test0
[ ] FixtureBase::SetUp
[ ] Run: Test0 FixtureWithoutOverride
[ ] FixtureBase::TearDown
[ OK ] FixtureWithoutOverride.Test0 (54 ms)
[ RUN ] FixtureWithoutOverride.Test1
[ ] FixtureBase::SetUp
[ ] Run: Test1 FixtureWithoutOverride
[ ] FixtureBase::TearDown
[ OK ] FixtureWithoutOverride.Test1 (7 ms)
[ ] FixtureBase::TearDownTestCase
[----------] 2 tests from FixtureWithoutOverride (79 ms total)
[----------] 2 tests from FixtureWithOverride
[ ] FixtureBase::SetUpTestCase
[ RUN ] FixtureWithOverride.Test0
[ ] FixtureWithOverride::SetUp
[ ] Run: Test0 FixtureWithOverride
[ ] FixtureWithOverride::TearDown
[ OK ] FixtureWithOverride.Test0 (12 ms)
[ RUN ] FixtureWithOverride.Test1
[ ] FixtureWithOverride::SetUp
[ ] Run: Test1 FixtureWithOverride
[ ] FixtureWithOverride::TearDown
[ OK ] FixtureWithOverride.Test1 (6 ms)
[ ] FixtureBase::TearDownTestCase
[----------] 2 tests from FixtureWithOverride (24 ms total)
[----------] Global test environment tear-down
[==========] 6 tests from 3 test cases ran. (174 ms total)
[ PASSED ] 6 tests.