51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1494|回复: 0
打印 上一主题 下一主题

[原创] GoogleTest单元测试框架总结之类测试和Test Fixture

[复制链接]
  • TA的每日心情
    无聊
    半小时前
  • 签到天数: 943 天

    连续签到: 2 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2021-10-25 13:25:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
     类的测试
      我们先从一个简单的计数器开始。

      示例一
      头文件Counter.h仅定义功能,不进行具体实现:增加计数 Increment()、减少计数Decrement()、打印。
    1. class Counter  
    2. {  
    3. private:  
    4.     int counter_;  
    5.   
    6. public:  
    7.     // Creates a counter that starts at 0.  
    8.     Counter() : counter_(0) {}   
    9.   
    10.     // Returns the current counter value, and increments it.  
    11.     int Increment();
    12.   
    13.     // Returns the current counter value, and decrements it.  
    14.     int Decrement();  
    15.   
    16.     // Prints the current counter value to STDOUT.  
    17.     void Print() const;  
    18. };
    复制代码
    配套的cpp文件,负责成员函数的具体实现。
    1. #include "Counter.h"   
    2. // Returns the current counter value, and increments it.  
    3. int Counter::Increment()  
    4. {  
    5.     return counter_++;  
    6. }  
    7.   
    8. // Returns the current counter value, and decrements it.  
    9. // counter can not be less than 0, return 0 in this case  
    10. int Counter::Decrement()  
    11. {  
    12.     if (counter_ == 0) {   
    13.         return counter_;  
    14.     }  
    15.     else {  
    16.         return counter_--;  
    17.     }  
    18. }  
    19.   
    20. // Prints the current counter value.  
    21. void Counter::Print() const {  
    22.     cout << counter_;  
    23. }
    复制代码
     单元测试用例
      先实例化对象,然后对类的成员函数进行测试。
    1. TEST(CounterTest, Increment)   
    2. {  
    3.     Counter c;     
    4.   
    5.     // Test that counter 0 returns 0  
    6.     EXPECT_EQ(0, c.Decrement());   
    7.   
    8.     EXPECT_EQ(0, c.Increment());  
    9.     EXPECT_EQ(1, c.Increment());  
    10.     EXPECT_EQ(2, c.Increment());  
    11.   
    12.     EXPECT_EQ(3, c.Decrement());  
    13. }
    复制代码
    用例执行结果如下:

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

    sample02.h源码如下:
    1. #include <string.h>  
    2.   
    3. // A simple string class.  
    4. class MyString  
    5. {  
    6.     private:  
    7.         const char* c_string_; //声明一个字符指针常量 c_string_  
    8.         const MyString& operator=(const MyString& rhs);   
    9.         //赋值运算符重载函数
    10.     public:  
    11.         // Clones a 0-terminated C string, allocating memory using new.  //克隆一个以 \0 结尾的字符串,通过 new 申请内存空间  
    12.         static const char* CloneCString(const char* a_c_string); //CloneCString() 函数将在 sample02.cpp中进行具体的实现  
    13.   
    14.         // The default c'tor constructs a NULL string.   
    15.         MyString() : c_string_(nullptr) {} //构造函数,将 c_string_ 赋值为 空指针nullptr(相当于NULL。C++中推荐nullptr)  
    16.   
    17.         // Constructs a MyString by cloning a 0-terminated C string.  
    18.         explicit MyString(const char* a_c_string) : c_string_(nullptr)  
    19.         { //构造函数,克隆一个以 \0 结尾的字符串
    20.             Set(a_c_string);  
    21.         }  
    22.   
    23.         // Copy constructor Func.  
    24.         MyString(const MyString &string) : c_string_(nullptr) //string是一个对象引用,用于初始化另一个对象  
    25.         { //拷贝构造函数  
    26.             Set(string.c_string_);   
    27.         }  
    28.   
    29.         //MyString is intended to be a final class, so the d'tor doesn't need to be virtual.  
    30.         ~MyString() { delete[] c_string_; }  //析构函数 deconstructor Func.  
    31.   
    32.         // Gets the 0-terminated C string this MyString object represents.   
    33.         const char* c_string() const { return c_string_; } // c_string()函数用于获取字符串 c_string_  
    34.       
    35.         // Length()函数用于返回字符串 c_string_的长度  
    36.         size_t Length() const { return c_string_ == nullptr ? 0 : strlen(c_string_); }  
    37.   
    38.         //Sets the 0-terminated C string this MyString object represents.  
    39.         void Set(const char* c_string); //set()函数将在 sample02.cpp中进行具体的实现  
    40. };
    复制代码
    sample02.cpp对成员函数CloneCString( )、Set( )进行了具体实现。

    1. #include "sample02.h"  
    2. #include <string.h>  
    3.   
    4. // Clones a 0-terminated C string, allocating memory using new.  
    5. const char* MyString::CloneCString(const char* a_c_string)  
    6. {  
    7.     if (a_c_string == nullptr) return nullptr; //空指针的情况  
    8.   
    9.     const size_t len = strlen(a_c_string); //size_t类似于int  
    10.     char* const clone = new char[len + 1];   
    11.     memcpy(clone, a_c_string, len + 1);  
    12.   
    13.     return clone;  
    14. }  
    15.   
    16. // Sets the 0-terminated C string this MyString object represents.  
    17. void MyString::Set(const char* a_c_string)  
    18. {  
    19.     // Makes sure this works when c_string == c_string_  
    20.     const char* const temp = MyString::CloneCString(a_c_string);  
    21.     delete[] c_string_; //执行内存释放  
    22.     c_string_ = temp;  
    23. }
    复制代码
    单元测试用例
      sample02UnitTest.cpp,各用例覆盖的成员函数如下图所示:


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-7 12:33 , Processed in 0.063840 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表