gtest单元测试示例

原创
2019/11/29 15:08
阅读数 280
// GTestSimple.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <gtest\gtest.h>

// ASSERT_* 系列的断言,当检查点失败时,退出当前函数(注意:并非退出当前案例)。
// EXPECT_* 系列的断言,当检查点失败时,继续往下执行。
// 常用断言参考:https://www.cnblogs.com/coderzh/archive/2009/04/06/1430364.html

//常用断言示例(ASSERT_TRUE同理)
TEST(FooAdd, HandleNoneZeroInput)
{
	auto add = [](int x, int y)
	{
		return x + y;
	};

	EXPECT_EQ(14, add(4, 10));
	EXPECT_EQ(48, add(30, 18));

	int val1[3] = { 1, 3, 5 };
	int val2[3] = { 2, 4, 6 };
	int ret[3] = { 3, 6, 10 };
	for (int i = 0; i < 3; i++)
	{
		//输出上下文调试信息
		//EXPECT_EQ(ret[i], add(val1[i], val2[i])) << "i=" << i;
		ASSERT_EQ(ret[i], add(val1[i], val2[i])) << "i=" << i;
	}
	
	//如果上面选择EXPECT_EQ,那么下面一行可以被执行到;
	//如果上面选择ASSERT_EQ,将直接退出此测试用例函数,下面一行不会被执行
	ASSERT_EQ(31, add(30, 0));
}

//boolean变量比较
TEST(NormalAssert, assert_boolean)
{
	auto is_greator = [](int x, int y)
	{
		return x > y;
	};

	EXPECT_TRUE(10 > 5);
	EXPECT_FALSE(10 < 5);
	EXPECT_PRED2(is_greator, 10, 5); //PRED?表示后续带多少个参数,当表达式返回false的时候会打印输入的参数
}

//数据值比较
TEST(NormalAssert, assert_number)
{
	EXPECT_EQ(3, 3); // 3 == 3
	EXPECT_NE(3, 5); // 3 != 5
	EXPECT_LT(5, 6); // 5 < 6
	EXPECT_LE(5, 5); // 5 <= 5
	EXPECT_GT(6, 5); // 6 > 5
	EXPECT_GE(6, 6); // 6 >= 6
}

//浮点数比较
TEST(NormalAssert, assert_float)
{
	EXPECT_FLOAT_EQ(10.5, 10.5000);
	EXPECT_DOUBLE_EQ(10.5, 10.5000);
	EXPECT_NEAR(10.5, 10.51, 0.01);  //返回true,误差在0.01之内
	//EXPECT_NEAR(10.5, 10.511, 0.01); //返回false,误差大于0.01
	//EXPECT_PRED_FORMAT2(testing::FloatLE, 10.50, 10.51);   //比较两个浮点数
	//EXPECT_PRED_FORMAT2(testing::DoubleLE , 10.50, 10.51); //浮点数的比较gtest并没有提供类似EXPECT_NE/LT/LE的宏
}

//字符串比较
TEST(NormalAssert, assert_string)
{
	char* szCoderZh = "abcde";
	wchar_t* wszCoderZh = L"abcde";
	std::string strCoderZh = "abcde";
	std::wstring wstrCoderZh = L"abcde";
	EXPECT_STREQ(szCoderZh, "abcde");
	EXPECT_STREQ(wszCoderZh, L"abcde");
	EXPECT_STREQ(strCoderZh.c_str(), "abcde");
	EXPECT_STREQ(wstrCoderZh.c_str(), L"abcde");
}

//直接返回成功或失败
TEST(NormalAssert, assert_return)
{
	//ADD_FAILURE() << "Sorry"; // None Fatal Asserton,继续往下执行
	//FAIL() << "Sorry, FAIL exit"; // Fatal Assertion,不再继续执行
	SUCCEED() << "I am OK !"; //SUCCEED()不会中断流程,也不会打印后面的字符串
	//ADD_FAILURE() << "Sorry"; //SUCCEED()之后仍然会执行到这里
	//EXPECT_EQ(7, 6);
}

//诊断和捕获异常
TEST(NormalAssert, assert_exception)
{
	auto dev = [](int x, int y)
	{
		if (y == 0) {
			throw std::runtime_error("除数不能为零!");
		}
		if (y < 0) {
			throw std::out_of_range("除数不能为负数!");
		}
		return x / y;
	};

	EXPECT_THROW(dev(5, 0), std::runtime_error); //表达式抛出执行异常类型std::runtime_error
	EXPECT_ANY_THROW(dev(10, -2)); //表达式抛出任意异常类型
	EXPECT_NO_THROW(dev(5, 1)); //表达式不会抛出异常
}

//死亡测试,名称固定为*DeathTest,gtest会自动检查测试用例优先执行
TEST(FooDeathTest, Demo)
{
	auto Dead = []()
	{
		int *pInt = 0;
		*pInt = 42;
	};

	//EXPECT_DEATH(Dead(), "") << "EXPECT Dead";
	//ASSERT_DEATH(Dead(), "") << "ASSERT Dead";

	//执行ASSERT_DEATH()之后下面就不会再执行到
	//EXPECT_EQ(3, 1) << "After Dead...";
}

int _tmain(int argc, _TCHAR* argv[])
{
	//禁止测试用例抛出异常
	testing::GTEST_FLAG(catch_exceptions) = 1;

	//指定输出测试结果到xml文件
	testing::GTEST_FLAG(output) = "xml:";

	//指定过滤器,只运行FooAdd测试组件中的所有测试用例
	//testing::GTEST_FLAG(filter) = "FooAdd.*";

	//指定过滤器,只运行NormalAssert组件
	//testing::GTEST_FLAG(filter) = "NormalAssert.*";

	//指定运行特所的测试用例
	//testing::GTEST_FLAG(filter) = "NormalAssert.assert_string";

	//使用命令行参数需要加上前缀--gtest_,使用:分割多个参数
	//foo_test --gtest_filter=*Null*:*Constructor*

	testing::InitGoogleTest(&argc, argv);
	RUN_ALL_TESTS();

	system("pause");
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部