Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1547174
  • 博文数量: 596
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 173
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-06 15:50
个人简介

在线笔记

文章分类

全部博文(596)

文章存档

2016年(1)

2015年(104)

2014年(228)

2013年(226)

2012年(26)

2011年(11)

分类: Android平台

2014-07-16 16:10:50


场景:
1. 准备数据A,TEST测试a
2. 准备数据A,TEST测试b
....
3. 准备数据A,TEST测试n

问题:
准备数据A太多次,,,可以使用TEST_F,就是为了解决这种情况。

  1. For each test defined with TEST_F(), Google Test will:

  2.     Create a fresh test fixture at runtime
  3.     Immediately initialize it via SetUp() ,
  4.     Run the test
  5.     Clean up by calling TearDown()
  6.     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 do not affect other tests.

  7. As an example, let's write tests for a FIFO queue class named Queue, which has the following interface:

  8. template // E is the element type.
  9. class Queue {
  10.  public:
  11.   Queue();
  12.   void Enqueue(const E& element);
  13.   E* Dequeue(); // Returns NULL if the queue is empty.
  14.   size_t size() const;
  15.   ...
  16. };

  17. First, define a fixture class. By convention, you should give it the name FooTest where Foo is the class being tested.

  18. class QueueTest : public ::testing::Test {
  19.  protected:
  20.   virtual void SetUp() {
  21.     q1_.Enqueue(1);
  22.     q2_.Enqueue(2);
  23.     q2_.Enqueue(3);
  24.   }

  25.   // virtual void TearDown() {}

  26.   Queue q0_;
  27.   Queue q1_;
  28.   Queue q2_;
  29. };

  30. 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.

  31. Now we'll write tests using TEST_F() and this fixture.

  32. TEST_F(QueueTest, IsEmptyInitially) {
  33.   EXPECT_EQ(0, q0_.size());
  34. }

  35. TEST_F(QueueTest, DequeueWorks) {
  36.   int* n = q0_.Dequeue();
  37.   EXPECT_EQ(NULL, n);

  38.   n = q1_.Dequeue();
  39.   ASSERT_TRUE(n != NULL);
  40.   EXPECT_EQ(1, *n);
  41.   EXPECT_EQ(0, q1_.size());
  42.   delete n;

  43.   n = q2_.Dequeue();
  44.   ASSERT_TRUE(n != NULL);
  45.   EXPECT_EQ(2, *n);
  46.   EXPECT_EQ(1, q2_.size());
  47.   delete n;
  48. }


阅读(6216) | 评论(0) | 转发(0) |
0

上一篇:重叠IO的四种机制

下一篇:VIM VMB 安装插件

给主人留下些什么吧!~~