gtest基础使用06:Google Test自带示例三:Test Fixture

一、环境信息

  1. Visual Studio 2019
  2. Windows 10
  3. C++模板介绍

二、Google Test Sample03

1. Test Fixture简介

1.1 Google Test sample03主要演示了 Test Fixture的使用
1.2 Fixtures are fittings or furniture which belong to a building and are legally part of it, for example, a bathtub or a toilet. 对于Test Fixture就是每个测试用例执行时都要用到的相同的测试资源,如果对每个测试用例都单独进行编码、用于准备测试资源,代码冗余量太大,因此Test Fixture对于 code sharing意义重大
1.3 使用Test Fixture,需要继承testing::Test类:说白了就是定制化自己的 void SetUp( ) 和 virtual void TearDown( ),使得不同用例可以使用相同的测试资源 及 复用相同的代码

//To use a test fixture, derive a class from testing::Test.
class QueueTestSmpl3 : public testing::Test
{
   
    protected:          // You should make the members protected s.t. they can be accessed from sub-classes. 
                        // s.t.全称subject to,意思是使得...满足...
        Queue<int> q0;// Declares the variables your tests want to use.
        Queue<int> q1; // 创建三个 sample03.h中定义的队列
        Queue<int> q2;

        // virtual void SetUp() will be called before each test is run.  //Google Test每一条用例执行前都会调用 void SetUp()
        // Youshould define it if you need to initialize the variables.
        // Otherwise, this can be skipped.
        void SetUp() override //override?
        {
   
            q1.Enqueue(1); //向队列中添加元素
            q2.Enqueue(2);
            q2.Enqueue(3);
        }

        // virtual void TearDown() will be called after each test is run. //Google Test每一条用例执行后都会调用 virtual void TearDown()
        // You should define it if there is cleanup work to do. //TearDown用来清理数据
        // Otherwise,you don't have to provide it. 
        //virtual void TearDown() { }
};

1.4 TEST Fixture需要使用宏 TEST_F,而且 测试用例集名字 必须和定义的类名保持一致

class QueueTest : public testing::Test{
   //realization };
TEST_F(QueueTest, DefaultConstructor){
   //realization}

1.5 各个测试用例TEST_F()使用相同的测试资源,测试资源不会受到上一条测试用例的影响

2. 单元测试用例

2.1 在单元测试用例中,首先定义了类Queue ,随后使用Test Fixture对类进行了测试,可以看到:所有的测试用例TEST_F()在执行时均调用了相同的测试资源
2.2 如下代码已经注释并调试通过,欢迎大家指导
在这里插入图片描述

//#ifndef GOOGLETEST_SAMPLES_SAMPLE3_INL_H_
//#define GOOGLETEST_SAMPLES_SAMPLE3_INL_H_

#include <stddef.h> //template
#include "pch.h" // #include "gtest/gtest.h"

// Queue is a simple queue implemented as a singled-linked list.//此队列是一个单链表
// The element type must support copy constructor.
template <typename E> class Queue; // E is the element type

// QueueNode is a node in a Queue, which consists of an element of type E and a pointer to the next node.
template <typename E> class QueueNode // QueueNode是队列中的结点,结点中包含 类型为E的元素 和 指向下一个结点的指针
{
   
	friend class Queue<E>; //Queue是QueueNode的友元类,因此Queue可以访问QueueNode的所有成员

private:
	E element_;
	QueueNode* next_; //指向类的指针

	// Creates a node with a given element value.  The next pointer is set to NULL.
	explicit QueueNode(const E &an_element) :element_(an_element), next_(nullptr) {
   } //构造函数 //explicit? 

	// We disable the default assignment operator and copy c'tor. //why disable?
	const QueueNode& operator= (const QueueNode&);
	QueueNode(const QueueNode&);

public:
	const E &
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值