• Google单元测试sample分析(二)


    本文开始介绍googletest/googletest/sample/sample5_unittest.cc
    有关TEST_F测试夹具的使用案例

    class QuickTest : public testing::Test {
     protected:
      // Remember that SetUp() is run immediately before a test starts.
      // This is a good place to record the start time.
      void SetUp() override { start_time_ = time(nullptr); }
    
      // TearDown() is invoked immediately after a test finishes.  Here we
      // check if the test was too slow.
      void TearDown() override {
        // Gets the time when the test finishes
        const time_t end_time = time(nullptr);
    
        // Asserts that the test took no more than ~5 seconds.  Did you
        // know that you can use assertions in SetUp() and TearDown() as
        // well?
        EXPECT_TRUE(end_time - start_time_ <= 5) << "The test took too long.";
      }
    
      // The UTC time (in seconds) when the test starts
      time_t start_time_;
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    这里的QuickTest继承自testing::Test,并重写SetUp和TearDown方法,SetUp方法在每个TEST_F开始运行一次,退出执行TearDown方法一次。
    这个案例里记录整个TEST_F测试夹具的测试用例的运行时间

    class IntegerFunctionTest : public QuickTest {
      // We don't need any more logic than already in the QuickTest fixture.
      // Therefore the body is empty.
    };
    
    
    // Now we can write tests in the IntegerFunctionTest test case.
    
    // Tests Factorial()
    TEST_F(IntegerFunctionTest, Factorial) {
      // Tests factorial of negative numbers.
      EXPECT_EQ(1, Factorial(-5));
      EXPECT_EQ(1, Factorial(-1));
      EXPECT_GT(Factorial(-10), 0);
    
      // Tests factorial of 0.
      EXPECT_EQ(1, Factorial(0));
    
      // Tests factorial of positive numbers.
      EXPECT_EQ(1, Factorial(1));
      EXPECT_EQ(2, Factorial(2));
      EXPECT_EQ(6, Factorial(3));
      EXPECT_EQ(40320, Factorial(8));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    这里IntegerFunctionTest 继承自QuickTest ,内部无其他实现,测试夹具IntegerFunctionTest整个运行时间

    class QueueTest : public QuickTest {
     protected:
      void SetUp() override {
        // First, we need to set up the super fixture (QuickTest).
        QuickTest::SetUp();
    
        // Second, some additional setup for this fixture.
        q1_.Enqueue(1);
        q2_.Enqueue(2);
        q2_.Enqueue(3);
      }
    
      // By default, TearDown() inherits the behavior of
      // QuickTest::TearDown().  As we have no additional cleaning work
      // for QueueTest, we omit it here.
      //
      // virtual void TearDown() {
      //   QuickTest::TearDown();
      // }
    
      Queue<int> q0_;
      Queue<int> q1_;
      Queue<int> q2_;
    };
    
    
    // Now, let's write tests using the QueueTest fixture.
    
    // Tests the default constructor.
    TEST_F(QueueTest, DefaultConstructor) {
      EXPECT_EQ(0u, q0_.Size());
    }
    
    // Tests Dequeue().
    TEST_F(QueueTest, Dequeue) {
      int* n = q0_.Dequeue();
      EXPECT_TRUE(n == nullptr);
    
      n = q1_.Dequeue();
      EXPECT_TRUE(n != nullptr);
      EXPECT_EQ(1, *n);
      EXPECT_EQ(0u, q1_.Size());
      delete n;
    
      n = q2_.Dequeue();
      EXPECT_TRUE(n != nullptr);
      EXPECT_EQ(2, *n);
      EXPECT_EQ(1u, q2_.Size());
      delete n;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    QueueTest类继承自QuickTest,并重写SetUp方法,在SetUp方法里调用QuickTest的父类方法,然后对Queue队列做初始化,

    TEST_F(QueueTest, DefaultConstructor) {
      EXPECT_EQ(0u, q0_.Size());
    }
    
    • 1
    • 2
    • 3

    这里判断q0队列大小是否为0u

    TEST_F(QueueTest, Dequeue) {
      int* n = q0_.Dequeue();
      EXPECT_TRUE(n == nullptr);
    
      n = q1_.Dequeue();   //先出列判断是否为nullptr,在判断数据是否为1,最后判断队列大小是否为0,最后delete删除n
      EXPECT_TRUE(n != nullptr);
      EXPECT_EQ(1, *n);
      EXPECT_EQ(0u, q1_.Size());
      delete n;
    
      n = q2_.Dequeue();
      EXPECT_TRUE(n != nullptr);
      EXPECT_EQ(2, *n);
      EXPECT_EQ(1u, q2_.Size());
      delete n;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    q0_.Dequeue队列本来大小为0,出列的数据为nullptr

  • 相关阅读:
    立体仓库-提升机保养程序
    “豫”见超融合,私有云浪潮开启新一线
    【相同数字的积木游戏1】python实现-附ChatGPT解析
    HC32 IIC/I2C读写
    go-carbon v2.2.13 发布,轻量级、语义化、对开发者友好的 Golang 时间处理库
    53、springboot对websocket的支持有两种方式-------1、基于注解开发 WebSocket ,简洁实现多人聊天界面
    Jrebel使用,解决可能出现的JRebel-JVMTI [FATAL] Couldn‘t write to C:\Users\异常
    Revit中墙体绘制的小技巧?CAD识别墙体快速生成
    uniapp升级中心的如何创建和使用
    大一作业HTML网页作业:简单的旅游 1页 (旅游主题)
  • 原文地址:https://blog.csdn.net/weixin_45312249/article/details/134072163