织梦CMS - 轻松建站从此开始!

罗索

Gtest使用笔记

jackyhwei 发布于 2013-04-18 19:46 点击:次 
The msvc directory contains two Visual Studio Solutions, gtest.sln and gtest-md.sln. Choose the former if your project’s code generation will use MTd for debug mode and MT for release mode. Choose the latter if your project’s code generation will u
TAG:

项目主页:http://code.google.com/p/googletest/

详细教程:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html

几点说明:

1. vs2010可参考http://www.cnblogs.com/SelaSelah/archive/2012/04/11/2442525.html 将待测模块、主入口、单元测试分别建立工程,根据设置的工程确定,生成解决方案的同时就进行了单元测试,同时不影响主入口的使用。

2.测试项目的路径配置可参考http://blog.afonseca.org/2010/09/20/gtest_vs_tutorial/

a. The msvc directory contains two Visual Studio Solutions, gtest.sln and gtest-md.sln. Choose the former if your project’s code generation will use MTd for debug mode and MT for release mode. Choose the latter if your project’s code generation will use MDd for debug mode and MD for release mode.*

b. For this tutorial, Let’s create a Visual studio project “test” and a source file named “gtest_test.cpp”. Since we are using gtest.sln, the project runtime library is set MTd for debug mode and MT for release mode. If you don’t use the same run-time library setup used to build Gtest with your project’s runtime library, you will get linking errors. (测试工程的运行时必须与编译gtest的运行时完全一致)

c. Next, go to Configuration Properties => C/C++ => General => Additional Include Directories and add the Gtest’s include directory path (In my computer, the include directory path is “D:\gtest-1.5.0\gtest-1.5.0\include”.) and click Apply.(将gtest的include目录添加到工程的include中)

d.To set the Gtest library path into the project for debug mode, locate the configuration drop-down menu once again but this time choose “Debug” and click apply. Next, go to Configuration Properties => Linker => General => Additional Library Directories and add the debug library path (The debug and release library path were created when you build the Gtest library. In my computer, the directory path is “D:\gtest-1.5.0\gtest-1.5.0\msvc\gtest\Debug”.) (将编译产生lib文件的debug目录添加到依赖 库目录中,当然可以拷贝到自己喜欢的其他目录,这一步只是添加目录,添加.lib文件没有用,相当于没添加,我就在这里犯了低级错误)

e.To add the Gtest library into the debug mode project’s configuration, go to Configuration Properties => Linker => Input => Additional Dependencies and add “gtestd.lib”. If you are not using a main function (like in our project) add “gtest_maind.lib”;(添加具体的lib文件,VS不会自动去扫描lib目录的)

f.To set the Gtest library path into the project for release mode, locate the configuration drop-down menu once again but this time choose “Release” and click apply. Next, go to Configuration Properties => Linker => General => Additional Library Directories and add the release library path (For this tutorial, the directory path is “D:\gtest-1.5.0\gtest-1.5.0\msvc\gtest\Release”.)

g.To add the Gtest library into the release mode project’s configuration, go to Configuration Properties => Linker => Input => Additional Dependencies and add “gtest.lib”. If you are not using a main function (like in our project) add “gtest_main.lib”; click OK and Apply.

测试代码

#include <gtest/gtest.h>

int add(int a, int b);

int add(int a, int b)

{

return a+b;

}

TEST(FooTest, HandleNoneZeroInput)

{

EXPECT_EQ(2, add(4, 10));

EXPECT_EQ(6, add(30, 18));

}

 

int main(int argc, char* argv[]) 

{

testing::InitGoogleTest(&argc,argv);

           RUN_ALL_TESTS();

          std::cin.get();

          return 0;

}

(gylxue)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201304/16585.html]
本文出处:百度博客 作者:gylxue 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容