Gtest:事件
前言
有时候在测试的时候,我们会在测试前做一些初始化活动,和测试后做一些清理工作,gtest提供了多种事件机制,非常方便我们在案例之前或之后做一些操作。总结一下gtest的事件一共有3种:
- 全局的,所有案例执行前后。
- TestSuite级别的,在某一批案例中第一个案例前,最后一个案例执行后。
- TestCase级别的,每个TestCase前后。
接下来按照倒叙3→2→1介绍如何使用事件机制
TestCase事件
TestCase事件是挂在每个案例执行前后的,实现方式和上面的几乎一样,不过需要实现的是SetUp方法和TearDown方法:
1. SetUp()方法在每个TestCase之前执行
2. TearDown()方法在每个TestCase之后执行
演示代码(Linux环境)
main.cpp
#include "sample-inl.h"
#include "gtest/gtest.h"
namespace {
class QueueTestSmpl : public testing::Test {
protected: virtual void SetUp() {
//q0_.Enqueue(1);
q1_.Enqueue();
q1_.Enqueue();
q2_.Enqueue();
} virtual void TearDown() {
} static int Double(int n) {
return * n;
} // A helper function for testing Queue::Map().
void MapTester(const Queue<int> * q) {
// Creates a new queue, where each element is twice as big as the
// corresponding one in q.
const Queue<int> * const new_q = q->Map(Double); // Verifies that the new queue has the same size as q.
ASSERT_EQ(q->Size(), new_q->Size()); // Verifies the relationship between the elements of the two queues.
for (const QueueNode<int>*n1 = q->Head(), *n2 = new_q->Head();
n1 != nullptr; n1 = n1->next(), n2 = n2->next()) {
EXPECT_EQ( * n1->element(), n2->element());
} delete new_q;
} // Declares the variables your tests want to use.
Queue<int> q0_;
Queue<int> q1_;
Queue<int> q2_;
}; // Tests Dequeue().
TEST_F(QueueTestSmpl, Dequeue) {
int * n = q0_.Dequeue();
EXPECT_TRUE(n == nullptr); n = q1_.Dequeue();
ASSERT_TRUE(n != nullptr);
EXPECT_EQ(, *n);
EXPECT_EQ(1u, q1_.Size());
delete n; n = q1_.Dequeue();
ASSERT_TRUE(n != nullptr);
EXPECT_EQ(, *n);
EXPECT_EQ(0u, q1_.Size());
delete n; n = q2_.Dequeue();
ASSERT_TRUE(n != nullptr);
EXPECT_EQ(, *n);
EXPECT_EQ(0u, q2_.Size());
delete n;
}
} // namespace
sample-inl.h
#ifndef GTEST_SAMPLES_SAMPLE_INL_H_
#define GTEST_SAMPLES_SAMPLE_INL_H_ #include <stddef.h> template <typename E> // E is the element type
class Queue; template <typename E> // E is the element type
class QueueNode {
friend class Queue<E>; public:
// Gets the element in this node.
const E& element() const { return element_; } // Gets the next node in the queue.
QueueNode* next() { return next_; }
const QueueNode* next() const { return next_; } private:
// 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) {} // We disable the default assignment operator and copy c'tor.
const QueueNode& operator = (const QueueNode&);
QueueNode(const QueueNode&); E element_;
QueueNode* next_;
}; template <typename E> // E is the element type.
class Queue {
public:
// Creates an empty queue.
Queue() : head_(nullptr), last_(nullptr), size_() {} // D'tor. Clears the queue.
~Queue() { Clear(); } // Clears the queue.
void Clear() {
if (size_ > ) {
// 1. Deletes every node.
QueueNode<E>* node = head_;
QueueNode<E>* next = node->next();
for (; ;) {
delete node;
node = next;
if (node == nullptr) break;
next = node->next();
} // 2. Resets the member variables.
head_ = last_ = nullptr;
size_ = ;
}
} size_t Size() const { return size_; } QueueNode<E>* Head() { return head_; }
const QueueNode<E>* Head() const { return head_; } QueueNode<E>* Last() { return last_; }
const QueueNode<E>* Last() const { return last_; } void Enqueue(const E& element) {
QueueNode<E>* new_node = new QueueNode<E>(element); if (size_ == ) {
head_ = last_ = new_node;
size_ = ;
}
else {
last_->next_ = new_node;
last_ = new_node;
size_++;
}
} E* Dequeue() {
if (size_ == ) {
return nullptr;
} const QueueNode<E>* const old_head = head_;
head_ = head_->next_;
size_--;
if (size_ == ) {
last_ = nullptr;
} E* element = new E(old_head->element());
delete old_head; return element;
} template <typename F>
Queue* Map(F function) const {
Queue* new_queue = new Queue();
for (const QueueNode<E>* node = head_; node != nullptr;
node = node->next_) {
new_queue->Enqueue(function(node->element()));
} return new_queue;
} private:
QueueNode<E>* head_; // The first node of the queue.
QueueNode<E>* last_; // The last node of the queue.
size_t size_; // The number of elements in the queue. // We disallow copying a queue.
Queue(const Queue&);
const Queue& operator = (const Queue&);
}; #endif // GTEST_SAMPLES_SAMPLE3_INL_H_

TestSuite事件
这个相对来说比较简单,相对上面的实现就是替换成静态的SetUpTestCase,代码
class FooTest : public testing::Test {
protected:
//准备资源
static void SetUpTestCase() {
shared_resource_ = new ;
}
//释放资源
static void TearDownTestCase() {
delete shared_resource_;
shared_resource_ = NULL;
}
// 资源
static T* shared_resource_;
};
全局事件
也是很简单的,就是继承于testing::Environment而已。
class FooEnvironment: public testing::Environment
{
public:
virtual void SetUp()
{
printf("Environment SetUp!\n");
a = 100;
}
virtual void TearDown()
{
printf("Environment TearDown!\n");
}
int a; //共享数据
};
FooEnvironment* foo_env; //对象指针声明
TEST(firstTest, first) //访问共享数据并改变它的值
{
printf("in the firstTest, foo_env->p is %d\n", foo_env->a);
foo_env->a ++;
}
TEST(secondTest, second) //访问共享数据
{
printf("in the secondTest, foo_env->p is %d\n", foo_env->a);
}
int main(int argc, char* argv[])
{
foo_env = new FooEnvironment;
testing::AddGlobalTestEnvironment(foo_env); //注册
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Gtest:事件的更多相关文章
- JNI详解---从不懂到理解
转载:https://blog.csdn.net/hui12581/article/details/44832651 Chap1:JNI完全手册... 3 Chap2:JNI-百度百科... 11 C ...
- gtest 三种事件机制
前言: 1.首先说明gtest中事件的结构层次: 测试程序:一个测试程序只有一个main函数,也可以说是一个可执行程序是一个测试程序.该级别的事件机制会在程序的开始和结束执行. 测试套件:代表一个测试 ...
- gTest详解
1. 安装使用 1.1 安装 在https://code.google.com/p/googletest/ 下载源码 进入msvc, 注意编译方式, 如果是dll, 选择 gtest-md 编译生成l ...
- 白盒测试之gtest第一个demo
认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...
- gtest框架使用
gtest文档说明: 由于公司单元测试的需要,自己花了大半天时间下载了一个gtest框架,使用了一些测试例子,总览了coderzh的玩转gtest测试框架,又看了几篇gtest博客,写下了以下内容,作 ...
- gtest的安装和测试[good]
一.前言 本篇将介绍一些gtest的基本使用,包括下载,安装,编译,建立我们第一个测试Demo工程,以及编写一个最简单的测试案例. 二.下载 如果不记得网址, 直接在google里搜gtest,第一个 ...
- gtest简短,简单易用
gtest它是一种跨平台的(Liunx.Mac OS X.Windows.Cygwin.Windows CE and Symbian)的C++测试框架.有google该公司宣布. gtest台上为编写 ...
- gtest框架
解析gtest框架运行机制 1.前言 Google test是一款开源的白盒单元测试框架,据说目前在Google内部已在几千个项目中应用了基于该框架的白盒测试. 最近的工作是在搞一个基于gtest ...
- 简单易懂的单元测试框架-gtest(二)
简介 事件机制用于在案例运行前后添加一些操作(相当于挂钩函数).目前,gtest提供了三种等级的事件,分别: 全局级,所有案例执行的前后 TestSuite级,某一个案例集的前后 TestCa ...
随机推荐
- 在fedora 31 安装docker
简介: 本来没啥特别的,但是fedora使用的cgroup版本太高,docker还没跟上来. 就简单介绍一下怎么在fedora上安装docker吧 一:回退cgroup $ sudo dnf inst ...
- python办公自动化(一)PPTX
简介: python-pptx是python处理PPT的一个库,注重的是读和写,无法导出,没有渲染功能. 办公自动化,说的是大了一点,但是最常见的office三件套,word,excel,ppt.这还 ...
- vue脚手架中使用Vant,实现自动按需引入组件,并将px转换为rem
偶然间看到一款不错的移动端vue组件库Vant,照着官方文档敲了一下,感觉还是不错的.想着以后的项目中可能会运用到,特此记录下,方便之后使用. 现在很多的组件库为了减小代码包体积,都支持按需加载了.V ...
- react-navigation安卓从右到左切换视图
百度搜了3天都没一个正确的答案,最后还是google查到的: "react-navigation": "^4.0.10", "react-navi ...
- 全网最详细的Windows里Git client客户端管理工具SourceTree的下载与安装(图文详解)
不多说,直接上干货! 很多人用Git命令行不熟练,那么可以尝试使用SourceTree进行操作. 安装之前的必备 (1)Git的安装 Git学习系列之Windows上安装Git详细步骤(图文详解 ...
- How long does it take to make a context switch?
FROM: http://blog.tsunanet.net/2010/11/how-long-does-it-take-to-make-context.html That's a interesti ...
- 【C语言】 strlen()入参空指针导致段错误
背景: 在工作中调试sqlite3相关代码的时候,调用printf()打印sqlite3_exec()的执行日志:因为sqlite3_exec()保存日志的参数传入时为NULL,且没有执行错误,所以再 ...
- Python学习之路:函数传递可变参数与不可变参数
函数传参的方法: 太基础了,8说了 直接上重点 一.可变参数的传递 可变参数有:列表.集合.字典 直接上代码: a = [1, 2] def fun(a): print('传入函数时a的值为:', a ...
- 学Python要避免哪些坑,如何巩固好基础
学Python要避免哪些坑?零基础怎么入门Python?Python入门简单.语法简洁.功能强大,非常适合零基础入门IT行业的人学习.随着人工智能时代的来临,企业纷纷选择使用Python进行开发,Py ...
- Linux基础(10)AIO项目设计与POSIX文件操作和目录管理
实现fast-cp :拷贝文件到目标对象 Linux的七种文件类型 :https://blog.csdn.net/linkvivi/article/details/79834143 ls -al :h ...