XCode使用googletest(包括googlemock)

1. 下载最新的googletest源代码,也可以从此处下载:https://pan.baidu.com/s/1qRn5BCpUIBz_PY0m6-7pgg

2. 解压后进入googletest/make,执行make,copy libgtest.a到/usr/local/lib,并copy include/gtest目录到/usr/local/include;

然后进入googlemock/make,执行make,copy gmock_main.a到/usr/local/lib

3.新建一个空白的c++项目,并将main.cpp里面的内容清空

4.添加路径到项目,如下图:

XCode使用googletest(包括googlemock)_第1张图片

5.添加.a文件到项目,如下图

XCode使用googletest(包括googlemock)_第2张图片

6.添加如下代码到main.cpp,直接执行:

//
//  main.cpp
//  gtestdemo
//
//  Created by [email protected] on 23/07/2018.
//  Copyright © 2018 bifang. All rights reserved.
//

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

#include 

TEST(HighestCommonDivissorTest, HandleNoneZeroInput)
{
	EXPECT_EQ(2, HighestCommonDivissor(4, 10));
	EXPECT_EQ(6, HighestCommonDivissor(30, 18));
}

7.得到如下结果:

Running main() from gmock_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from FooTest
[ RUN      ] FooTest.HandleNoneZeroInput
[       OK ] FooTest.HandleNoneZeroInput (0 ms)
[----------] 1 test from FooTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 1 test.

 

你可能感兴趣的:(杂项)