【C++ UnitTest】基于Google Test 的Value-Parameterized Tests使用和示例

本文介绍了在C++编程中,特别是在继承关系复杂的情况下,如何通过引入GoogleTest中的值参数化测试(Value-ParameterizedTests)来减少重复代码,提高测试的可维护性和覆盖范围。作者通过示例展示了如何利用参数化测试模板验证不同子类的接口实现,以及如何编写和运行这样的测试用例。

在前面的阶乘代码演示中,出现过很多对相同参数变量赋值的情况。同样在继承关系多且复杂的C++代码中,Overriding的同名实现,难道要为此写不同的UT? 有没有简单的办法可以偷懒 ------ 值参数化(Value-Parameterized Tests)。

引入值参数化的好处

引入值参数化测试的概念可以让你更方便地编写和管理多个相似但具有不同输入的测试用例。Google Test中引入参数化测试的主要目的有以下几点:

1.减少重复代码: 值参数化测试允许你定义一个测试模板,并在其中指定参数的范围或值。这样一来,你可以避免编写大量相似的测试用例,而是在一个地方定义测试模板,然后通过提供不同的参数来生成多个测试用例。

2.提高可维护性: 使用值参数化测试,当测试逻辑需要更新时,你只需更新测试模板,而不必修改多个重复的测试用例。这样可以减少代码冗余,并提高测试代码的可维护性。

3.扩展性: 值参数化测试使得添加新的测试用例变得更加简单和灵活。你可以很容易地向测试模板中添加新的参数组合,从而扩展测试覆盖范围。

4.更好的测试覆盖: 值参数化测试可以轻松地测试多个输入值对于函数的各种情况下的行为。通过提供不同的参数组合,你可以更全面地测试函数的各种边界情况和特殊情况。

总的来说,引入值参数化测试的概念可以帮助你编写更简洁、更可维护、更灵活的测试代码,从而提高测试的效率和覆盖范围。

先请看官方的示例

函数代码来自google test在github上的第7个示例,这里应用了参数化的方式对子类的同名函数实现接口进行了测试,而无需对不同子类的同名实现进行不同的UT编写:

代码说明:

这里父类PrimeTable 定了两个纯虚成员函数IsPrimeGetNextPrime,并在两个子类中分别实现了他们,OnTheFlyPrimeTablePreCalculatedPrimeTable ,两个子类对成员函数除了具体代码内容不同,功能和输入输出参数都是一致的。继承于同一父类的子类接口就可以用参数化的方式来验证接口是否OK。

src:
https://github.com/google/googletest/blob/v1.8.x/googletest/samples/prime_tables.h

接口实现代码
// This provides interface PrimeTable that determines whether a number is a
// prime and determines a next prime number. This interface is used
// in Google Test samples demonstrating use of parameterized tests.

#ifndef GTEST_SAMPLES_PRIME_TABLES_H_
#define GTEST_SAMPLES_PRIME_TABLES_H_

#include <algorithm>

// The prime table interface.
class PrimeTable {
   
   
public:
    virtual ~PrimeTable() {
   
   }

    // Returns true iff n is a prime number.
    virtual bool IsPrime(int n) const = 0;

    // Returns the smallest prime number greater than p; or returns -1
    // if the next prime is beyond the capacity of the table.
    virtual int GetNextPrime(int p) const = 0;
};

// Implementation #1 calculates the primes on-the-fly.
class OnTheFlyPrimeTable : public PrimeTable {
   
   
public:
    virtual bool IsPrime(int n) const {
   
   
        if (n <= 1) return false;

        for (int i = 2; i * i <= n; i++) {
   
   
            // n is divisible by an integer other than 1 and itself.
            if ((n % i) == 0) return false;
        }

        return true;
    }

    virtual int GetNextPrime(int p) const {
   
   
        for (int n = p + 1; n > 0; n++) {
   
   
            if (IsPrime(n)) return n;
        }

        return -1;
    }
};

// Implementation #2 pre-calculates the primes and stores the result
// in an array.
class PreCalculatedPrimeTable : public PrimeTable {
   
   
public:
    // 'max' specifies the maximum number the prime table holds.
    explicit PreCalculatedPrimeTable(int max)
        : is_prime_size_(max 
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值