Google Test Tutorial
1. 简介(Introduction)
google开发的测试框架
2. 术语(Nomenclature)
Test Case:一组相关的测试,GoolgeTest
Test Suit: 一些出版物、教科书、包括国际软件测试认证委员会资料使用的术语
GoogleTest在逐渐使用TestSuit代替TestCase
3. 基本概念(Basic Concepts)
- 使用GoogleTest从写断言(assertions)开始
- 断言检查条件是否为真
- 断言的结果可以是success(成功), nonfatal failure(非致命错误), fatal failure(致命错误)
- fatal failure会中止当前的函数;
- test suite(测试套件)包含一个或多个测试,将相关的测试组织在一起,可以体现测试代码的良好的组织结构
- test suite中的多个test需要共享对象或者子程序时,可以将他们放在test fixture类里
- 一个测试程序可以包含多个test suite
4. 断言(assertions)
通过对类或函数的行为进行断言来对其进行测试
断言如果失败,将打印一些信息,包括源文件、行号以及错误信息,也可以追加自定义的信息(<<)
ASSERT_*
失败时产生致命错误,程序中断
EXPECT_*
失败时产生非致命错误,程序继续
5. 创建一个test
- 使用test宏定义一个函数,没有返回值
- 在函数体中添加assertion语句
- 所有的测试通过即测试成功
TEST(TestSuiteName, TestName)
{
... test body ...
}
参数1是测试套件的名字,参数2是测试名字(描述一组测试)
参数必须是有效的c++标识符,并且不能包含下划线(_)
例如,定义一个函数
int Factorial(int n); // Returns the factorial of n
创建一个测试套件来测试它:
// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(Factorial(0), 1);
}
// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(Factorial(1), 1);
EXPECT_EQ(Factorial(2), 2);
EXPECT_EQ(Factorial(3), 6);
EXPECT_EQ(Factorial(8), 40320);
}
6. 创建一个Test Fixtures
Test Fixtures,多个测试共享相同的数据
- 继承testing::Test,成员访问权限为protected
- 在类内生命任意要使用的对象
- 如有需要,声明默认构造函数或者override SetUp函数
- 如有需要,声明析构函数或者override TearDown函数
- 如有需要,定义子程序
使用Test fixture的宏为TEST_F(),其中F代表fixture
TEST_F(TestFixtureClassName, TestName) {
... test body ...
}
例如:一个待测试的Queue类,接口如下
template <typename E>
class Queue
{
public:
Queue();
void Enqueue(const E& ele);
E* Dequeue(); // returns null if queue is empy
size_t size() const;
...
};
首先定义一个test fixture类,给它一个名字FooTest,Foo为被测试的类
class QueueTest : public testing::Test
{
protected:
QueueTest()
{
q1_.Enqueue(1);
q2_.Enqueue(2);
q2_.Enqueue(3);
}
Queue<int> q0_;
Queue<int> q1_;
Queue<int> q2_;
};
接下来使用TEST_F()和上述fixture写测试代码
TEST_F(QueueTest, IsEmptyInitially)
{
int* n = q0_.Dequeue();
EXPECT_EQ(q0_.size(), 0);
}
TEST_F(QueueTest, DequeueWorks)
{
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;
}
7. 测试调用
GoogleTest会隐式的注册TEST()和TEST_F(),不需要手动的再一次列出所定义的test
通过调用RUN_ALL_TESTS()运行所有的test,成功返回0,否则返回1
RUN_ALL_TESTS();
8. Main函数
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
InitGoogleTest通过命令行参数控制测试程序的行为
多数情况下可以不用写main函数,GoogleTest提供了gtest_main
9. 在visual studio中运行googletest
- clone GoogleTest
- cd到目录下,
- 新建build文件夹,cd到build文件夹
cmake .. -G "Visual Studio 14 2015"
- 打开build目录下的solution文件
- 编译googletest
- 在自己的project中,右键project选择properties
- 添加include directories
C/C++ -> General -> Additional Include Directories -> Edit
添加include路径, “d:\googletest-main\googletest\include”
- 添加library路径
Linker -> Additional Library Directories -> Edit
D:\googletest-main\build\lib\Debug
- 添加连接依赖
Linker -> Input ->Additional Dependencies -> Edit
添加gtest.lib, gtest_main.lib
- 运行时库
C/C++ -> Code Generation ->Runtime Library -> Select “Multi-threaded Debug (/MTd)”
- 编译运行
测试通过,得到类似这样的打印结果