前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gtest初识_tests strength

gtest初识_tests strength

作者头像
全栈程序员站长
发布2022-11-11 13:30:03
4730
发布2022-11-11 13:30:03
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

gtest初识总结

本文以结合gtest github内容进行学习gtest。

gtest github地址

gtest编译

g++ xx.cpp xx.h -lgtest -lpthread -o main

gtest编写

创建测试的一个简易的步骤:

1.使用TEST()宏来定义和命名测试函数,这些是不返回值的普通C ++函数。 2.在此函数中,与要包含的任何有效C ++语句一起使用各种googletest断言来检查值.(ASSERT_()、EXPECT_()) 3.测试的结果由断言决定; 如果测试中的任何断言失败(无论是致命的还是非致命的),或者测试崩溃,整个测试都会失败。否则,它会成功。

TEST()第一个参数是测试用例的名称,第二个参数是测试用例中的测试名称(有效的C++标识符,不应包含下划线)。 googletest按照测试用例对测试结果进行分组。

例子Demo

文件

描述

sample1.cc

待测试代码包含两个函数:1:Factorial(int n) 阶乘函数。 2:IsPrime(int n) 是否是质数

sample1.h

待测试代码头文件

simple1_unittest.cc

测试用例代码文件

main.cpp

程序入口文件

代码如下:

sample1.h

代码语言:javascript
复制
#ifndef GTEST_SAMPLES_SAMPLE1_H_
#define GTEST_SAMPLES_SAMPLE1_H_

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n);

// Returns true iff n is a prime number.
bool IsPrime(int n);

#endif  // GTEST_SAMPLES_SAMPLE1_H_

sample.cc

代码语言:javascript
复制
#include "sample1.h"

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
int Factorial(int n) {
  int result = 1;
  for (int i = 1; i <= n; i++) {
    result *= i;
  }

  return result;
}

// Returns true iff n is a prime number.
bool IsPrime(int n) {
  // Trivial case 1: small numbers
  if (n <= 1) return false;

  // Trivial case 2: even numbers
  if (n % 2 == 0) return n == 2;

  // Now, we have that n is odd and n >= 3.

  // Try to divide n by every odd number i, starting from 3
  for (int i = 3; ; i += 2) {
    // We only have to try i up to the square root of n
    if (i > n/i) break;

    // Now, we have i <= n/i < n.
    // If n is divisible by i, n is not prime.
    if (n % i == 0) return false;
  }

  // n has no integer factor in the range (1, n), and thus is prime.
  return true;
}

sample1_unittest.cc

代码语言:javascript
复制
#include <limits.h>
#include "sample1.h"
#include "gtest/gtest.h"
namespace {

// Tests Factorial().

// Tests factorial of negative numbers.
TEST(FactorialTest, Negative) {
  // This test is named "Negative", and belongs to the "FactorialTest"
  // test case.
  EXPECT_EQ(1, Factorial(-5)) << "this sunrise test"; //后面的信息在失败的情况下输出到终端
  EXPECT_EQ(1, Factorial(-1));
  EXPECT_GT(Factorial(-10), 0);

// Tests factorial of 0.
TEST(FactorialTest, Zero) {
  EXPECT_EQ(1, Factorial(0));
}

// Tests factorial of positive numbers.
TEST(FactorialTest, Positive) {
  EXPECT_EQ(1, Factorial(1));
  EXPECT_EQ(2, Factorial(2));
  EXPECT_EQ(6, Factorial(3));
  EXPECT_EQ(40320, Factorial(8));
}


// Tests IsPrime()

// Tests negative input.
TEST(IsPrimeTest, Negative) {
  // This test belongs to the IsPrimeTest test case.

  EXPECT_FALSE(IsPrime(-1));
  EXPECT_FALSE(IsPrime(-2));
  EXPECT_FALSE(IsPrime(INT_MIN));
}

// Tests some trivial cases.
TEST(IsPrimeTest, Trivial) {
  EXPECT_FALSE(IsPrime(0));
  EXPECT_FALSE(IsPrime(1));
  EXPECT_TRUE(IsPrime(2));
  EXPECT_TRUE(IsPrime(3));
}

// Tests positive input.
TEST(IsPrimeTest, Positive) {
  EXPECT_FALSE(IsPrime(4));
  EXPECT_TRUE(IsPrime(5));
  EXPECT_FALSE(IsPrime(6));
  EXPECT_TRUE(IsPrime(23));
}
} // namespace

main.cpp

代码语言:javascript
复制
#include<iostream>
#include<gtest/gtest.h>

using namespace std;

GTEST_API_ int main(int argc, char **argv) {
    printf("Running main");
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

编译命令:

g++ sample1.cc sample1.h sample1_unittest.cc main.cpp -lgtest -lpthread -o main

运行效果如下:

代码语言:javascript
复制
Running main[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
[       OK ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 6 tests.

更改FactorialTest.Negative中的用例代码

代码语言:javascript
复制
// EXPECT_EQ(1, Factorial(-5)) << "this sunrise test"; //后面的信息在失败的情况下输出到终端
EXPECT_EQ(-1, Factorial(-5)) << "this sunrise test";

运行效果:

代码语言:javascript
复制
Running main[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN      ] FactorialTest.Negative
sample1_unittest.cc:79: Failure
Value of: Factorial(-5)
  Actual: 1
Expected: -1
this sunrise test
[  FAILED  ] FactorialTest.Negative (0 ms)
[ RUN      ] FactorialTest.Zero
[       OK ] FactorialTest.Zero (0 ms)
[ RUN      ] FactorialTest.Positive
[       OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (0 ms total)

[----------] 3 tests from IsPrimeTest
[ RUN      ] IsPrimeTest.Negative
[       OK ] IsPrimeTest.Negative (0 ms)
[ RUN      ] IsPrimeTest.Trivial
[       OK ] IsPrimeTest.Trivial (0 ms)
[ RUN      ] IsPrimeTest.Positive
[       OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 5 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] FactorialTest.Negative

 1 FAILED TEST
基础语法介绍
断言

分为ASSERT_*和EXPECT_*两种类型:

ASSERT_*

EXPECT_*

致命的断言,终止当前功能(以测试用例为组)

非致命故障,不会终止当前功能

终止:是终止自身处于的那一组测试用例,如上例中的FactorialTest.Negative是一组测试。

断言详细函数
  • 基本函数,基本的真/假条件测试。

Fatal assertion

Nonfatal assertion

Verifies

ASSERT_TRUE(condition);

EXPECT_TRUE(condition);

condition is true

ASSERT_FALSE(condition);

EXPECT_FALSE(condition);

condition is false

  • 二元比较

Fatal assertion

Nonfatal assertion

Verifies

ASSERT_EQ(val1, val2);

EXPECT_EQ(val1, val2);

val1 == val2

ASSERT_NE(val1, val2);

EXPECT_NE(val1, val2);

val1 != val2

ASSERT_LT(val1, val2);

EXPECT_LT(val1, val2);

val1 < val2

ASSERT_LE(val1, val2);

EXPECT_LE(val1, val2);

val1 <= val2

ASSERT_GT(val1, val2);

EXPECT_GT(val1, val2);

val1 > val2

ASSERT_GE(val1, val2);

EXPECT_GE(val1, val2);

val1 >= val2

  • 字符串比较

Fatal assertion

Nonfatal assertion

Verifies

ASSERT_STREQ(str1, str2);

EXPECT_STREQ(str1, str2);

the two C strings have the same content

ASSERT_STRNE(str1, str2);

EXPECT_STRNE(str1, str2);

the two C strings have different contents

ASSERT_STRCASEEQ(str1, str2);

EXPECT_STRCASEEQ(str1, str2);

the two C strings have the same content, ignoring case

ASSERT_STRCASENE(str1, str2);

EXPECT_STRCASENE(str1, str2);

the two C strings have different contents, ignoring case

Test Fixtures: 为多个测试使用相同的数据配置

Fixtures 是测试中非常重要的一部分。他们的主要目的是建立一个固定/已知的环境状态以确保 测试可重复并且按照预期方式运行。

  1. 创建Fixture类继承至::testing::Test.
  2. 在类中,声明需要使用的对象
  3. 编写SetUp函数
  4. 编写TearDown函数
  5. 如果需要,请为要共享的测试定义子例程。

例子: Fixtures类

代码语言:javascript
复制
class QueueTest : public ::testing::Test {
 protected:
  void SetUp() override {
     q1_.Enqueue(1);
     q2_.Enqueue(2);
     q2_.Enqueue(3);
  }

  // void TearDown() override {}

  Queue<int> q0_;
  Queue<int> q1_;
  Queue<int> q2_;
};

测试用例

代码语言:javascript
复制
TEST_F(QueueTest, IsEmptyInitially) {
  EXPECT_EQ(q0_.size(), 0);
}

TEST_F(QueueTest, DequeueWorks) {
  // construct an instance QueueTest q;q.SetUp()
  int* n = q0_.Dequeue();
  EXPECT_EQ(n, nullptr);

  n = q1_.Dequeue();
  ASSERT_NE(n, nullptr);
  EXPECT_EQ(*n, 1);
  EXPECT_EQ(q1_.size(), 0);
  delete n;

  n = q2_.Dequeue();
  ASSERT_NE(n, nullptr);
  EXPECT_EQ(*n, 2);
  EXPECT_EQ(q2_.size(), 1);
  delete n;
  // q.TearDown()
}

用例DequeueWorks和用例DequeueWorks共用的QueueTest中的q0_,q1_,q2_,SetUp()和TearDown().

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/187784.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月29日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • gtest初识总结
    • gtest编译
      • gtest编写
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档