Download: http://code.google.com/p/googlemock/downloads/list
Install: Decompress the downloaded package and run the following command in the decompressed directory:
./Configure
Then execute:
Make
Note that this make will not compile Google Mock's own test cases. To compile them, go to the "make" subdirectory in the decompressed directory and execute:
Make gmock_test
Then we can see that a gmock_test executable file is generated under this directory. When we execute this executable file, we can see the execution result of the Google mock test case (it should be successful, if any fails, the makefile should be modified to make it successful ):
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.
Other records:
(1) The Google mock object will check whether the execution result of the mock function is consistent with expectation during analysis. If the mock object persists, the final check will not happen. Therefore, if you create a mock object in the heap, you 'd better use the memory leak check tool to determine whether your test has completed the mock object destruction.
(2) Google mock requires that expected results be set before the mock function is called; otherwise, the behavior is "undefined.
(3) Use the expect_call () macro to set the expected results of a mock function call. Its syntax is:
Expect_call (mock_object, method (matchers ))
. Times (cardinality)
. Willonce (Action)
. Willrepeatedly (action );
The first parameter is the mock object, and the second parameter is the mock function name and its parameters. The two are separated by commas (rather than dots). As to why, there is only one answer: technical reasons.
Macros can be followed by several statements to provide more information about the expected results of mock function calls. This style of syntax is called domain-specific language (DSL) by some people ).
(4) There are some concepts that may seem unfamiliar to Google mock at the first time, such as matcher, cardinality, etc. these concepts are described in detail in the gmock manual, and some Chinese documents can be found online. In order to truly use gmock, you must understand the meanings of these concepts.
(5) A mock function with a return value of the bool type returns false by default! Therefore, if you do not set the default return value of the function, your test function may be executed only a part after it is called (for example, your test function calls a mock function, when the function returns false, it will no longer execute the subsequent code), so that your test will not overwrite all the code of a function, which is not your favorite, remember.
(6) When the mock object is destructed, the "expectation" test will be executed. Therefore, if your mock object has no destructor (for example, you created a mock object in the heap, but forgot to delete it.
. Some people say that the memory check tool can be used to prevent this, but you must know that there is no tool to ensure 100% reliability), the test will not be executed. If you are worried about this situation, you can take the initiative to test:
Mock: verifyandclearexpectations (& mock_object):
This method returns a bool value to show whether the check is successful (true indicates success ). Therefore, you can nest it in expect_true () or assert_true () to test the test result.
(7) There is a Python script fuse_gmock_files.py in the "scripts" subdirectory under the decompressed directory of the gmock installation package. This script is generally unavailable and is used to merge Google mock files. Google mock contains N files. If you want to expand its functions, You need to modify the source code of some files. If you copy Google mock to another computer, it may be difficult to modify the source code of many files from the beginning, so the script fuse_gmock_files.py can combine the original n multiple files of Google mock into three files: gtest/gtest. h, gmock/gmock. H and gmock-gtest-all.cc. This makes it much easier.
(8) Google mock has three (main) guidance documents, whose difficulty is from low to high: fordummies, cheatsheet, and cookbook. Therefore, if you are new to gmock, you should learn the three documents in sequence.