Gtest使用笔记
项目主页: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. Themsvc 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;
}
本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。