美文网首页
Gtest 快速上手

Gtest 快速上手

作者: 臻甄 | 来源:发表于2020-03-24 22:11 被阅读0次

Overview

  • GTest:Google的开源C++单元测试框架Google Test

Install

Ubuntu下安装

Step 1. 确保安装了cmake

$ cmake --version # 如果没有安装就用 sudo apt-get install cmake 

Step 2. apt安装源码并编译

$ sudo apt-get install libgtest-dev # apt安装源码
$ cd /usr/src/gtest  # 进入源码存放的地方,里面的文件很简单(使用ls查看):cmake, src, CMakeLists.txt
$ sudo mkdir build # 用于存放编译产物
$ cd build
$ sudo cmake ..  # 编译
$ sudo make 

Step 3. 将编译生成好的库拷贝到系统目录下

将生成的libgtest.a 和 libgtest_main.a 拷贝到系统的lib路径下.

sudo cp libgtest*.a /usr/local/lib

OK,安装完成。

源码安装

$ git clone https://github.com/google/googletest  # 下载源码
$ cd build && cmake .. && make -j 2  # 用cmake生成Makefile后编译
$ sudo make install  # 安装

只要在/usr/include路径下找到gtest.h就说明安装成功了。


QuickStart

demo1:G++例子

main.cpp

#include <iostream>
#include <gtest/gtest.h>

int add(int a, int b) {
    return a + b;
}

int sub(int a, int b) {
    return a - b;
}

// case1
TEST(test, c1) {
    EXPECT_EQ(3, add(1, 2));  // pass
    EXPECT_EQ(12, add(2, 6));  // fail
}

// case2
TEST(test, c2) {
    EXPECT_EQ(-1, sub(1, 2));  // pass
}

GTEST_API_ int main(int argc, char ** argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

里面工包含3个断言,其中2个是正确的,1个是错误的,编译一下看下效果(注意要链接gtest的lib库)。

$ g++ main.cpp /usr/local/lib/libgtest.a -lpthread -o demo
$ ./demo

提示非常简明易懂

gtest

demo2: CMake例子

在demo1的main.cpp的基础上,增加一个CMakefile.txt文档

cmake_minimum_required(VERSION 2.6) # 需要的最低版本

project(demo)

find_package(GTest REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} gtest pthread)

编译工程并运行

$ mkdir build && cd build
$ cmake ..
$ make
$ ./demo
看效果 image.png

demo3:

根据官网的一个例子改编
文件如下:

$ tree
.
├── CMakeLists.txt
├── main.cpp
└── src
    └── footest.cpp

main.cpp

#include "gtest/gtest.h"

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

footest.cpp

#include "gtest/gtest.h"

// The fixture for testing class Foo.
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.
    }

    // 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 {
         // Code here will be called immediately after the constructor (right
         // before each test).
    }

    void TearDown() override {
         // 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.
};

// Tests that the Foo::Bar() method does Abc.
TEST_F(FooTest, MethodFooBarTest) {
    EXPECT_TRUE(3 > 0);
}

// Tests that Foo does Xyz.
TEST_F(FooTest, DoesXyz) {
    // Exercises the Xyz feature of Foo.
    EXPECT_EQ(1, 1);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

project(demo)

find_package(GTest REQUIRED)

file(GLOB src "src/*.cpp")
add_executable(${PROJECT_NAME} main.cpp ${src})

target_link_libraries(${PROJECT_NAME} gtest pthread)

编译工程并运行

$ mkdir build && cd build
$ cmake ..
$ make
$ ./demo
看效果

基本使用方式

1. 断言

  • 断言即assertion,检查条件是否为真,一个断言的结果有
    (1)sucess
    (2)nonfatal failure(非致命失败)
    (3)fatal failure(致命失败)
  • 两类断言ASSERT_*系列和EXPECT_*系列:
    (1)ASSERT 失败之后就不会运行后续的测试了;
    (2) EXPECT 虽然失败,但是不影响后续测试的进行。
  • 常见断言

    (1)布尔值检查
(2)数值型数据检查 (3)字符串检查 (4)异常检查 image.png

其他用法请参考文档:

相关文章

网友评论

      本文标题:Gtest 快速上手

      本文链接:https://www.haomeiwen.com/subject/nuaoyhtx.html