gtest初识_tests strength

gtest初识_tests strengthgtest初识总结本文以结合gtestgithub内容进行学习gtest。gtestgithub地址gtest编译g++xx.cppxx.h-lgtest-lpthread-omaingtest编写创建测试的一个简易的步骤:1.使用TEST()宏来定义和命名测试函数,这些是不返回值的普通C++函数。2.在此函数中,与要包含的任何有效C++语句一起使用各种g…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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

#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

#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

#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

#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

运行效果如下:

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中的用例代码

// EXPECT_EQ(1, Factorial(-5)) << "this sunrise test"; //后面的信息在失败的情况下输出到终端
EXPECT_EQ(-1, Factorial(-5)) << "this sunrise test";

运行效果:

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类

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_;
};

测试用例

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.net/187784.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 系统分析师零散知识点「建议收藏」

    系统分析师零散知识点「建议收藏」数据库连接池技术是指在系统初期或者初次使用时,完成数据库的连接,以后不再释放此连接,在处理后面的请求时,反复使用这些已经建立的连接。这种方式可以大大减少数据库的处理时间,有利于提高系统的整体性能、可测量性和扩展性。应用服务器的高速缓存?在应用服务器中有页面的缓存和数据库的缓存。页面的缓存是指将特定的URL对应的页面在缓存中予以记录,以便在

    2022年5月24日
    84
  • 研究学习之java使用selenium教程[通俗易懂]

    研究学习之java使用selenium教程[通俗易懂]提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结前言提示:这里可以添加本文要记录的大概内容:例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。提示:以下是本篇文章正文内容,下面案例可供参考一、pandas是什么?示例:pandas是基于NumPy的一种工具,该工具是为了解决数据分析任务而创建的。二、使用步骤1.引入库代码

    2022年6月28日
    38
  • 安装keil5(MDK)及导入pack包教程

    安装keil5(MDK)及导入pack包教程首先说明的是我安装的Keil版本为KeilV5.29.0.01、安装软件右键管理员权限运行安装包设置安装路径以及pack的存放路径随意输入FirstName和E-mail安装驱动完成软件安装,然后可以先关闭弹出的PackInstall(一会再添加)2、激活一下软件打开桌面的KeilFile→LicenseManagement(我之前注册过了)右…

    2022年5月27日
    2.0K
  • 网页游戏开发入门教程二(游戏模式+系统)[通俗易懂]

    网页游戏开发入门教程二(游戏模式+系统)[通俗易懂]一、游戏模式目前webgame游戏模式大体上可以分为以下四类:1、玩家拥有一个城市,不断的升级城市内建筑,建筑可以自动获得物资,可以生产军队,军队之间进行对比数值的战斗。这里我简单的称为Ogame

    2022年8月1日
    3
  • UVa 884 – Factorial Factors

    UVa 884 – Factorial Factors

    2021年12月16日
    37
  • JVM异常FGC问题查找过程总结

    JVM异常FGC问题查找过程总结问题现象及分析可能原因分析手动重现异常大对象问题流量暴涨CPU资源被其他进程占用内存被其他进程占用的情况swap分区问题数据库连接异常堆文件分析shell脚本JVM问题排查总结其他遗留的问题前段时间线上的zzuser的服务模块出现大量的异常FGC情况,经过大量排查工作,最后锁定是因为一个sql的大查询导致的。这也给了我非常大的教训,同时我在这次问题的排查过程中也获益匪浅,

    2022年6月19日
    31

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号