google test的使用

来源:互联网 发布:网络许可证怎么办理 编辑:程序博客网 时间:2024/06/10 20:08

 本人终于将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,具体的可以在我的下载中找到。
原创粉丝点击