googletest:sample1分析
待测文件
sample1 包含2个功能:
- Factorial: 求n!;
- IsPrime: 判断n是否为素数.
sample1.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);
sample1.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 if and only if 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 "sample1.h"
#include <limits.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));
EXPECT_EQ(1, Factorial(-1));
EXPECT_GT(Factorial(-10), 0);
// <TechnicalDetails>
//
// EXPECT_EQ(expected, actual) is the same as
//
// EXPECT_TRUE((expected) == (actual))
//
// except that it will print both the expected value and the actual
// value when the assertion fails. This is very helpful for
// debugging. Therefore in this case EXPECT_EQ is preferred.
//
// On the other hand, EXPECT_TRUE accepts any Boolean expression,
// and is thus more general.
//
// </TechnicalDetails>
}
// 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
对于函数Factorial
的测试,定义了测试组件FactorialTest
,而每个测试用例对应一个名称. 分别有:
- Negative: 代表输入的n为负数;
- Zero:代表输入的n为0;
- Positive:代表输入的n为正数.
数据检查:
EXPECT_EQ(1, Factorial(-5));
断言值相等,相当于非致命的assert 1 == Factorial(-5)
.
EXPECT_GT(Factorial(-10), 0);
断言大小,相当于非致命的assert Factorial(-10) > 0
. "<"断言用EXPECT_LT
.
对于函数IsPrime
的测试,定义了组件IsPrimeTest
,每个测试用例对应一个名称,可与FactorialTest
组件的测试用例名同名. 分别有:
- Negative: 代表输入的n为负数;
- Trivial: 简单的用例,通常是验证代码基本功能和边界条件;
- Positive:代表输入的n为正数.
Test,Test Case, Test Suite
GoogleTest primer描述如下:
Note: There might be some confusion arising from different definitions of the terms Test, Test Case and Test Suite, so beware of misunderstanding these.
Historically, GoogleTest started to use the term Test Case for grouping related tests, whereas current publications, including International Software Testing Qualifications Board (ISTQB) materials and various textbooks on software quality, use the term Test Suite for this.
The related term Test, as it is used in GoogleTest, corresponds to the term Test Case of ISTQB and others.
The term Test is commonly of broad enough sense, including ISTQB’s definition of Test Case, so it’s not much of a problem here. But the term Test Case as was used in Google Test is of contradictory sense and thus confusing.
GoogleTest recently started replacing the term Test Case with Test Suite. The preferred API is TestSuite. The older TestCase API is being slowly deprecated and refactored away.
So please be aware of the different definitions of the terms:
Meaning GoogleTest Term ISTQB Term Exercise a particular program path with specific input values and verify the results TEST() Test Case
大意是:历史上,GoogleTest最开始用TestCase对相关测试进行分组,而ISTQB委员会和教科书都是用的Test Suite表示这个概念. GoogleTest用Test表示对应于ISTQB的Test Case.
GoogleTest最近开始用Test Suite来替换Test Case一词. 首选API是TestSuite,旧的API TestCase开始慢慢被弃用和重构.
GoogleTest中,这是容易混淆的点.
简单说,Googletest中,以前,"Test Case = Test = TEST()";现在,"Test Case = Test Suite".
ISTQB关于Test Case定义:
A set of preconditions, inputs, actions (where applicable), expected results and postconditions, developed based on test conditions.
小结
TEST
用于用户定义测试函数,一个TEST(TestSuiteName, TestName)
代表一个测试,TestSuiteName
表示测试组件名,TestName
表示测试名. 每个测试,都归属一个测试组件;一个测试组件,通常包含多个测试.
注意:
1)用户定义的TestSuiteName
和TestName
都不应当包含下划线("_");
2)googletest 保证用户定义的测试函数只执行一次;
3)用户编写的测试,其测试结果不应依赖于测试顺序.
本文作者:明明1109
本文链接:https://www.cnblogs.com/fortunely/p/18741214
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2022-02-27 muduo笔记 线程安全相关类MutexLock, MutexLockGuard
2022-02-27 muduo笔记 标记类copyable, noncopyable
2022-02-27 muduo笔记 原子类AtomicIntegerT<T>
2022-02-27 muduo笔记 时间戳类Timestamp