2021.1.18 (含GTest第七个例子)

第三十四天

早上

    与昨天稍有不同,今天的是“数值参数化“ , 昨天的是”类型参数化“,也就是说进行参数化测试的不是各种各样的class,而是变量(一般都是指针)。
   不得不说,现在看代码的主要障碍依然是C++的用法,Gtest写的很专业,向我们这种非妓院的还有很多用法不熟。
Gtest 小技巧 - 8:不同工具(参数)使用测试接口
    同上一个例子一样,我们应该尽量避免在接口(夹具)内直接调用工具的方法或参数,而是通过指针间接调用。
    但是注意,这次我们传入的不是类,而是指针变量,因此我们让这个指针指向的是创造工具类的实例的功能函数,它们的返回值就是new申请的工具类的指针,这样我们在夹具的setup中调用测试参数,就能创建得到对应的类的指针,再用得到的指针进行工具类的测试即可。
   应当注意的是,seout 和teardown 在每次测试前后都会执行,若不想造成内存泄漏或者影响紧接的测试,一定要注意在teardown中清空相关指针和变量。
最后参数话的时候:
INSTANTIATE_TEST_SUITE_P(OnTheFlyAndPreCalculated, //实例名
PrimeTableTestSmpl7,//夹具名
Values(&CreateOnTheFlyPrimeTable,
&CreatePreCalculatedPrimeTable<1000>));//传入的参数列表,这里是函数指针

注意,有一点不是很明白,上一节是吧new delete写在构造里的,这一节却专门要求写在了setup里,难道是不同参数化测试方法的执行方式不同吗?(数值参数化只会产生一个夹具,在一个夹具内进行多次测试;而类型参数话会产生多个夹具,相当于对于每个类都重用了夹具的独立测试?)

源代码:

sample7_unittest.cc

// This sample shows how to test common properties of multiple
// implementations of an interface (aka interface tests) using
// value tests. Each test in the test case has
// a parameter that is an interface pointer to an implementation
// tested.

// The interface and its implementations are in this header.
#include "prime_tables.h"

#include "gtest/gtest.h"
namespace {

using ::testing:: ;
using ::testing::Values;

// As a general rule, to prevent a test from affecting the tests that come
// after it, you should create and destroy the tested objects for each test
// instead of reusing them.  In this sample we will define a simple factory
// function for PrimeTable objects.  We will instantiate objects in test's
// SetUp() method and delete them in TearDown() method.
typedef PrimeTable* CreatePrimeTableFunc();

PrimeTable* CreateOnTheFlyPrimeTable() {
  return new OnTheFlyPrimeTable();
}

template <size_t max_precalculated>
PrimeTable* CreatePreCalculatedPrimeTable() {
  return new PreCalculatedPrimeTable(max_precalculated);
}

// Inside the test body, fixture constructor, SetUp(), and TearDown() you
// can refer to the test parameter by GetParam().  In this case, the test
// parameter is a factory function which we call in fixture's SetUp() to
// create and store an instance of PrimeTable.
class PrimeTableTestSmpl7 : public TestWithParam<CreatePrimeTableFunc*> {
 public:
  ~PrimeTableTestSmpl7() override { delete table_; }
  void SetUp() override { table_ = (*GetParam())(); }
  void TearDown() override {
    delete table_;
    table_ = nullptr;
  }

 protected:
  PrimeTable* table_;
};

TEST_P(PrimeTableTestSmpl7, ReturnsFalseForNonPrimes) {
  EXPECT_FALSE(table_->IsPrime(-5));
  EXPECT_FALSE(table_->IsPrime(0));
  EXPECT_FALSE(table_->IsPrime(1));
  EXPECT_FALSE(table_->IsPrime(4));
  EXPECT_FALSE(table_->IsPrime(6));
  EXPECT_FALSE(table_->IsPrime(100));
}

TEST_P(PrimeTableTestSmpl7, ReturnsTrueForPrimes) {
  EXPECT_TRUE(table_->IsPrime(2));
  EXPECT_TRUE(table_->IsPrime(3));
  EXPECT_TRUE(table_->IsPrime(5));
  EXPECT_TRUE(table_->IsPrime(7));
  EXPECT_TRUE(table_->IsPrime(11));
  EXPECT_TRUE(table_->IsPrime(131));
}

TEST_P(PrimeTableTestSmpl7, CanGetNextPrime) {
  EXPECT_EQ(2, table_->GetNextPrime(0));
  EXPECT_EQ(3, table_->GetNextPrime(2));
  EXPECT_EQ(5, table_->GetNextPrime(3));
  EXPECT_EQ(7, table_->GetNextPrime(5));
  EXPECT_EQ(11, table_->GetNextPrime(7));
  EXPECT_EQ(131, table_->GetNextPrime(128));
}

// In order to run value-parameterized tests, you need to instantiate them,
// or bind them to a list of values which will be used as test parameters.
// You can instantiate them in a different translation module, or even
// instantiate them several times.
//
// Here, we instantiate our tests with a list of two PrimeTable object
// factory functions:
INSTANTIATE_TEST_SUITE_P(OnTheFlyAndPreCalculated, PrimeTableTestSmpl7,
                         Values(&CreateOnTheFlyPrimeTable,
                                &CreatePreCalculatedPrimeTable<1000>));//????????????????????????????????????

}  // namespace

练一下转述哦

下午

C

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值