现在的位置: 首页 > 综合 > 正文

使用VS2005建立最精简Google C++ Test项目示例(教程)

2013年01月10日 ⁄ 综合 ⁄ 共 822字 ⁄ 字号 评论关闭
工程代码下载

详细步骤如下:

1,编译生成gtest.lib和gtestd.lib静态库,分别对应Release和Debug版本.


(附件中有)

2,在VS2005的工程属性中包含gtest的include和lib目录.(附件中有)

3,使用VS2005建立空项目,工程属性的设置:
 


工程属性设置

Release

Debug

C/C++->代码生成->运行时库

多线程(/MT)

线调试(/MTd)

C/C++->代码生成->基本运行时检查

默认值

两者(/RTC1,等同于 /RTCsu)

链接器->输入->附加依赖项

Gtest.lib

Gtestd.lib

4,输入MyMath.h和MyMath.cpp源文件如下:
MyMath.h:

  1. class MyMath
  2. {
  3. public:
  4.     static int Add(int num1, int num2);
  5. };

MyMath.cpp:

  1. #include "MyMath.h"
  2. int MyMath::Add(int num1, int num2)
  3. {
  4.     return num1 + num2;
  5. }

5,建立测试文件MyMathTest.cpp如下:

  1. #include "MyMath.h"
  2. #include <gtest/gtest.h>
  3. TEST(MyMathTest, Positive)
  4. {
  5.     EXPECT_EQ(3, MyMath::Add(1, 2));
  6. }

6,建立主执行文件main.cpp如下:

  1. #include <iostream>
  2. #include <gtest/gtest.h>
  3. int main(int argc, char **argv) {
  4.     std::cout << "Running main() from gtest_main.cc/n";
  5.     testing::InitGoogleTest(&argc, argv);
  6.     return RUN_ALL_TESTS();
  7. }

7,编译执行即可,输出如下:
 

抱歉!评论已关闭.