51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

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

[原创] GoogleTest框架总结之类Test Fixture的继承和差异化和接口测试

[复制链接]
  • TA的每日心情
    郁闷
    昨天 09:01
  • 签到天数: 980 天

    连续签到: 5 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2021-11-2 15:21:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    一、Test Fixture的继承和差异化
      之前介绍了Test Fixture,下面通过示例sample05说明下如何进行Test Fixture的继承。
      对已有的Test Fixture进行继承,往往发生在如下两个场景 (1) 定义的Test Fixture可用于指定的一套单元测试用例集,当有多套测试用例时,如何处理? (2)当不同的测试用例集对应的测试资源基本相同、但又存在少量差异,又如何处理?
      被继承的Test Fixture在Google Test 中被称为 Super Fixture、继承者被称为sub fixture。Super Fixture /Test Fixture 本质就是一个类,类当然可以被继承啦。

      sample05的测试对象
      1. 测试对象是 sample01.h + sample01.cpp, sample03.h,文件结构如下图所示:

    sample01.h 对Factorial( ), IsPrime( )函数进行了声明、sample01.cpp 对Factorial( ) 、IsPrime( )两个函数进行了具体实现:Factorial用于求一个整数的阶乘、IsPrime用于判定一个整数是否为素数
      sample03.h 完整定义了类Queue(包含成员函数的具体实现):类Queue实现了队列的基本功能,同时他包含了一个定制化的Map( )函数
      上述源码文件在之前的文档中已经展示过,这里不再重复。

      对应的单元测试用例
      1. sample05UnitTest.cpp中定义了名为QuickTest的Test Fixture,用来判定用例的执行时间是否超过5秒:从SetUp( )调用时启动计时,从TearDown( )调用时结束计时,期间时间就是用例执行时间。
      GoogleTest的每一条用例,正式启动前先调用SetUp( ),执行结束后调用TearDown( )。
    1. class QuickTest : public testing::Test   
    2.     {  
    3.     protected:  
    4.         time_t start_time_;  
    5.         void SetUp() override  
    6.          {   
    7.             start_time_ = time(nullptr);   
    8.         }  
    9.         void TearDown() override  
    10.         {   
    11.             const time_t end_time = time(nullptr);  
    12.             EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";  
    13.         }  
    14. };
    复制代码
    2. Test Fixture的继承
      IntegerFunctionTest继承自测试夹具QuickTest,该继承解决了上面提到的问题一:只定义了一个Test Fixture,但是需要编写多套测试用例?
    1. class IntegerFunctionTest : public QuickTest{ };  
    2. TEST_F(IntegerFunctionTest, Factorial)  
    3. {  
    4.         EXPECT_EQ(1, Factorial(-5)); //每条用例都会判定是否超过5秒  
    5.         EXPECT_EQ(1, Factorial(-1));  
    6. }
    复制代码
     QueueTest也继承自测试夹具QuickTest,他的目标是测试类Queue,因此进行了必要的差异化:在SetUp( )中为类Queue的实例化对象赋初值
      该继承解决了上面提到的问题二:不同的测试用例集使用相同的测试资源、但又存在差异?
    1. class QueueTest : public QuickTest  
    2.     {  
    3.     protected:  
    4.          Queue<int> q0_; //差异化  
    5.         Queue<int> q1_;  
    6.         Queue<int> q2_;
    7.         void SetUp() override  
    8.         {  
    9.             QuickTest::SetUp();  
    10.             q1_.Enqueue(1);  
    11.             q2_.Enqueue(2);  
    12.             q2_.Enqueue(3);  
    13.         }            
    14.     };
    复制代码
    使用QueueTest的宏TEST_F,由于继承自QuickTest,同样会判定用例的执行是否超过5秒:
    1. TEST_F(QueueTest, DefaultConstructor)  
    2.     {  
    3.         EXPECT_EQ(0, q0_.Size());  
    4.     }
    复制代码
    QueueTest, IntegerFunctionTest 也可以被继承:可以进行Test Fixture继承的继承,此类继承没有层数限制。

      3. 单元测试用例的完整源码
      下面通过测试夹具QuickTest, QueueTest, IntegerFunctionTest完成sample01.h和 sample03.h的测试。
    1. #include "pch.h"  
    2. #include <limits.h>  
    3. #include <time.h>   
    4. #include "sample01.h"  
    5. #include "sample03.h"   
    6. namespace {  
    7.     class QuickTest : public testing::Test   //首先定义测试夹具QuickTest
    8.     {  
    9.     protected:   
    10.         time_t start_time_;   
    11.         void SetUp() override  
    12.         {   
    13.             start_time_ = time(nullptr);   
    14.         }  
    15.         void TearDown() override   
    16.         {  
    17.             const time_t end_time = time(nullptr);
    18.             EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";  
    19.         }  
    20.     };  
    21.   
    22.     class IntegerFunctionTest : public QuickTest  
    23.     {  
    24.         // IntegerFunctionTest继承自QuickTest,由于不需要差异化,因此代码为空
    25.     };  
    26.   
    27.       //通过IntegerFunctionTest完成Factorial() 和 IsPrime()的单元测试

    28.       TEST_F(IntegerFunctionTest, Factorial)
    29.        {  
    30.         // Tests factorial of negative numbers.  
    31.         EXPECT_EQ(1, Factorial(-5));  
    32.         EXPECT_EQ(1, Factorial(-1));  
    33.         EXPECT_GT(Factorial(-10), 0);  
    34.   
    35.         // Tests factorial of 0.  
    36.         EXPECT_EQ(1, Factorial(0));  
    37.   
    38.         // Tests factorial of positive numbers.  
    39.         EXPECT_EQ(1, Factorial(1));  
    40.         EXPECT_EQ(2, Factorial(2));  
    41.         EXPECT_EQ(6, Factorial(3));  
    42.         EXPECT_EQ(40320, Factorial(8));  
    43.         }  
    44.   
    45.   
    46.     // Tests IsPrime()  
    47.     TEST_F(IntegerFunctionTest, IsPrime)  
    48.     {  
    49.         // Tests negative input.  
    50.         EXPECT_FALSE(IsPrime(-1));  
    51.         EXPECT_FALSE(IsPrime(-2));  
    52.         EXPECT_FALSE(IsPrime(INT_MIN)); //INT_MIN是边界值  
    53.   
    54.         // Tests some trivial cases.  
    55.         EXPECT_FALSE(IsPrime(0));  
    56.         EXPECT_FALSE(IsPrime(1));  
    57.         EXPECT_TRUE(IsPrime(2));  
    58.         EXPECT_TRUE(IsPrime(3));  
    59.   
    60.         // Tests positive input.  
    61.         EXPECT_FALSE(IsPrime(4));  
    62.         EXPECT_TRUE(IsPrime(5));  
    63.         EXPECT_FALSE(IsPrime(6));  
    64.         EXPECT_TRUE(IsPrime(23));  
    65.     }  
    66.   
    67.     class QueueTest : public QuickTest  
    68.     {  // QueueTest 也继承自QuickTest,由于是测试Queue,进行了部分差异化
    69.     protected:  
    70.         Queue<int> q0_;  
    71.         Queue<int> q1_;  
    72.         Queue<int> q2_;  
    73.         void SetUp() override  
    74.         {   
    75.             QuickTest::SetUp();  
    76.             q1_.Enqueue(1);  
    77.             q2_.Enqueue(2);  
    78.             q2_.Enqueue(3);  
    79.         }   
    80.     };  

    81. //通过 QueueTest完成类Queue的测试,进行代码测试并评估用例的执行时间是否超过5秒
    82.     TEST_F(QueueTest, DefaultConstructor)  
    83.     {  
    84.         EXPECT_EQ(0, q0_.Size());  
    85.     }  
    86.   
    87.     // Tests Dequeue().  
    88.     TEST_F(QueueTest, Dequeue)   
    89.     {  
    90.         int* n = q0_.Dequeue();  
    91.         EXPECT_TRUE(n == nullptr);  
    92.   
    93.         n = q1_.Dequeue();  
    94.         EXPECT_TRUE(n != nullptr);  
    95.         EXPECT_EQ(1, *n);  
    96.         EXPECT_EQ(0u, q1_.Size());  
    97.         delete n;  
    98.   
    99.         n = q2_.Dequeue();  
    100.         EXPECT_TRUE(n != nullptr);  
    101.         EXPECT_EQ(2, *n);  
    102.         EXPECT_EQ(1u, q2_.Size());  
    103.         delete n;  
    104.     }  
    105. }  // namespace
    复制代码
    4. 用例执行结果如下
      从测试结果来看,所有用例的执行时间都在5秒内:






    本帖子中包含更多资源

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

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

    使用道具 举报

    本版积分规则

    关闭

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

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

    GMT+8, 2024-7-6 04:29 , Processed in 0.067697 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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