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;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章