目录
- 待测文件
- 测试文件
- 小结
待测文件
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 numbersif (n <= 1) return false;// Trivial case 2: even numbersif (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 3for (int i = 3;; i += 2) {// We only have to try i up to the square root of nif (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(TestSuiteName, TestName)
代表一个测试用例,TestSuiteName
表示测试组件名,TestName
表示测试用例名TestName
. 每个测试用例,都归属一个测试组件;一个测试组件,通常包含多个测试用例.
注意:
1)用户定义的TestSuiteName
和TestName
都不应当包含下划线("_");
2)googletest 保证用户定义的测试函数只执行一次;
3)用户编写的测试,其测试结果不应依赖于测试顺序.