[单元测试]_[VC2010使用gtest单元测试入门]


场景:

1. gtest作为C++的单元测试工具很优秀了,它集成了很多标准assert所没有的功能,比如让流程继续执行的EXPECT,只测试特定测试用例的--gtest_filter,

输出xml文件的测试报告.

2.方便的FilePath对路径操作的类和Message设置Log级别,当然还有很多方便的功能,比如设置环境变量.

using ::testing::internal::FilePath;
using ::testing::Message;

3.这里编写了一个vc2010简单的使用gtest的例子.使用gcc版本请看,用的Makefile:

http://blog.csdn.net/infoworld/article/details/20457353


代码:

// test_gtest.cpp : 定义控制台应用程序的入口点。
//
// 在 googletest 中实现单元测试,可通过 ASSERT_* 和 EXPECT_* 
// 断言来对程序运行结果进行检查。 ASSERT_* 版本的断言失败时会产生致命失败,
// 并结束当前函数; EXPECT_* 版本的断言失败时产生非致命失败,但不会中止当前函数。
// 因此, ASSERT_* 常常被用于后续测试逻辑强制依赖的处理结果的断言,
// 如创建对象后检查指针是否为空,若为空,则后续对象方法调用会失败;
// 而 EXPECT_* 则用于即使失败也不会影响后续测试逻辑的处理结果的断言,
// 如某个方法返回结果的多个属性的检查。

#include "stdafx.h"
#include <iostream>
#include "gtest/gtest.h"
using ::testing::internal::FilePath;
using ::testing::Message;


using namespace std;
//To generate the XML report, set the GTEST_OUTPUT environment variable or 
//the --gtest_output flag to the string "xml:_path_to_output_file_", 
//which will create the file at the given location. 
//You can also just use the string "xml", in which case the output can be found in the test_detail.xml file in the current directory.
int main(int argc, _TCHAR* argv[])
{
    testing::InitGoogleTest(&argc, argv);

	FilePath path(argv[0]);
    FilePath currentDir = path.GetCurrentDir();
    Message message("currentDir is: ");
    message << currentDir.ToString();
    GTEST_LOG_(INFO) << message ;
    GTEST_LOG_(INFO) << ".............." ;

	RUN_ALL_TESTS();
	system("pause");
    return 0;
}


TEST(test_main,TestBaseAssert)
{
	cout << "TestBaseAssert" << endl;
	ASSERT_TRUE(__LINE__);
	EXPECT_TRUE(__LINE__);

	
	EXPECT_FALSE(true);
	ASSERT_FALSE(false);
}

TEST(test_main,TestBinaryAssert)
{
	cout << "TestBinaryAssert" << endl;
	const char* str = "11";

	ASSERT_EQ(str,str);
	EXPECT_EQ(false,0);

	ASSERT_NE(str,str+1);
	EXPECT_NE(false,1);

	ASSERT_LT(str,str+1);
	EXPECT_LT(0x1,0x2);

	ASSERT_LE(0x1,0x2);
	EXPECT_LE(0x1,0x2);

	ASSERT_GT(0x2,0x1);
	EXPECT_GT(0x2,0x1);

	ASSERT_GE(0x2,0x1);
	EXPECT_GE(0x2,0x1);
}

TEST(test_main,TestStrAssert)
{
	cout << "TestStrAssert" << endl;
	const char* str = "11ab";
	std::string str2("11ab");
	std::string str21("11aB");
	//1.判断字符串是否相等.
	ASSERT_STREQ(str,str2.c_str());
	EXPECT_STREQ(str,str2.c_str());

	//2.字符串大小写敏感
	ASSERT_STRNE(str,str21.c_str());
	EXPECT_STRNE(str,str21.c_str());

	//3.字符串大小写不敏感
	ASSERT_STRCASEEQ(str,str21.c_str());
	EXPECT_STRCASEEQ(str,str21.c_str());

	str21.append("!");
	ASSERT_STRCASENE(str,str21.c_str());
	EXPECT_STRCASENE(str,str21.c_str());
}

注意:

1. 使用gtest需要自己先编译,下载,我用的是1.5.0. 完整源文件放在csdn download里,下边看下载链接。

http://code.google.com/p/googletest

2. 如果需要自己编译gtest,在目录src\msvc下有sln文件, 默认编译出来的是静态库,貌似动态库使用上有问题.

3. 使用gtest时可能会报错

1>msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: class std::locale::facet * __thiscall std::locale::facet::_Decref(void)" (?_Decref@facet@locale@std@@QAEPAV123@XZ) 已经在 gtestd.lib(gtest.obj) 中定义
因为C/C++->代码生成->设置了"多线程调试 (/MTd)"
而项目使用的是 多线程调试 DLL (/MDd).

解决办法:把测试项目改为 多线程调试 (/MTd) 即可,注意一点,如果把gtest编译为 多线程调试 DLL (/MDd),按CTRL+F5会在
testing::InitGoogleTest(&argc, argv); 崩溃.我估计是共享运行时库DLL的做了某些加锁操作造成的.所以还是把测试项目exe改为
多线程调试 (/MTd)好点.

3.例子输出:

2.输出
C:\workspace\script-test\test_gtest\test_gtest\Debug>test_gtest --gtest_output=x
ml
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from test_main
[ RUN      ] test_main.TestBaseAssert
TestBaseAssert
c:\workspace\script-test\test_gtest\test_gtest\test_gtest\test_gtest.cpp(35): er
ror: Value of: true
  Actual: true
Expected: false
[  FAILED  ] test_main.TestBaseAssert (16 ms)
[ RUN      ] test_main.TestBinaryAssert
TestBinaryAssert
[       OK ] test_main.TestBinaryAssert (16 ms)
[ RUN      ] test_main.TestStrAssert
TestStrAssert
[       OK ] test_main.TestStrAssert (0 ms)
[----------] 3 tests from test_main (32 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (32 ms total)
[  PASSED  ] 2 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] test_main.TestBaseAssert

 1 FAILED TEST
请按任意键继续. . .

4.完整项目下载地址,项目里包含了编译好的gtestd.lib库和头文件,偷懒的童鞋可以直接先用着。
docs目录里有gtest的一些资料.

http://download.csdn.net/download/infoworld/7539997

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peter(阿斯拉达)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值