测试代码
class GlobalEnv : public testing::Environment
{
public:
virtual void SetUp()
{
std::cout << "global SetUP" << std::endl;
}
virtual void TearDown()
{
std::cout << "global TearDown" << std::endl;
}
};
class TestCaseA : public testing::Test
{
protected:
static void SetUpTestCase() {
std::cout << "A static setup" << std::endl;
}
static void TearDownTestCase() {
std::cout << "A static teardown" << std::endl;
}
virtual void SetUp()
{
std::cout << "A setup" << std::endl;
}
virtual void TearDown()
{
std::cout << "A TearDown" << std::endl;
}
};
class TestCaseB : public testing::Test
{
protected:
static void SetUpTestCase() {
std::cout << "B static setup" << std::endl;
}
static void TearDownTestCase() {
std::cout << "B static teardown" << std::endl;
}
virtual void SetUp()
{
std::cout << "B setup" << std::endl;
}
virtual void TearDown()
{
std::cout << "B TearDown" << std::endl;
}
};
TEST_F(TestCaseA, test1) {
std::cout << "testing...\n";
}
TEST_F(TestCaseA, tes2) {
std::cout << "testing...\n";
}
TEST_F(TestCaseB, test1) {
std::cout << "testing...\n";
}
TEST_F(TestCaseB, tes2) {
std::cout << "testing...\n";
}
int main(int argc, char* argv[])
{
testing::AddGlobalTestEnvironment(new GlobalEnv);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
测试结果
[==========] Running 4 tests from 2 test cases.
[----------] Global test environment set-up.
global SetUP
[----------] 2 tests from TestCaseA
A static setup
[ RUN ] TestCaseA.test1
A setup
testing...
A TearDown
[ OK ] TestCaseA.test1 (0 ms)
[ RUN ] TestCaseA.tes2
A setup
testing...
A TearDown
[ OK ] TestCaseA.tes2 (0 ms)
A static teardown
[----------] 2 tests from TestCaseA (1 ms total)
[----------] 2 tests from TestCaseB
B static setup
[ RUN ] TestCaseB.test1
B setup
testing...
B TearDown
[ OK ] TestCaseB.test1 (1 ms)
[ RUN ] TestCaseB.tes2
B setup
testing...
B TearDown
[ OK ] TestCaseB.tes2 (0 ms)
B static teardown
[----------] 2 tests from TestCaseB (1 ms total)
[----------] Global test environment tear-down
global TearDown
[==========] 4 tests from 2 test cases ran. (7 ms total)
[ PASSED ] 4 tests.
结论
global Environment setup [member functinon of GlobalEnv]
||
|| SetUpTestCase [static functionclass of TestCaseA]
|| ||
|| || SetUp [member functinon of TestCaseA]
|| || || TestCaseA.test1
|| || TearDown [member functinon of TestCaseA]
|| ||
|| || SetUp [member functinon of TestCaseA]
|| || || TestCaseA.test2
|| || TearDown [member functinon of TestCaseA]
|| ||
|| TearDownTestCase [static function of TestCaseA]
||
|| SetUpTestCase [static functionclass of TestCaseB]
|| ||
|| || SetUp [member functinon of TestCaseB]
|| || || TestCaseB.test1
|| || TearDown [member functinon of TestCaseB]
|| ||
|| || SetUp [member functinon of TestCaseB]
|| || || TestCaseB.test2
|| || TearDown [member functinon of TestCaseB]
|| ||
|| TearDownTestCase [static function of TestCaseB]
||
global Environment teardown [member functinon of GlobalEnv]
注意事项
- 需要使用
TEST_F宏定义每一个test,这是测试的最小单元 - 纠正网络文章的错误:testcase(在test的头文件中) 有时候也叫 testsuit(在google官方文档中) 这里使用testcase,所以三种事件的执行顺序
- 所有testcase执行前后。
- 在某个testcase中第一个test前,最后一个test执行后。
- 每个test前后。
TEST TEST_F TEST_P 区别
-
对于TEST_F() 与 TEST()
- 每个TEST_F()或者TEST()就是一个test
- 第一个参数就是testcase的名字,test的名字就是第一个参数+第二个参数
区别就是TEST_F多了一个夹具fixture TEST_F的第一个参数需要时类的名字
-
TEST_P需要实例化,最终的test数量=TEST_P数量*参数数量
TEST_P 相当于可以传参数的TEST_F > INSTANTIATE_TEST_CASE_P(prefix,testcasename,values) > prefix 不清楚有啥用 > ::testing::TestWithParam<>已经继承TEST,也就是TEST_P有TEST_F的功能
[--------] 标记testcase
[--------]
[RUN ] 标记一个test
[ OK ]
TEST_F与TEST()都是一个test实例,而TEST_P需要传入相应的参数才能实例化。 gtest最基本的单元是单个test,其名字=testcasename +"."+ testname.