google test的使用

 本人终于将google test搞定了,实在是费了不少的力气,根据readme文件的提示是./configure ------make ----make check ----make install 但是在编译自己的的test文件的是时候出错了,我用gcc进行编译是无论如何也编译不过去的,不知道是什么原因,只有将文件后缀名改为cpp,用g++才编译过去,

 

生成.o文件  g++ $(gtest-config --cppflags --cxxflags) -o code_test.o -c a_test.cpp
生成可执行文件 g++ $(gtest-config --ldflags --libs) -o main_test a.o code_test.o test_main.o

 

像文件的编写在http://jamesxud.spaces.live.com/Blog/cns!477F9C5882C309E7!425.entry介绍的很清楚了,我也不知道那个人是怎么编译过去的,如果有人知道,清告知,谢谢!

 

 

google开放了一个测试框架,可以用于测试多种平台下的C++代码。该开源项目的主页是http://code.google.com/p/googletest/ 。
下面对这个测试框架在linux平台下的安装和使用做一个简单的介绍。

1. 获得源代码进行安装
   查看readme.txt,运行aclocal,libtoolize,autoheader,automake,autoconf,make,make check,sudo make install
  
   得到gtest-config ,gtest-config用于生成编译和连接时的参数。
   没有得到gtester gtester-report。
   gtester是运行测试程序的命令,gtester- report用于将gtester声称的日志文件转换为html格式。
   下面将通过一个小程序的测试来说明这个测试工具的使用方法
 2. 需要进行测试的代码
   这里列出进行测试的代码,一共两个文件 code.h code.cpp 。
   code.h
      #ifndef CODE_H
      #define CODE_H
     
      int f( int a );
      int b( int b );
      #endif

   code.cpp
      #include "code.h"

      int f( int a ){
          return a+1;
      }

      int b( int b ){
          return b+2;
      }
3. 编写测试代码
    测试代码为code_test.cpp
     #include <limits.h>
     #include "code.h"
     #include <gtest/gtest.h>


     TEST( TEST_A,NAME ){         //TEST_A 是测试用例名称, NAME 是测试名称
         EXPECT_EQ( 2,f( 1 ) );   //这个测试执行函数f,参数为1,查看返回值是不是2
         EXPECT_EQ( 3,f( 2 ) );
     }

     TEST( TEST_B,NAME ){
        EXPECT_EQ( 10,b( 2 ) );
     }

4. 运行测试代码
   运行测试代码需要一个主函数,在这里主函数在文件 main_test.cpp 中
   main_test.cpp
   #include <gtest/gtest.h>
   #include <iostream>
   int main(int argc, char **argv) {
       // std::cout << "Running main() from gtest_main.cc/n";

   testing::InitGoogleTest(&argc, argv);
   return RUN_ALL_TESTS();
   }
5. 编译
   使用下面的命令进行编译和连接

   #gcc $(gtest-config --cppflags --cxxflags) -o code.o -c code.cpp
   #gcc $(gtest-config --cppflags --cxxflags) -o code_test.o -c code_test.cpp
   #gcc $(gtest-config --cppflags --cxxflags) -o main_test.o -c main_test.cpp
   #gcc $(gtest-config --ldflags --libs) -o main_test code.o code_test.o main_test.o
   g++ -I/root/lgtnt/gtest/gtest_1.5.0/include -pthread -o code_test.o -c code_test.cpp
   g++ -I/root/lgtnt/gtest/gtest_1.5.0/include -pthread -o code.o -c code.cpp
   g++ -I/root/lgtnt/gtest/gtest_1.5.0/include -pthread -o main_test.o -c main_test.cpp
   g++ -L/root/lgtnt/gtest/gtest_1.5.0/lib/.libs -lgtest -pthread -o main_test code.o code_test.o main_test.o

6. 测试
   使用gtester运行上面编译的程序
 $ ./main_test
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from TEST_A
[ RUN      ] TEST_A.NAME
[       OK ] TEST_A.NAME
[----------] 1 test from TEST_B
[ RUN      ] TEST_B.NAME
code_test.cpp:12: Failure
Value of: b( 2 )
  Actual: 4
Expected: 10
[  FAILED  ] TEST_B.NAME
[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran.
[  PASSED  ] 1 test.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] TEST_B.NAME
 1 FAILED TEST
 
7.TEST_F的使用
TEST_F与TEST的区别是TEST_F提供了一个初始化函数(SetUp)和一个清理函数(TearDown),在TEST_F中使用的 变量可以在初始化函数SetUp中初始化,在TearDown中销毁,并且所有的TEST_F是互相独立的,都是在初始化以后的状态开始运行,一个 TEST_F不会影响另一个TEST_F所使用的数据,下面是一个例子。

需要测试的代码:
A.h
#ifndef A_H
#define A_H

class A
{
private:
    int _a;
public:
    A( int a );
    ~A( );
    void add( int a );
    int getA( );
};
#endif

A.cpp
#include "A.h"

A::A( int a ){
    this->_a = a;
}
A::~A( ){
}

void A::add( int a ){
    this->_a += a;
}

int A::getA( ){
    return this->_a;
}

测试代码:
A_test.cpp

#include "A.h"
#include <gtest/gtest.h>

class A_test : public testing::Test {
protected:
    A* _p_a;

    virtual void SetUp( ){      //初始化函数
        this->_p_a = new A( 1 );
    }
    virtual void TearDown( ){   //清理函数
        delete this->_p_a;
    }
};

//第一个测试,参数A_test是上面的那个类,第二个参数FirstAdd是测试名称
TEST_F( A_test,FirstAdd ){        
    EXPECT_EQ( 1,_p_a->getA( ) );
    _p_a->add( 3 );
    EXPECT_EQ( 4,_p_a->getA( ) );
}

//第二个测试
TEST_F( A_test,SecondAdd ){
    EXPECT_EQ( 1,_p_a->getA( ) );
    _p_a->add( 5 );
    EXPECT_EQ( 6,_p_a->getA( ) );
}
/*
上面的两个测试都是在SetUp函数执行后的状态下执行,也就是说在执行任意一个TEST_F时 _p_a->_a 的值都是初始值1
*/

主函数(同上一个例子相同)
main.cpp
#include <gtest/gtest.h>

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

    return 0;
}
 

编译后执行的结果如下:
$ ./main
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from A_test
[ RUN      ] A_test.FirstAdd
[       OK ] A_test.FirstAdd
[ RUN      ] A_test.SecondAdd
[       OK ] A_test.SecondAdd
[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran.
[  PASSED  ] 2 tests

此代码的版本的1.5.0,具体的可以在我的下载中找到。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Google TestGoogle 推出的一个 C++ 单元测试框架,它旨在提供简单、灵活、可扩展的单元测试工具。下面是一个简单的使用 Google Test 进行单元测试的示例: 假设我们要测试的函数是一个计算阶乘的函数,代码如下: ```c++ int Factorial(int n) { return n <= 1 ? 1 : n * Factorial(n - 1); } ``` 我们可以使用 Google Test 编写一个测试用例,代码如下: ```c++ #include "gtest/gtest.h" // 定义一个测试用例 TEST(FactorialTest, Zero) { EXPECT_EQ(Factorial(0), 1); } TEST(FactorialTest, Positive) { EXPECT_EQ(Factorial(1), 1); EXPECT_EQ(Factorial(2), 2); EXPECT_EQ(Factorial(3), 6); EXPECT_EQ(Factorial(10), 3628800); } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } ``` 在上面的代码中,我们定义了两个测试用例,一个测试输入为 0 的情况,另一个测试输入为正整数的情况。在测试用例中,我们使用 `EXPECT_EQ` 宏来断言计算结果是否正确。最后,我们在 `main` 函数中使用 `testing::InitGoogleTest` 函数来初始化 Google Test 框架,并使用 `RUN_ALL_TESTS` 函数来运行所有测试用例。 注意,为了使用 Google Test,我们需要下载并安装 Google Test 库,并将其包含在项目中。另外,我们需要在编译时链接 Google Test 库,具体方法可以参考 Google Test 的文档。 以上是一个简单的使用 Google Test 进行单元测试的示例,你可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值