gtest使用教程
1 简介
之前对gtest一无所知,最近,找了些相关的资料,学习了下.这里主要记录了学习过程和相关知识点.
什么是gtest: gtest测试框架是在不同平台上(Linux,Mac OS X,Windows,Cygwin,Windows CE和Symbian)为编写C++测试而生成的。它是基于xUnit架构的测试框架,支持自动发现测试,丰富的断言集,用户定义的断言.
2 准备工作
无需linux虚拟机,Windows就可以,无需安装任何专业软件,但是要求可以上网.
是的,我们将直接在网页上进行在线编码,编译,运行.还不知道如何在线编译调试代码的,见上一篇文章
3 获取gtest源码
https://github.com/google/googletest.git
我是用git小乌龟下载的.
也可以用其他方式,目的就是获取源码:
4 创建工程
进入https://replit.com/
创建一个c++空白工程.
我的工程名叫test.
然后导入源码.
选择本地的gtest源码目录.
成功后:
5 编译gtest
- 1 进入googletest,创建build文件夹(实际拼写出了点问题)
在shell下:
cd googletest/bulid/
cmake ..
make
成功后得到静态库文件:
将libgtest.a libgtest_main.a拷贝到main.cpp同级目录下(main.cpp自己创建)
cp googletest/bulid/lib/libgtest*.a .
6 测试
先搞个例子跑起来.
main.cpp
/* main.cpp
* Created by 一条晓鱼 on 2022/12/15.
*/
#include "gtest/gtest.h"
#include <iostream>
#include <string>
int add(int a, int b)
{
return a + b;
}
TEST(fun, add_a)
{
EXPECT_EQ(-3, add(-2,-1));
EXPECT_EQ(-2, add(1,-3));
}
int main(int argc, char **argv){
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
return 0;
}
g++ main.cpp libgtest.a -lpthread -std=c++14 -I /home/runner/test/googletest/googletest/include -o m
./m
(由于gtest版本问题,12.x 必须c++14以上才能编译通过,刚开始编译用的11标准,一直报错)
(有用的就是静态库和头文件,将来移植的时候就是需要这两个东西)
这样就算可以用啦.后面具体讲一下,gtest怎么用.
7 gtest 怎么用
-
断言
分ASSERT_XXX 和 EXPECT_XXX两类.
区别:
如果ASSERT_XXX 测试结果不通过,后面的测试就不会执行了.
如果EXPECT_XXX 测试结果不通过,后面的测试会接着执行.
布尔断言:单参断言
ASSERT_TRUE、ASSERT_FALSE、EXPECT_TRUE、EXPECT_FALSE
数值断言:双参
ASSERT_EQ、ASSERT_NE、ASSERT_LT、ASSERT_LE、ASSERT_GT、ASSERT_GE
EXPECT_EQ、EXPECT_NE、EXPECT_LT、EXPECT_LE、EXPECT_GT、EXPECT_GE
字符串断言
ASSERT_STREQ、ASSERT_STRCASEEQ -
TEST(test_suite_name,test_name)
一个TEST()算是一个测试case.
TEST(x,y)展开为x_y_TEST()这样的函数
比如测试add()函数,我们可以考虑多种测试情况:和为负数,和为正数,极限值测试.
/*
* Created by 一条晓鱼 on 2022/12/15.
*/
#include "gtest/gtest.h"
#include <iostream>
#include <string>
int add(int a, int b)
{
return a + b;
}
TEST(add, negative)
{
EXPECT_EQ(-3, add(-2,-1));
EXPECT_EQ(-2, add(1,-3));
}
TEST(add, positive)
{
EXPECT_EQ(1, add(2,-1));
EXPECT_EQ(2, add(-1,3));
}
TEST(add, limit)
{
int a = 0x7fffffff + 1;
std::cout<<"a = "<<a<<"\n";
EXPECT_EQ(a, add(0x7fffffff,1));
EXPECT_EQ(0, add(0xffffffff,1));
}
int main(int argc, char **argv){
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
return 0;
}
- TEST_F(x,y)
以下函数使用需要定义测试套类继承::testing::Test,重新实现对应函数.
SetUpTestSuite:static测试套级别,运行测试套第一个用例前执
TearDownTestSuite: static 测试套级别,运行测试套最后一个用例后执行
SetUp:virtual 测试套中每个测试用例开始时执行
TearDown:virtrual 测试套中每个测试用例运行后执行
x 为 class名.
/*
* Created by 一条晓鱼 on 2022/12/15.
*/
#include "gtest/gtest.h"
#include <iostream>
#include <string>
int add(int a, int b)
{
return a + b;
}
TEST(add, negative)
{
EXPECT_EQ(-3, add(-2,-1));
EXPECT_EQ(-2, add(1,-3));
}
TEST(add, positive)
{
EXPECT_EQ(1, add(2,-1));
EXPECT_EQ(2, add(-1,3));
}
TEST(add, limit)
{
int a = 0x7fffffff + 1;
std::cout<<"a = "<<a<<"\n";
EXPECT_EQ(a, add(0x7fffffff,1));
EXPECT_EQ(0, add(0xffffffff,1));
}
class FooTest : public ::testing::Test {
protected:
// You can remove any or all of the following functions if their bodies would
// be empty.
FooTest() {
// You can do set-up work for each test here.
}
~FooTest() override {
// You can do clean-up work that doesn't throw exceptions here.
}
static void SetUpTestSuite() {
std::cout<<"===================run before first case..."<<std::endl;
}
static void TearDownTestSuite() {
std::cout<<"===================run after last case..."<<std::endl;
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
void SetUp() override {
std::cout<<" =========================SetUp() \n";
// Code here will be called immediately after the constructor (right
// before each test).
}
void TearDown() override {
std::cout<<" =========================TearDown() \n";
// Code here will be called immediately after each test (right
// before the destructor).
}
// Class members declared here can be used by all tests in the test suite
// for Foo.
};
TEST_F(FooTest,test_a)
{
EXPECT_EQ(2, add(0x7fffffff,1));
}
TEST_F(FooTest,test_b)
{
EXPECT_EQ(1, add(0,1));
}
int main(int argc, char **argv){
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
return 0;
}
上面两个是常用的.基本上就是初级教程里面的内容,高级教程后面有链接.