Configuration and entry example test of gtest under vs2013

Original link: http://blog.csdn.net/ruyueyini/article/details/47448211

Configuration steps

  1. Download gtest first and unzip it. Download address: https://github.com/google/googletest
  2. Open the gtest--msvc--gtest project with vs2013 (double-click gtest), the following "One-Way Upgrade" window will pop up, click "OK", the "Migration Report" will pop up, and close the "Migration Report".
  3. Open the project gtest with vs2013; right-click on gtest -> Generate, you can generate gtestd.lib (this corresponds to Debug), and there is a gtest.lib (this corresponds to Release), which can generate corresponding files as needed. Note the configuration to check before compiling: Right-click on gtest -> Properties -> Configuration Properties -> C/C++ -> Code Generation, check the "Runtime Library" on the right, and select Multi-Thread Debugging (MTd); The configuration is the same, keep the same.
  4. Write an example configuration: 
    Build a project: 
    In VS2013, create a project, Visual C++ -> win32 console application (win32 console application), name it test, enter the wizard, and complete it directly. 
    Configuration: 
    Right-click on the project name->Properties->Configuration Properties->C/C++->General->Additional Include Directories: Add /gtest/include under the gtest library path; 
    right-click on the project name->Properties->Configuration Properties- >C/C++->Code Generation->Runtime Library: Same as the previous gtest configuration, select MTd; 
    right-click on the project name->Properties->Configuration Properties->Linker->Input->Additional Dependencies: Enter the gtest path /msvc/gtest/Debug/gtestd.lib under. 
    At this point, the configuration is complete, and you can start writing test cases.
  5. Example 
    header file fun.h
# pragma once
int fun(int a, int b);
  • 1
  • 2

Source file fun.cpp

#include "fun.h"  
#include <iostream>  
using namespace std;

int fun(int a, int b)
{
    return (a - b);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Source file test.cpp

//#include "stdafx.h"   VS2013 中不需要此句,详见第6条
#include "gtest/gtest.h"  
#include  "fun.h"
#include <tchar.h>   //若不包含,main中参数会报错

TEST(fun, case1)
{
    EXPECT_LT(-2, fun(1, 2));
    EXPECT_EQ(-1, fun(1, 2));
    ASSERT_LT(-2, fun(1, 2));
    ASSERT_EQ(-1, fun(1, 2));
}

int _tmain(int argc, _TCHAR* argv[])
{
    //多个测试用例时使用,如果不写,运行RUN_ALL_TESTS()时会全部测试,加上则只返回对应的测试结果  
    //testing::GTEST_FLAG(filter) = "test_case_name.test_name";
    //测试初始化
    testing::InitGoogleTest(&argc, argv);
    //return RUN_ALL_TESTS();
    RUN_ALL_TESTS();
    //暂停,方便观看结果,结果窗口将会一闪而过  
    system("PAUSE");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

Result: 
这里写图片描述 
6. About stdafx.h 
vs2013 already includes stdafx.h, if it is added repeatedly in the source file, an error will be reported. 
这里写图片描述

7 运行库版本不同导致链接.LIB静态库时发生重复定义问题的一个案例分析和总结 
参考http://www.cnblogs.com/waytofall/archive/2012/05/11/2496360.html 
这里写图片描述 
一般情况下不会出错,不需要自己去配置或者寻找;我在配置的时候一直报错,找不到libcpmtd.lib文件,为此查找了很多种方法均不好使,最后重装了vs2013好使了,可能是安装的问题;看到网上说还有一种原因是gtest编译库的方式与实例编译的方式不同,就是前面第三步中配置MTd的地方,最好保持一致。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325936114&siteId=291194637