本文主要探讨gtest相关内容。
gtest安装
wget -O gtest.zip https://github.com/google/googletest/archive/refs/heads/main.zipunzip gtest.zipcd googletest-mainmkdir bulid && cd buildcmake .. && make && make install
gtest API
TEST/TEST_F
TEST(test_case_name,test_name)
宏创建测试
TEST_F(test_case_name,test_name)
不同TestCase用同一测试数据(test_name不同)
test_case_name测试用例名,通常为测试函数名或测试类名
test_name测试名
断言宏
ASSERT_*:检测失败退出,EXPECT_*:检测失败继续执行
ASSERT_TRUE(arg),期待结果是true
ASSERT_FALSE(arg),期待结果是false
ASSERT_EQ(arg1,arg2),比较是否==
ASSERT_NE(arg1,arg2),比较是否!=
ASSERT_LT(arg1,arg2),比较是否<
ASSERT_GT(arg1,arg2),比较是否>
ASSERT_LE(arg1,arg2),比较是否<=
ASSERT_GE(arg1,arg2),比较是否>=
ASSERT_STREQ(s1,s2),比较C字符串相等
ASSERT_STRNE(s1,s2),比较C字符串不相等
ASSERT_STRCASEEQ(s1,s2)比较C字符串相等(忽略大小写)
ASSERT_STRCASENE(s1,s2)比较C字符串不相等(忽略大小写)
事件机制
TestSuite事件(局部事件)
写类继承testing::Test,实现静态方法SetUpTestCase(在第一个TestCase前执行),TearDownTestCase(在最后一个TestCase前执行)
TestCase事件(个体事件)
每个案例前后执行,需实现SetUp(单个TestCase之前执行)和TearDown方法(单个TestCase之后执行)
全局事件
写类继承testing::Environment类,需实现SetUp(TestCase之前执行)和TearDown方法(TestCase之后执行),定义全局环境对象添加到全局环境测试中
程序退出/崩溃
ASSERT_DEATH(func,des)
程序退出0(程序正常)则补满足结果打印错误描述信息
ASSERT_EXIT(func,return_func,desc)
测函数、退出码/退出信号、错误信息regex,待测函数退出值错误码/错误信号不同退出打印错误描述信息
demo1:
断言
目录:
run.sh
cmake .make ./pro
clean.sh
rm -rf CMakeFiles CTestTestfile.cmake Makefile CMakeCache.txt cmake_install.cmake pro
clean_src.sh
rm -rf *.cc *.c *.h *.hpp *.cpp
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.20) #最低版本要求PROJECT(GooleTest) #设置工程名SET(CMAKE_CXX_COMPILER "g++-11") #设置g++编译器ENABLE_TESTING() #开启测试ADD_EXECUTABLE(pro Assert.cpp AssertUnitTest.cpp) #生成可执行文件TARGET_LINK_LIBRARIES(pro gtest_main gtest) #添加链接文件add_test(test tset) #添加测试(测试名,测试命令)
Assert.hpp
#ifndef __ASSERT_HPP__
#define __ASSERT_HPP__int AddNum(const int a,const int b);int ReduceNum(const int a,const int b);int MulNum(const int a,const int b);int DivNum(const int a,const int b);#endif //__ASSERT_HPP__
Assert.cpp
#include <iostream>
#include "Assert.hpp"int AddNum(const int a,const int b)
{return (a + b);
}int ReduceNum(const int a,const int b)
{return abs(a - b);
}int MulNum(const int a,const int b)
{return (a * b);
}int DivNum(const int a,const int b)
{return (a / b);
}
AssertUnitTest.cpp
#include <iostream>#include "gtest/gtest.h"
#include "Assert.hpp"using namespace std;namespace
{TEST(AssertTest,MathTest){ASSERT_TRUE(AddNum(1, 1));ASSERT_FALSE(ReduceNum(1, 1));ASSERT_EQ(1,MulNum(1,1));ASSERT_NE(2,MulNum(1,1));ASSERT_LT(1,AddNum(1,1));ASSERT_GT(3,AddNum(1,1));ASSERT_LE(1,AddNum(1,1));ASSERT_GE(2,AddNum(1,1));ASSERT_STREQ("hello","hello");ASSERT_STRNE("hello","word");ASSERT_STRCASEEQ("hellO","Hello");ASSERT_STRCASENE("hello","word");}} // namespace
结果示例:
demo2:
事件
run.sh
cmake .make ./pro
clean.sh
rm -rf CMakeFiles CTestTestfile.cmake Makefile CMakeCache.txt cmake_install.cmake pro
clean_src.sh
rm -rf *.cc *.c *.h *.hpp *.cpp
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.20) #最低版本要求PROJECT(GooleTest) #设置工程名SET(CMAKE_CXX_COMPILER "g++-11") #设置g++编译器ADD_EXECUTABLE(pro Event.cpp) #生成可执行文件TARGET_LINK_LIBRARIES(pro pthread gtest) #添加链接文件
Event.cpp
#include <iostream>#include "gtest/gtest.h"using namespace std;
using namespace testing;class Ev1:public Environment
{public:void SetUp(){cout << "global event1 start" << endl;}void TearDown(){cout << "global event1 end" << endl;}
};class Ev2:public Environment
{public:void SetUp(){cout << "global event2 start" << endl;}void TearDown(){cout << "global event2 end" << endl;}
};class Ev3:public Test
{protected:static void SetUpTestSuite(){cout << "local event3 start" << endl;}static void TearDownTestSuite(){cout << "loacal event3 end" << endl;}
};class Ev4:public Test
{protected:static void SetUpTestSuite(){cout << "local event4 start" << endl;}static void TearDownTestSuite(){cout << "loacal event4 end" << endl;}
};class Ev5:public Test
{protected:virtual void SetUp(){cout << "individual event5 start" << endl;}virtual void TearDown(){cout << "individual event5 end" << endl;}
};class Ev6:public Test
{protected:virtual void SetUp(){cout << "individual event6 start" << endl;}virtual void TearDown(){cout << "individual event6 end" << endl;}
};TEST_F(Ev3,EnvT0)
{ASSERT_EQ(1,1);
}TEST_F(Ev3,EnvT1)
{ASSERT_EQ(1,1);
}TEST_F(Ev4,EnvT0)
{ASSERT_EQ(1,1);
}TEST_F(Ev4,EnvT1)
{ASSERT_EQ(1,1);
}TEST_F(Ev5,EnvT0)
{ASSERT_EQ(1,1);
}TEST_F(Ev5,EnvT1)
{ASSERT_EQ(1,1);
}TEST_F(Ev6,EnvT0)
{ASSERT_EQ(1,1);
}TEST_F(Ev6,EnvT1)
{ASSERT_EQ(1,1);
}int main()
{AddGlobalTestEnvironment(new Ev1());AddGlobalTestEnvironment(new Ev2());InitGoogleTest();return RUN_ALL_TESTS();
}
结果示例:
demo3:
程序退出/崩溃
目录:
run.sh
cmake .make ./pro
clean.sh
rm -rf CMakeFiles CTestTestfile.cmake Makefile CMakeCache.txt cmake_install.cmake pro
clean_src.sh
rm -rf *.cc *.c *.h *.hpp *.cpp
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.20) #最低版本要求PROJECT(GooleTest) #设置工程名SET(CMAKE_CXX_COMPILER "g++-11") #设置g++编译器ADD_EXECUTABLE(pro Exit.cpp) #生成可执行文件TARGET_LINK_LIBRARIES(pro pthread gtest) #添加链接文件
Exit.cpp
#include <iostream>
#include <unistd.h>
#include "gtest/gtest.h"using namespace std;
using namespace testing;void DeathTest1()
{cerr << "DeathTest1 ,exit(1)" << endl;exit(1);
}void DeathTest2()
{cerr << "DeathTest2 ,exit(0)" << endl;exit(0);
}void ExitTest1()
{cerr << "ExitTest1 ,exit(1)" << endl;exit(1);
}void ExitTest2()
{cerr << "ExitTest2 ,exit(0)" << endl;exit(0);
}TEST(ExitTest,ExitT)
{ASSERT_EXIT(ExitTest1(),ExitedWithCode(1),".*");ASSERT_EXIT(ExitTest2(),ExitedWithCode(0),".*");
}TEST(DeathTest,DeathT)
{ASSERT_DEATH(DeathTest1(),".*");//ASSERT_DEATH(DeathTest2(),".*");
}int main()
{InitGoogleTest();return RUN_ALL_TESTS();
}
结果示例: