GoogleTest单元测试框架总结之类测试和Test Fixture

发表于:2021-10-25 09:35

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Ron    来源:51Testing软件测试网原创

  类的测试
  我们先从一个简单的计数器开始。

  示例一
  头文件Counter.h仅定义功能,不进行具体实现:增加计数 Increment()、减少计数Decrement()、打印。
class Counter  
{  
private:  
    int counter_;  
  
public:  
    // Creates a counter that starts at 0.  
    Counter() : counter_(0) {}   
  
    // Returns the current counter value, and increments it.  
    int Increment(); 
  
    // Returns the current counter value, and decrements it.  
    int Decrement();  
  
    // Prints the current counter value to STDOUT.  
    void Print() const;  
}; 

  配套的cpp文件,负责成员函数的具体实现。
#include "Counter.h"   
// Returns the current counter value, and increments it.  
int Counter::Increment()  
{  
    return counter_++;  
}  
  
// Returns the current counter value, and decrements it.  
// counter can not be less than 0, return 0 in this case  
int Counter::Decrement()  
{  
    if (counter_ == 0) {   
        return counter_;  
    }  
    else {  
        return counter_--;  
    }  
}  
  
// Prints the current counter value.  
void Counter::Print() const {  
    cout << counter_;  
}

  单元测试用例
  先实例化对象,然后对类的成员函数进行测试。
TEST(CounterTest, Increment)   
{  
    Counter c;     
  
    // Test that counter 0 returns 0  
    EXPECT_EQ(0, c.Decrement());   
  
    EXPECT_EQ(0, c.Increment());  
    EXPECT_EQ(1, c.Increment());  
    EXPECT_EQ(2, c.Increment());  
  
    EXPECT_EQ(3, c.Decrement());  
}

  用例执行结果如下:

  示例二
  接下来的示例复杂一些。
  sample02由三个部分组成:sample02.h、 sample02.cpp、sample02UnitTest.cpp。
  sample02.h定义了一个string类MyString,阅读代码时,重点关注各成员的逻辑关系、厘清功能点,为用例的代码覆盖做准备。
  提炼的逻辑关系和功能点如下图所示:

  sample02.h源码如下:

#include <string.h>  
  
// A simple string class.  
class MyString  
{  
    private:  
        const char* c_string_; //声明一个字符指针常量 c_string_  
        const MyString& operator=(const MyString& rhs);   
        //赋值运算符重载函数
    public:  
        // Clones a 0-terminated C string, allocating memory using new.  //克隆一个以 \0 结尾的字符串,通过 new 申请内存空间  
        static const char* CloneCString(const char* a_c_string); //CloneCString() 函数将在 sample02.cpp中进行具体的实现  
  
        // The default c'tor constructs a NULL string.   
        MyString() : c_string_(nullptr) {} //构造函数,将 c_string_ 赋值为 空指针nullptr(相当于NULL。C++中推荐nullptr)  
  
        // Constructs a MyString by cloning a 0-terminated C string.  
        explicit MyString(const char* a_c_string) : c_string_(nullptr)  
        { //构造函数,克隆一个以 \0 结尾的字符串 
            Set(a_c_string);  
        }  
  
        // Copy constructor Func.  
        MyString(const MyString &string) : c_string_(nullptr) //string是一个对象引用,用于初始化另一个对象  
        { //拷贝构造函数  
            Set(string.c_string_);   
        }  
  
        //MyString is intended to be a final class, so the d'tor doesn't need to be virtual.  
        ~MyString() { delete[] c_string_; }  //析构函数 deconstructor Func.  
  
        // Gets the 0-terminated C string this MyString object represents.   
        const char* c_string() const { return c_string_; } // c_string()函数用于获取字符串 c_string_  
      
        // Length()函数用于返回字符串 c_string_的长度  
        size_t Length() const { return c_string_ == nullptr ? 0 : strlen(c_string_); }  
  
        //Sets the 0-terminated C string this MyString object represents.  
        void Set(const char* c_string); //set()函数将在 sample02.cpp中进行具体的实现  
};

  sample02.cpp对成员函数CloneCString( )、Set( )进行了具体实现。
#include "sample02.h"  
#include <string.h>  
  
// Clones a 0-terminated C string, allocating memory using new.  
const char* MyString::CloneCString(const char* a_c_string)  
{  
    if (a_c_string == nullptr) return nullptr; //空指针的情况  
  
    const size_t len = strlen(a_c_string); //size_t类似于int  
    char* const clone = new char[len + 1];   
    memcpy(clone, a_c_string, len + 1);  
  
    return clone;  
}  
  
// Sets the 0-terminated C string this MyString object represents.  
void MyString::Set(const char* a_c_string)  
{  
    // Makes sure this works when c_string == c_string_  
    const char* const temp = MyString::CloneCString(a_c_string);  
    delete[] c_string_; //执行内存释放  
    c_string_ = temp;  
}

  单元测试用例
  sample02UnitTest.cpp,各用例覆盖的成员函数如下图所示:


......
查看更多精彩内容,请点击下载:
  版权声明:本文出自《51测试天地》第六十三期。51Testing软件测试网及相关内容提供者拥有51testing.com内容的全部版权,未经明确的书面许可,任何人或单位不得对本网站内容复制、转载或进行镜像,否则将追究法律责任。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号