CMake GoogleTest工程

CMake GoogleTest工程

编写被测试函数

class Calc
{
public:
        Calc(){}
        ~Calc(){}

        template<typename T>
        T test_fun(T a, T b)
        {
                return a+b;
        }

};

编写测试用例

#include<gtest/gtest.h>
#include<iostream>
#include<string>
#include"calc.hpp"

Calc calcObj;

TEST(FunTest,HandlesZeroInput){
        EXPECT_EQ(5,calcObj.test_fun(2,3));
        EXPECT_EQ(0,calcObj.test_fun(-5,5));
        EXPECT_EQ(2.1,calcObj.test_fun(1.5,0.6));
        EXPECT_EQ(std::string("I have a dream"),calcObj.test_fun(std::string("I have a "),std::string("dream")));
}

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

编写makefile

cmake_minimum_required(VERSION 2.8.0)
project(Test)
add_executable(Test test.cpp)
target_link_libraries(Test /usr/local/lib/libgtest.a -lpthread)

发布了2 篇原创文章 · 获赞 0 · 访问量 14

猜你喜欢

转载自blog.csdn.net/tangtinghao/article/details/104954328