Mac下GTest的基本使用

时间:2018-09-10 01:00:46   收藏:0   阅读:2750

Mac下GTest的基本使用

gtest全称Google C++ Testing Framework,它是从谷歌内部诞生并受到业界追捧的一个非常优秀的测试框架,支持如自动发现测试、自定义断言、死亡测试、自动报告等诸多功能。

本文记录Mac下安装配置gtest以及它的基本应用

一、安装

博主尝试用brew install 遇到了问题,于是直接源码安装

编译安装

 git clone https://github.com/google/googletest
 cd googletest
 mkdir build && cd build
 cmake ..
 make
 make install

测试代码

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 11)

project(demo)

find_package(GTEST REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES})

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));
    EXPECT_EQ(12, add(2, 6));
}

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

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

编译运行

cmake .
make
./demo
# 输出
?  build ./demo
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from test
[ RUN      ] test.c1
/Users/fanghao/Desktop/test/main.cpp:15: Failure
Expected equality of these values:
  12
  add(2, 6)
    Which is: 8
[  FAILED  ] test.c1 (0 ms)
[ RUN      ] test.c2
[       OK ] test.c2 (0 ms)
[----------] 2 tests from test (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] test.c1

 1 FAILED TEST

二、基本概念

三、 Test Fixture

Test Fixture是对基本test case的封装,如果多个test需要操作类似数据,可以把他们封装在一个类里面,然后共享一些配置。

编写Fixure步骤

  1. 建立一个类,并继承::testing::Test,并且使用protected或public限制符,以便其子类可以访问到共享的数据
  2. 在这个类中,声明你想复用的对象。
  3. 写一个SetUp函数来准备所需对象
  4. 写一个TearDown函数来释放资源
  5. 其他用法和基本的test case一样,只是用TEST_F()宏代替TEST()宏,每次调用一个TEST_F都会重新运行一次SetUp和TearDown,但但成员对象可以共享

看一个示例应该更能说明问题

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

class Foo
{
  public:
    int add(int a, int b)
    {
        return a + b;
    }
    int sub(int a, int b)
    {
        return a - b;
    }
};

class FooTest : public ::testing::Test
{
  protected:
    Foo foo; // shared object
    // SetUp && TearDown will be called for every Test Case
    virtual void SetUp()
    {
        std::cout << "Code here will be called immediately after the constructor" << std::endl;
    }
    virtual void TearDown()
    {
        std::cout << "Code here will be called immediately after each test" << std::endl;
    }
};

// Test Case 1
TEST_F(FooTest, TestMethodAdd)
{
    EXPECT_EQ(3, foo.add(1, 2));
}

// Test Case 2
TEST_F(FooTest, TestMethodSub)
{
    EXPECT_EQ(-1, foo.sub(1, 2));
}

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

# 输出
?  build ./demo
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from FooTest
[ RUN      ] FooTest.TestMethodAdd
Code here will be called immediately after the constructor
Code here will be called immediately after each test
[       OK ] FooTest.TestMethodAdd (0 ms)
[ RUN      ] FooTest.TestMethodSub
Code here will be called immediately after the constructor
Code here will be called immediately after each test
[       OK ] FooTest.TestMethodSub (0 ms)
[----------] 2 tests from FooTest (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 2 tests.

四、小结

感觉gtest还是挺优雅的,适合TDD模式,把测试和开发完全解耦。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!