-
作用
作为代码编码人员,写完代码,不仅要保证编译通过和运行,还要保证逻辑尽量正确。单元测试是对软件可测试最小单元的检查和校验。单元测试与其他测试不同,单元测试可看作是编码工作的一部分,应该由程序员完成,也就是说,经过了单元测试的代码才是已完成的代码,提交产品代码时也要同时提交测试代码。测试部门可以作一定程度的审核。(来自百度百科)
-
其作用可以归为四种:
验证:验证程序逻辑的正确性,即使后期升级,通过跑单元测试,也可以看到升级后是否会对旧逻辑有影响
- 设计:促使程序员写出可单独测试的代码,从而更容易解耦
- 文档:作为程序使用的sample
- 回归:应对程序升级,也可以集成到code review之前的编译,自动做回归,典型用法就是在DevOps的编译打包阶段,例如jeckins编译完成后,自动触发单元测试。
原理
- gtest主要由一系列的宏和事件实现。宏:有TEST和TEST_F宏,TEST宏针对简单的测试用例,TEST_F宏针对需要做初始化和资源回收的测试用例,有点像类似C++的构造函数和析构函数,两个宏都是把参数展开后拼成一个类。
#include <iostream>#include <gtest/gtest.h>using namespace std;class MyEnvironment0 : public testing::Environment {public:virtual void SetUp() {cout << \"Global event0 : start1\" << endl;}virtual void TearDown() {cout << \"Global event0 : end\" << endl;}};class MyEnvironment1 : public testing::Environment {public:virtual void SetUp() {cout << \"Global event1 : start\" << endl;}virtual void TearDown() {cout << \"Global event1 : end\" << endl;}};TEST(GlobalTest0, test0) {EXPECT_EQ(1, 1);};TEST(GlobalTest0, test1) {EXPECT_EQ(1, 1);};TEST(GlobalTest1, test0) {EXPECT_EQ(1, 1);};int main(int argc, char *argv[]) {testing::AddGlobalTestEnvironment(new MyEnvironment0);testing::AddGlobalTestEnvironment(new MyEnvironment1);testing::InitGoogleTest(&argc, argv);return RUN_ALL_TESTS();}
View Code
- 代码:
#include <iostream>#include <gtest/gtest.h>using namespace std;class MyTestSuite0 : public testing::Test{protected:// 网上大部分写的是SetUpTestSuite,google后面升级版本了,改成SetUpTestCase// 相关讨论:https://stackoverflow.com/questions/54468799/google-test-using-setuptestsuite-doesnt-seem-to-workstatic void SetUpTestCase(){cout << \"TestSuite event0 : start\" << endl;}static void TearDownTestCase(){cout << \"TestSuite event0 : end\" << endl;}};class MyTestSuite1 : public testing::Test{protected:static void SetUpTestCase(){cout << \"TestSuite event1 : start\" << endl;}static void TearDownTestCase(){cout << \"TestSuite event1 : end\" << endl;}};// 必须用TEST实现,实现上是拼成一个类MyTestSuite0_test0TEST_F(MyTestSuite0, test0){EXPECT_EQ(1, 1);}TEST_F(MyTestSuite1, test0){EXPECT_EQ(1, 1);}TEST_F(MyTestSuite0, test1){EXPECT_EQ(1, 1);}TEST_F(MyTestSuite1, test1){EXPECT_EQ(1, 1);}//int main(int argc, char *argv[])//{// testing::InitGoogleTEST_F(&argc, argv);//// return RUN_ALL_TESTS();//}
View Code
- 代码:
#include <iostream>#include <gtest/gtest.h>using namespace std;class MyTestCase0 : public testing::Test{protected:virtual void SetUp(){cout << \"TestCase event0 : start\" << endl;}virtual void TearDown(){cout << \"TestCase event0 : end\" << endl;}};class MyTestCase1 : public testing::Test{protected:virtual void SetUp(){cout << \"TestCase event1 : start\" << endl;}virtual void TearDown(){cout << \"TestCase event1 : end\" << endl;}};TEST_F(MyTestCase0, test0){EXPECT_EQ(1, 1);}TEST_F(MyTestCase0, test1){EXPECT_EQ(1, 1);}TEST_F(MyTestCase1, test0){EXPECT_EQ(1, 1);}TEST_F(MyTestCase1, test1){EXPECT_EQ(1, 1);}int main(int argc, char *argv[]){testing::InitGoogleTest(&argc, argv);return RUN_ALL_TESTS();}
View Code
代码
- 上传到个人github中:https://github.com/longbozhan/sample/tree/master/gtest
参考
- https://www.shuzhiduo.com/A/n2d9gnDgJD/