目录
test宏TESTTEST_FTEST_PINSTANTIATE_TEST_SUITE_PTYPED_TESTTYPED_TEST_SUITETYPED_TEST_PFRIEND_TESTSCOPED_TRACEGTEST_SKIPGTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST
类和类型::testing::AssertionResult::testing::AssertionException::testing::EmptyTestEventListener::testing::Environment::testing::ScopedTrace::testing::Test::testing::TestWithParam<T>TestSuite::testing::TestInfo::testing::TestParamInfo<T>::testing::UnitTest::testing::TestEventListener::testing::TestEventListeners::testing::TestPartResult::testing::TestProperty::testing::TestResult::testing::TimeInMillis::testing::Types<T…>::testing::WithParamInterface<T>
函数InitGoogleTestAddGlobalTestEnvironmentRegisterTestRUN_ALL_TESTSAssertionSuccessAssertionFailureStaticAssertTypeEqPrintToStringPrintToStringParamName
test宏
TEST
如下示例,在名为TestSuiteName的test suite下定义一个名为TestName的test。
TEST(TestSuiteName, TestName) {
... statements ...
}
123
TestSuiteName和TestName不能包含下划线
不同的test suite下可以有相同的test名称
TEST_F
如下,在test fixture的类名为TestFixtureName下定义一个名为TestName 的test
TEST_F(TestFixtureName, TestName) {
... statements ...
}
123
同TEST,TestFixtureName和TestName名称中不能有下划线
TEST_P
TEST_P(TestFixtureName, TestName) {
... statements ...
}
123
test fixture的类名为TestFixtureName,私有的test name为TestName,不能使用下划线命名。TestFixtureName必须是value-parameterized test fixture的类名
可以通过 GetParam()
函数获取test的参数,如下
TEST_P(MyTestSuite, DoesSomething) {
...
EXPECT_TRUE(DoSomething(GetParam()));
...
}
12345
INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(InstantiationName,TestSuiteName,param_generator)
INSTANTIATE_TEST_SUITE_P(InstantiationName,TestSuiteName,param_generator,name_generator)
配合TEST_P一起使用的,用来初始化TestSuiteName。
第一个参数InstantiationName 名字是唯一的,用来区分不同的实例,在输出信息中,这个参数会作为前缀添加到test suite的名字TestSuiteName之前
第三个参数param_generator是gtest在命名空间::testing 中提供的一个函数,这个函数用来生成测试参数,有以下
Parameter Generator | Behavior |
---|---|
Range(begin, end [, step]) | Yields values {begin, begin+step, begin+step+step, …}. The values do not include end. step defaults to 1. |
Values(v1, v2, …, vN) | Yields values {v1, v2, …, vN}. |
ValuesIn(container) or ValuesIn(begin,end) | Yields values from a C-style array, an STL-style container, or an iterator range [begin, end). |
Bool() | Yields sequence {false, true}. |
Combine(g1, g2, …, gN) | Yields as std::tuple n-tuples all combinations (Cartesian product) of the values generated by the given n generators g1, g2, …, gN. |
ConvertGenerator(g) | Yields values generated by generator g, static_cast to T. |
最后一个可选的参数只用来生成一个后缀名吗? |
TYPED_TEST
TYPED_TEST(TestSuiteName, TestName) {
... statements ...
}
123
类型测试,TestSuiteName必须要在TYPED_TEST_SUITE中定义。
TypeParam关键字指模板类型参数的类型
TYPED_TEST_SUITE
TYPED_TEST_SUITE(TestFixtureName,Types)
配合TYPED_TEST一起使用,Types是一系列要测试的类型。类型别名(using or typedef)对TYPED_TEST_SUITE 宏来说是有必要的。
TYPED_TEST_P
TYPED_TEST_SUITE_P
REGISTER_TYPED_TEST_SUITE_P
INSTANTIATE_TYPED_TEST_SUITE_P
TYPED_TEST_P(TestSuiteName, TestName) {
... statements ...
}
123
FRIEND_TEST
语法:FRIEND_TEST(TestSuiteName,TestName)
在一个class内部,使用FRIEND_TEST声明一个test作为友元类,这样可以访问类的私有成员。
如果要被测试的类定义在某个namespace中,为了能够访问,fixture类和tests也必须定义在同样的namespace中(匿名namespace不行)。
namespace my_namespace {
class MyClass {
friend class MyClassTest;
FRIEND_TEST(MyClassTest, HasPropertyA);
FRIEND_TEST(MyClassTest, HasPropertyB);
... definition of class MyClass ...
};
}
12345678910
test code如下:
namespace my_namespace {
class MyClassTest : public ::testing::Test {
...
};
TEST_F(MyClassTest, HasPropertyA) { ... }
TEST_F(MyClassTest, HasPropertyB) { ... }
} // namespace my_namespace
12345678910
SCOPED_TRACE
GTEST_SKIP
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST
类和类型
::testing::AssertionResult
::testing::AssertionException
::testing::EmptyTestEventListener
::testing::Environment
protected成员:
virtual void Environment::SetUp()virtual void Environment::TearDown()
::testing::ScopedTrace
::testing::Test
抽象类,所有的测试类都从这个类继承,不可拷贝
public成员:
static void Test::SetUpTestSuite()static void Test::TearDownTestSuite()static bool Test::HasFatalFailure()static bool Test::HasNonfatalFailure()static bool Test::HasFailure()static bool Test::IsSkipped()static void Test::RecordProperty(const std::string& key, const std::string& value)static void Test::RecordProperty(const std::string& key, int value)
protected成员:virtual void Test::SetUp()virtual void Test::TearDown()
::testing::TestWithParam
从Test和WithParamInterface继承的类
TestSuite
表示一个test suite,不可拷贝
public成员:
const char* TestSuite::name() const 获取test suite的名字const char* TestSuite::type_param() const 返回类型参数名bool TestSuite::should_run() constint TestSuite::successful_test_count() constint TestSuite::skipped_test_count() constint TestSuite::failed_test_count() constint TestSuite::reportable_disabled_test_count() constint TestSuite::disabled_test_count() constint TestSuite::reportable_test_count() constint TestSuite::test_to_run_count() constint TestSuite::total_test_count() constbool TestSuite::Passed() constbool TestSuite::Failed() constTimeInMillis TestSuite::elapsed_time() constTimeInMillis TestSuite::start_timestamp() constconst TestInfo* TestSuite::GetTestInfo(int i) constconst TestResult& TestSuite::ad_hoc_test_result() const
::testing::TestInfo
存储一个test的信息
public成员:
const char* TestInfo::test_suite_name() constconst char* TestInfo::name() constconst char* TestInfo::type_param() constconst char* TestInfo::value_param() constconst char* TestInfo::file() constint TestInfo::line() constbool TestInfo::is_in_another_shard() constbool TestInfo::should_run() constbool TestInfo::is_reportable() constconst TestResult* TestInfo::result() const
::testing::TestParamInfo
描述value-parameterized test的参数,包含两个字段:param和index,参数值和它的整数序号
::testing::UnitTest
这个类包含了测试程序的信息,是一个单例,只能由UnitTest::GetInstance()在第一次调用时创建,这个实例永远不会被删除;是不可拷贝的。
public成员:
static UnitTest* UnitTest::GetInstance()const char* UnitTest::original_working_dir() constconst TestSuite* UnitTest::current_test_suite() constconst TestInfo* UnitTest::current_test_info() constint UnitTest::random_seed() constint UnitTest::successful_test_suite_count() constint UnitTest::failed_test_suite_count() constint UnitTest::total_test_suite_count() constint UnitTest::test_suite_to_run_count() constint UnitTest::successful_test_count() constint UnitTest::skipped_test_count() constint UnitTest::failed_test_count() constint UnitTest::reportable_disabled_test_count() constint UnitTest::disabled_test_count() constint UnitTest::reportable_test_count() constint UnitTest::total_test_count() constint UnitTest::test_to_run_count() constTimeInMillis UnitTest::start_timestamp() constTimeInMillis UnitTest::elapsed_time() constbool UnitTest::Passed() constbool UnitTest::Failed() constconst TestSuite* UnitTest::GetTestSuite(int i) constconst TestResult& UnitTest::ad_hoc_test_result() constTestEventListeners& UnitTest::listeners()
::testing::TestEventListener
追踪测试的执行
::testing::TestEventListeners
可以让用户添加监听者用来追踪事件
::testing::TestPartResult
::testing::TestProperty
::testing::TestResult
::testing::TimeInMillis
::testing::Types<T…>
::testing::WithParamInterface
函数
InitGoogleTest
函数原型:
void ::testing::InitGoogleTest(int* argc, char** argv)
void ::testing::InitGoogleTest(int* argc, wchar_t** argv)
void ::testing::InitGoogleTest()
123
AddGlobalTestEnvironment
函数原型:Environment* ::testing::AddGlobalTestEnvironment(Environment* env)
RegisterTest
函数原型:template <typename Factory> TestInfo* ::testing::RegisterTest(const char* test_suite_name, const char* test_name, const char* type_param, const char* value_param, const char* file, int line, Factory factory)
RUN_ALL_TESTS
函数原型:int RUN_ALL_TESTS()
AssertionSuccess
函数原型:AssertionResult ::testing::AssertionSuccess()
AssertionFailure
函数原型:AssertionResult ::testing::AssertionFailure()
StaticAssertTypeEq
函数原型:::testing::StaticAssertTypeEq<T1, T2>()
PrintToString
函数原型:std::string ::testing::PrintToString(x)
PrintToStringParamName
函数原型:std::string ::testing::PrintToStringParamName(TestParamInfo<T>& info)