gtest学习

一.用 TEST() 宏声明测试函数

 

TEST(name1, name2)

{

EXPECT_EQ(value1, value2);

}

name1:测试用例名称  类名  文件名
name2:测试名称   方法名   函数名

二.初始化 googletest 并运行所有测试

int main(intargc,char** argv)

{

::testing::InitGoogleTest(&argc, argv);

returnRUN_ALL_TESTS();

}

InitGoogleTest会初始化一些环境变量,RUN_ALL_TESTS()会调用所有的TEST(name1, name2)

 

编译时链接-lgtest -lpthread

 

 ASSERT_* 版本的断言失败时会产生致命失败,并结束当前函数; 

EXPECT_* 版本的断言失败时产生非致命失败,但不会中止当前函数。

 



 


例子

[cpp]  view plain  copy
  1. // Configure.h   
  2.  #pragma once   
  3.   
  4.  #include <string>   
  5.  #include <vector>   
  6.   
  7.  class Configure   
  8.  {   
  9.  private:   
  10.     std::vector<std::string> vItems;   
  11.   
  12.  public:   
  13.     int addItem(std::string str);   
  14.   
  15.     std::string getItem(int index);   
  16.   
  17.     int getSize();   
  18.  };   

[cpp]  view plain  copy
  1. // Configure.cpp   
  2.  #include "Configure.h"   
  3.   
  4.  #include <algorithm>   
  5.   
  6.  /**  
  7.  * @brief Add an item to configuration store. Duplicate item will be ignored  
  8.  * @param str item to be stored  
  9.  * @return the index of added configuration item  
  10.  */   
  11.  int Configure::addItem(std::string str)   
  12.  {   
  13. std::vector<std::string>::const_iterator vi=std::find(vItems.begin(), vItems.end(), str);   
  14.     if (vi != vItems.end())   
  15.         return vi - vItems.begin();   
  16.   
  17.     vItems.push_back(str);   
  18.     return vItems.size() - 1;   
  19.  }   
  20.   
  21.  /**  
  22.  * @brief Return the configure item at specified index.  
  23.  * If the index is out of range, "" will be returned  
  24.  * @param index the index of item  
  25.  * @return the item at specified index  
  26.  */   
  27.  std::string Configure::getItem(int index)   
  28.  {   
  29.     if (index >= vItems.size())   
  30.         return "";   
  31.     else   
  32.         return vItems.at(index);   
  33.  }   
  34.   
  35.  /// Retrieve the information about how many configuration items we have had   
  36.  int Configure::getSize()   
  37.  {   
  38.     return vItems.size();   
  39.  }   


[cpp]  view plain  copy
  1. // ConfigureTest.cpp   
  2.  #include <gtest/gtest.h>   
  3.   
  4.  #include "Configure.h"   
  5.   
  6.  TEST(ConfigureTest, addItem)   
  7.  {   
  8.     // do some initialization   
  9.     Configure* pc = new Configure();   
  10.       
  11.     // validate the pointer is not null   
  12.     ASSERT_TRUE(pc != NULL);   
  13.   
  14.     // call the method we want to test   
  15.     pc->addItem("A");   
  16.     pc->addItem("B");   
  17.     pc->addItem("A");   
  18.   
  19.     // validate the result after operation   
  20.     EXPECT_EQ(pc->getSize(), 2);   
  21.     EXPECT_STREQ(pc->getItem(0).c_str(), "A");   
  22.     EXPECT_STREQ(pc->getItem(1).c_str(), "B");   
  23.     EXPECT_STREQ(pc->getItem(10).c_str(), "");   
  24.   
  25.     delete pc;   
  26.  }  

[cpp]  view plain  copy
  1. #include <gtest/gtest.h>   
  2.   
  3.  int main(int argc, char** argv) {   
  4.     testing::InitGoogleTest(&argc, argv);   
  5.   
  6.     // Runs all tests using Google Test.   
  7.     return RUN_ALL_TESTS();   
  8.  }  

g++ -c configure.cpp -lgtest -lpthread -L /usr/local/webserver/gtest/lib -I /usr/local/webserver/gtest/include 

g++ -c configuretest.cpp -lgtest -lpthread -L /usr/local/webserver/gtest/lib -I /usr/local/webserver/gtest/include 

g++ -c main.cpp -lgtest -lpthread -L /usr/local/webserver/gtest/lib -I /usr/local/webserver/gtest/include 

 g++ -o app main.o  configure.o  -lgtest -lpthread -L /usr/local/webserver/gtest/lib -I /usr/local/webserver/gtest/include 

 

猜你喜欢

转载自blog.csdn.net/wangjingqi930330/article/details/80509857