简介
单元测试(Unit Test)是对软件基本组成单元(如函数或是一个类的方法)进行的测试,是开发者编写的一小段代码,用于检验被测试代码一个很小的、很明确的功能是否正确。
在gtest中,一个测试用例(test case)可以包含一个或多个测试。一个测试程序可以包含多个测试用例。
一个较完整的 googletest 单元测试实例
Configure.h
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <gtest/gtest.h>
class Configure
{
private:
std::vector<std::string> vItems;
public:
int addItem(std::string str);
std::string getItem(int index);
int getSize();
};
Configure.cpp
#include <algorithm>
#include "configure.h"
/**
* @brief Add an item to configuration store. Duplicate item will be ignored
* @param str item to be stored
* @return the index of added configuration item
*/
int Configure::addItem(std::string str)
{
std::vector<std::string>::const_iterator vi=std::find(vItems.begin(), vItems.end(), str);
if (vi != vItems.end())
return vi - vItems.begin();
vItems.push_back(str);
return vItems.size() - 1;
}
/**
* @brief Return the configure item at specified index.
* If the index is out of range, "" will be returned
* @param index the index of item
* @return the item at specified index
*/
std::string Configure::getItem(int index)
{
if (index >= vItems.size())
return "";
else
return vItems.at(index);
}
/// Retrieve the information about how many configuration items we have had
int Configure::getSize()
{
return vItems.size();
}
main.cc
#include <gtest/gtest.h>
#include "configure.h"
TEST(ConfigureTest, addItem)
{
// do some initialization
Configure* pc = new Configure();
// validate the pointer is not null
ASSERT_TRUE(pc != NULL);
// call the method we want to test
pc->addItem("A");
pc->addItem("B");
pc->addItem("A");
// validate the result after operation
EXPECT_EQ(pc->getSize(), 2);
EXPECT_STREQ(pc->getItem(0).c_str(), "A");
EXPECT_STREQ(pc->getItem(1).c_str(), "B");
EXPECT_STREQ(pc->getItem(10).c_str(), "");
delete pc;
}
int main()
{
testing::InitGoogleTest();
return RUN_ALL_TESTS();
}