mrfangzheng

Hope and fear are useless. Be confident, and always be prepared for the worst.
  首页  :: 新随笔  :: 联系 :: 管理

C++ UnitTest: gtest-1.3.0 + VS2008

Posted on 2010-12-21 22:18  mrfangzheng  阅读(601)  评论(0编辑  收藏  举报

参考: http://www.cnitblog.com/zouzheng/archive/2010/06/09/61614.html

gtest下载: http://googletest.googlecode.com/files/gtest-1.3.0.zip 

已编译: /Files/mrfangzheng/gtest-include-lib.zip

  1. 下载解压打开msvc > gtest.sln编译, 我们需要
    1. gtest-1.3.0\msvc\Debug\gtestd.lib
    2. gtest-1.3.0\include
  2. 新建Win32 Console Application, 修改项目属性
    C/C++
      General > Addtional Include Directories > gtest-1.3.0\include
      Code Generation > Runtime Library > Multi-threaded Debug (/MTd)
    Linker
      Addtional Dependencies > gtest-1.3.0\msvc\Debug\gtestd.lib
  3. 测试文件如下
    #include "stdafx.h"
    #include <gtest/gtest.h>


    int Foo(int a, int b)
    {
        
    if (a == 0 || b == 0)
        {
            
    throw "don't do that";
        }
        
    int c = a % b;
        
    if (c == 0)
            
    return b;
        
    return Foo(b, c);
    }

    TEST(FooTest, HandleNoneZeroInput)
    {
        
    EXPECT_EQ(2, Foo(410));
        
    EXPECT_EQ(6, Foo(3018));
        
    EXPECT_EQ(6, Foo(30182));
    }


    int _tmain(int argc, _TCHAR* argv[])
    {
        testing::InitGoogleTest(
    &argc, argv);
        
    return RUN_ALL_TESTS();

    }