工程代码下载
详细步骤如下:
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:
- class MyMath
- {
- public:
- static int Add(int num1, int num2);
- };
MyMath.cpp:
- #include "MyMath.h"
- int MyMath::Add(int num1, int num2)
- {
- return num1 + num2;
- }
5,建立测试文件MyMathTest.cpp如下:
- #include "MyMath.h"
- #include <gtest/gtest.h>
- TEST(MyMathTest, Positive)
- {
- EXPECT_EQ(3, MyMath::Add(1, 2));
- }
6,建立主执行文件main.cpp如下:
- #include <iostream>
- #include <gtest/gtest.h>
- int main(int argc, char **argv) {
- std::cout << "Running main() from gtest_main.cc/n";
- testing::InitGoogleTest(&argc, argv);
- return RUN_ALL_TESTS();
- }
7,编译执行即可,输出如下: