GoogleTest单元测试使用2

简介

单元测试(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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Google Test单元测试,你可以在C++项目中使用Google Test框架来编写和运行单元测试。 首先,你需要在项目中包含Google Test库。你可以从Google Test的官方GitHub仓库下载最新版本的源代码,并将其添加到你的项目中。 在编写单元测试之前,你需要创建一个测试文件。这个文件应该包含一个或多个测试用例,每个测试用例都是一个函数。你可以使用Google Test提供的宏来定义和运行测试用例。 一个简单的示例代码如下: ```c++ #include <gtest/gtest.h> // 定义一个测试用例 TEST(ExampleTest, Addition) { int a = 2; int b = 3; int result = a + b; EXPECT_EQ(result, 5); } // 定义另一个测试用例 TEST(ExampleTest, Subtraction) { int a = 5; int b = 3; int result = a - b; EXPECT_EQ(result, 2); } int main(int argc, char** argv) { // 初始化 Google Test 框架 ::testing::InitGoogleTest(&argc, argv); // 运行所有的测试用例 return RUN_ALL_TESTS(); } ``` 在这个例子中,我们定义了两个测试用例:Addition和Subtraction。每个测试用例都包含一些断言,用于验证预期结果和实际结果是否相等。在main函数中,我们初始化Google Test框架并运行所有的测试用例。 要编译和运行这个测试文件,你需要将Google Test库链接到你的项目中。具体的编译和链接过程可能因你使用开发环境而有所不同。 当你运行这个测试文件时,Google Test将会执行所有的测试用例,并输出测试结果。如果所有的断言都通过了,测试将会被标记为通过;否则,测试将会被标记为失败,并显示详细的错误信息。 这只是一个简单的示例,Google Test还提供了很多其他的功能和特性,例如测试夹具、参数化测试、测试报告等。你可以查阅Google Test的官方文档以获取更多信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值