TEST_F:多个测试使用同样的配置
2015-07-07 22:27
316 查看
转自:http://blog.chinaunix.net/uid-24709751-id-4358266.html
https://code.google.com/p/googletest/wiki/V1_7_Primer 场景:
1. 准备数据A,TEST测试a
2. 准备数据A,TEST测试b
....
3. 准备数据A,TEST测试n
问题:
准备数据A太多次,,,可以使用TEST_F,就是为了解决这种情况。
For each test defined with TEST_F(), Google Test will:
Create a fresh test fixture at runtime
Immediately initialize it via SetUp() ,
Run the test
Clean up by calling TearDown()
Delete the test fixture. Note that different tests in the same test case
have different test fixture objects, and Google Test always
deletes a test fixture before it creates the next one. Google Test does not reuse
the same test fixture for multiple tests. Any changes one test makes
to the fixture donot affect other tests.
As an example, let's write tests for a FIFO queue class named Queue, which has the following interface:
template // E is the element type.
class Queue {
public:
Queue();
void Enqueue(const E& element);
E* Dequeue(); // Returns NULL if the queue is empty.
size_t size() const;
...
};
First, define a fixture class. By convention, you should give it the name FooTest where Foo is the class being tested.
class QueueTest : public ::testing::Test {
protected:
virtual void SetUp() {
q1_.Enqueue(1);
q2_.Enqueue(2);
q2_.Enqueue(3);
}
// virtual void TearDown() {}
Queue q0_;
Queue q1_;
Queue q2_;
};
In this case, TearDown() is not needed since we don't have to clean up after each test, other than what's
already done by the destructor.
Now we'll write tests using TEST_F() and this fixture.
TEST_F(QueueTest, IsEmptyInitially) {
EXPECT_EQ(0, q0_.size());
}
TEST_F(QueueTest, DequeueWorks) {
int* n = q0_.Dequeue();
EXPECT_EQ(NULL, n);
n = q1_.Dequeue();
ASSERT_TRUE(n != NULL);
EXPECT_EQ(1, *n);
EXPECT_EQ(0, q1_.size());
delete n;
n = q2_.Dequeue();
ASSERT_TRUE(n != NULL);
EXPECT_EQ(2, *n);
EXPECT_EQ(1, q2_.size());
delete n;
}
https://code.google.com/p/googletest/wiki/V1_7_Primer 场景:
1. 准备数据A,TEST测试a
2. 准备数据A,TEST测试b
....
3. 准备数据A,TEST测试n
问题:
准备数据A太多次,,,可以使用TEST_F,就是为了解决这种情况。
For each test defined with TEST_F(), Google Test will:
Create a fresh test fixture at runtime
Immediately initialize it via SetUp() ,
Run the test
Clean up by calling TearDown()
Delete the test fixture. Note that different tests in the same test case
have different test fixture objects, and Google Test always
deletes a test fixture before it creates the next one. Google Test does not reuse
the same test fixture for multiple tests. Any changes one test makes
to the fixture donot affect other tests.
As an example, let's write tests for a FIFO queue class named Queue, which has the following interface:
template // E is the element type.
class Queue {
public:
Queue();
void Enqueue(const E& element);
E* Dequeue(); // Returns NULL if the queue is empty.
size_t size() const;
...
};
First, define a fixture class. By convention, you should give it the name FooTest where Foo is the class being tested.
class QueueTest : public ::testing::Test {
protected:
virtual void SetUp() {
q1_.Enqueue(1);
q2_.Enqueue(2);
q2_.Enqueue(3);
}
// virtual void TearDown() {}
Queue q0_;
Queue q1_;
Queue q2_;
};
In this case, TearDown() is not needed since we don't have to clean up after each test, other than what's
already done by the destructor.
Now we'll write tests using TEST_F() and this fixture.
TEST_F(QueueTest, IsEmptyInitially) {
EXPECT_EQ(0, q0_.size());
}
TEST_F(QueueTest, DequeueWorks) {
int* n = q0_.Dequeue();
EXPECT_EQ(NULL, n);
n = q1_.Dequeue();
ASSERT_TRUE(n != NULL);
EXPECT_EQ(1, *n);
EXPECT_EQ(0, q1_.size());
delete n;
n = q2_.Dequeue();
ASSERT_TRUE(n != NULL);
EXPECT_EQ(2, *n);
EXPECT_EQ(1, q2_.size());
delete n;
}
相关文章推荐
- Linux上安装Apache环境及安装过程报错解决方案(零初始环境)
- jdk安装和环境变量的配置
- FlashDevelop安装配置
- 导入Android项目出现invalid project description问题
- 【一些事晚报】周鸿祎:要一夜情的客户
- D-U-N-S申请流程
- [SaltStack] salt-master启动流程
- w7安装双系统
- if语句求三个数中最大的
- ios 保存本地数据的方法
- phpQuery—基于jQuery的PHP实现(转)
- 求连续子数组最大和问题的两种解法_PHP实现
- 【Go语言】【13】 4000 再谈GO语言的结构体
- 【Go语言】【13】再谈GO语言的结构体
- 《鸟哥的Linux私房菜》第六章 Linux的文件权限与目录配置
- c语言中的isalpha,isdigit,islower,isupper等一系列函数
- Project Euler:Problem 56 Powerful digit sum
- 每个磁道上的扇区数一样吗?
- stun技术原理与实现(一)——背景
- poj 3268 Silver Cow Party