学习使用Google Test

  1. 云栖社区>
  2. 博客>
  3. 正文

学习使用Google Test

eryar 2012-05-27 15:55:00 浏览524
展开阅读全文

主要参考: 玩转Google开源C++单元测试框架Google Test系列(gtest)之一 - 初识gtest

 

配置好VS后, 编写第一个Demo.

 

   1:  // gTest.cpp : 定义控制台应用程序的入口点。
   2:  //
   3:   
   4:  #include "stdafx.h"
   5:   
   6:  int Foo(int a, int b)
   7:  {
   8:      if (a == 0 || b == 0)
   9:      {
  10:          throw "don't do that";
  11:      }
  12:      int c = a % b;
  13:      if (c == 0)
  14:          return b;
  15:      return Foo(b, c);
  16:  }
  17:   
  18:  int Add(int a, int b)
  19:  {
  20:      return a + b;
  21:  }
  22:   
  23:  #include <gtest/gtest.h>
  24:   
  25:  TEST(FooTest, HandleNoneZeroInput)
  26:  {
  27:      EXPECT_EQ(2, Foo(4, 10));
  28:      EXPECT_EQ(6, Foo(30, 18));
  29:      //ASSERT_EQ(3, Foo(3, 6));
  30:      //ASSERT_EQ(3, Foo(3, 7));
  31:      //EXPECT_EQ(3, Foo(5, 7));
  32:  }
  33:   
  34:  TEST(AddTest, HandleNoneZeroInput)
  35:  {
  36:      EXPECT_EQ(2, Add(1, 1));
  37:      //EXPECT_EQ(2, Add(1, 2));
  38:  }
  39:   
  40:  int _tmain(int argc, _TCHAR* argv[])
  41:  {
  42:      testing::InitGoogleTest(&argc, argv);
  43:      return RUN_ALL_TESTS();
  44:  }
  45:   
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

运行结果如下图所示:

Google Test Result

网友评论

登录后评论
0/500
评论
eryar
+ 关注