something about Google Mock

来源:互联网 发布:好视力眼贴 知乎 编辑:程序博客网 时间:2024/04/29 21:18

下载:http://code.google.com/p/googlemock/downloads/list

 

安装:将下载的压缩包解压出来,到解压目录下执行:

./configure

然后执行:

make

注意,这个make不会编译Google Mock自己的测试用例。要编译它们,需要到解压目录下的“make”子目录下,执行:

make gmock_test

然后就会看到该目录下生成了一个可执行文件 gmock_test ,执行这个可执行文件,就可以看到Google Mock测试用例的执行结果(应该是成功的,如果有失败,则应修改Makefile使之成功):

 

 

Running main() from gmock_main.cc

[==========] Running 13 tests from 3 test cases.

[----------] Global test environment set-up.

[----------] 6 tests from InitGoogleMockTest

[ RUN      ] InitGoogleMockTest.ParsesInvalidCommandLine

[       OK ] InitGoogleMockTest.ParsesInvalidCommandLine (0 ms)

[ RUN      ] InitGoogleMockTest.ParsesEmptyCommandLine

[       OK ] InitGoogleMockTest.ParsesEmptyCommandLine (0 ms)

[ RUN      ] InitGoogleMockTest.ParsesSingleFlag

[       OK ] InitGoogleMockTest.ParsesSingleFlag (0 ms)

[ RUN      ] InitGoogleMockTest.ParsesUnrecognizedFlag

[       OK ] InitGoogleMockTest.ParsesUnrecognizedFlag (0 ms)

[ RUN      ] InitGoogleMockTest.ParsesGoogleMockFlagAndUnrecognizedFlag

[       OK ] InitGoogleMockTest.ParsesGoogleMockFlagAndUnrecognizedFlag (0 ms)

[ RUN      ] InitGoogleMockTest.CallsInitGoogleTest

[       OK ] InitGoogleMockTest.CallsInitGoogleTest (0 ms)

[----------] 6 tests from InitGoogleMockTest (1 ms total)

 

[----------] 6 tests from WideInitGoogleMockTest

[ RUN      ] WideInitGoogleMockTest.ParsesInvalidCommandLine

[       OK ] WideInitGoogleMockTest.ParsesInvalidCommandLine (0 ms)

[ RUN      ] WideInitGoogleMockTest.ParsesEmptyCommandLine

[       OK ] WideInitGoogleMockTest.ParsesEmptyCommandLine (0 ms)

[ RUN      ] WideInitGoogleMockTest.ParsesSingleFlag

[       OK ] WideInitGoogleMockTest.ParsesSingleFlag (0 ms)

[ RUN      ] WideInitGoogleMockTest.ParsesUnrecognizedFlag

[       OK ] WideInitGoogleMockTest.ParsesUnrecognizedFlag (0 ms)

[ RUN      ] WideInitGoogleMockTest.ParsesGoogleMockFlagAndUnrecognizedFlag

[       OK ] WideInitGoogleMockTest.ParsesGoogleMockFlagAndUnrecognizedFlag (0 ms)

[ RUN      ] WideInitGoogleMockTest.CallsInitGoogleTest

[       OK ] WideInitGoogleMockTest.CallsInitGoogleTest (0 ms)

[----------] 6 tests from WideInitGoogleMockTest (0 ms total)

 

[----------] 1 test from FlagTest

[ RUN      ] FlagTest.IsAccessibleInCode

[       OK ] FlagTest.IsAccessibleInCode (0 ms)

[----------] 1 test from FlagTest (0 ms total)

 

[----------] Global test environment tear-down

[==========] 13 tests from 3 test cases ran. (1 ms total)

[  PASSED  ] 13 tests.

 

 

 

 

其他记录:

(1)Google Mock对象在析构的时候会检查mock函数的执行结果是否与预期相符,如果mock对象一直存在,最终的检查就不会发生。因此,如果你在堆(heap)中创建mock对象的话,最好使用内存泄漏检查工具来确定你的test是否做好了mock对象销毁工作。

(2)Google Mock要求在调用mock函数之前设置好预期的结果,否则其行为就是“未定义”的。

(3)使用EXPECT_CALL() 宏来设置一个mock函数调用的预期结果。其语法为:

 

EXPECT_CALL(mock_object, method(matchers))

.Times(cardinality)

.WillOnce(action)

.WillRepeatedly(action);

 

 

第一个参数是mock对象,第二个参数是mock函数名及其参数。二者中间是以逗号(而不是点号)分隔的,至于为什么是这样,回答只有一个:技术原因。

宏后面还可以紧跟若干语句,以提供对mock函数调用的预期结果的更多信息。这种风格的语法被一些人称作是“特定领域语言”(Domain-Specific Language,DSL)。

(4)Google Mock中有一些可能初次见到时感觉比较陌生的概念,例如 matcher,cardinality,etc.  这些概念在gmock的手册中都有详细说明,网上也可以找到一些中文文档,为了能真正用起来gmock,一定要看清了这些概念的含义。

(5)一个返回值为 bool 类型的mock函数默认是返回 false 的!所以,如果你没有设置函数的默认返回值,你的test函数被调用了之后很可能只执行了一部分(例如,你的test函数调用了一个mock函数,而该函数在返回 false 时,将不再继续执行其后的代码),这样你的test将不能覆盖某函数的全部代码,而这并不是你乐见的,切记。

(6)在mock对象析构时,对“期望”的检验才会被执行,所以,如果你的mock对象一直没有析构(例如,你在堆中创建了一个mock对象,却忘了删除它。有人说可以用内存检查工具来防止这一点,但是要知道没有工具是可以保证100%可靠的),则检验一直不会被执行。如果你担心这种情况,就可以主动进行检验:

Mock::VerifyAndClearExpectations(&mock_object):

该方法返回一个bool值,以显示检验是否成功(true代表成功)。所以,你可以把它嵌套在EXPECT_TRUE()或者ASSERT_TRUE()里面来测试检验结果。

(7)gmock安装包解压目录下的“scripts”子目录下,有一个Python脚本fuse_gmock_files.py,这个脚本一般用不到,它是用来合并Google Mock的文件的。Google Mock包含N多文件,如果你要自己扩充它的功能,那么你就需要自行修改某些文件的源码,如果你把Google Mock拷贝到另一台计算机上,从头开始修改很多个文件的源码可能就很麻烦,所以脚本fuse_gmock_files.py就可以将Google Mock原来的N多文件合并为三个文件:gtest/gtest.h,gmock/gmock.h 和 gmock-gtest-all.cc。这样的话就方便多了。

(8)Google Mock有三个(主要的)指导文档,其难度由低到高分别为:ForDummies,CheatSheet,CookBook。所以,如果你刚接触gmock,应该按这个顺序依次学习那三个文档。

 

原创粉丝点击