gtest基础使用04:Google Test自带示例一:Func.

一、环境信息

  1. Visual Studio 2019
  2. Windows 10
  3. 前导文档:gtest基础使用02gtest基础使用03
  4. 在现有知识的基础上,很有必要认真学习下Google Test的示例文档,汲取示例中的单元测试思想

二、Google Test Sample01

1. 示例描述

1.1 Google Test Framework附带了10个单元测试用例,难度由浅及深。sample01主要演示了如何测试函数
1.2 sample01由三个部分组成:sample01.h , sample01.cpp , sample01UnitTest.cpp (作者对原始文档进行了命名调整,以便更好的进行说明)
1.3 sample01.cpp中撰写了待测试的函数,对应头文件 sample01.h。Factorial( )用于求一个数的阶乘,IsPrime( ) 用于判定一个数是否是素数

#include "sample01.h"

// Returns n! (the factorial of n).  For negative n and zero, 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 if and only if n is a prime number.
bool IsPrime(int n) //素数定义: 在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数
{
    // Trivial case 1: small numbers
    if (n <= 1) return false;

    // Trivial case 2: even numbers
    if (n % 2 == 0) return n == 2; //目前不清楚实现原理,当n=2 返回True;当n=4 6.., 返回False 
    //if (n % 2 == 0) return false; //wrong code 

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

1.4 sample01.h 进行了函数声明

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

// Returns true if and only if n is a prime number.
bool IsPrime(int n);

1.5 sample01.cpp 和 sample01.h 归属于项目 practice, sample01UnitTest.cpp 归属于测试项目 practice_gtest 。项目及对应测试项目的创建可以参考基础使用02,这里不再重复叙述

2. 对应的单元测试用例

2.1 在sample01UnitTest.cpp中include的是头文件 sample01.h ,而不是源文件 sample01.cpp ,因为要调用接口,而不是调用具体实现
2.2 sample01UnitTest.cpp的源码如下,对原有的注释进行了一些修改,并结合当前环境添加了一些信息
2.3 在单元测试用例中,覆盖了待测函数的各个分支。可以通过阅读源码和注释进行熟悉和理解

// Writing a unit test using Google C++ testing framework is easy as 1-2-3:

// Step 1. Include necessary header files. Don't forget gtest.h, which declares the testing framework.
// Note: For current sample, writer substitude pch.h for gtest.h!
#include "pch.h"
#include "sample01.h"

// Step 2. Use the TEST macro to define your tests.
// TEST has two parameters: the test case name and the test name. 
// You should put logically related tests into the same test case.
// The test case name and the test name should both be valid C++
// identifiers.  And you should not use underscore (_) in the names.

// Note: test case name is equal to test suit name , test name is equal to test case name.
// Note: For google test framework, name of *.h & *.cpp  must not include underscore _, or cause great failue!!

// Google Test guarantees that each test you define is run exactly
// once, but it makes no guarantee on the order the tests are
// executed.  Therefore, you should write your tests in such a way that their results don't depend on their order.

// 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));
	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) 
{
	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));
}

TEST(IsPrimeTest, PositiveMore)
{
	EXPECT_FALSE(IsPrime(2));
}

// Step 3. Call RUN_ALL_TESTS() in main().
// This runs all the tests you've defined, prints the result, and
// returns 0 if successful, or 1 otherwise.
//
// Did you notice that we didn't register the tests?  The
// RUN_ALL_TESTS() macro magically knows about all the tests we
// defined.  Isn't this convenient?

// Note : For this sample(vs 2019) , you just need open "Test resource manager" and execute the start, 
// don't need Call RUN_ALL_TESTS() in main().
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值