Instructions for Google C++ Testing and Mocking (For Dummies), Unix Edition
Keywords: quickstart, quick, start, dummies, easy, guide, test, mock, google
Motivation
Although the Googletest Primer and the Googletest README are enlightening and informative, they don't contain a step-by-step, "quick start" guide to incorporating the Google C++ testing or mocking frameworks into an existing project. Hopefully, this repository can help people get quickly started with it.
NOTE: If there are any problems or inaccurate information in this guide, please submit an issue!
Generalized Instructions
Here are some steps for incorporating Google Unit Testing (or Mocking) into a basic project:
-
Modify your source file to include the following header (for example, in
tests.cc
):#include "gtest/gtest.h"
Similarly, for mocking, include this:
#include "gmock/gmock.h"
Additional setup for mocking: Make sure to add
::testing::InitGoogleMock(&argc, argv)
to yourmain()
function as such:int main(int argc, char** argv) { ::testing::InitGoogleMock(&argc, argv); // Unit test as usual, Google Mock is initialized }
And, if you want Google Mock to throw an exception when some mock-related assertion fails, add
::testing::GTEST_FLAG(throw_on_failure) = true;
as such:int main(int argc, char** argv) { ::testing::GTEST_FLAG(throw_on_failure) = true; ::testing::InitGoogleMock(&argc, argv); // Unit test as usual, Google Mock is initialized }
-
Download these two files to the same directory as your source file:
CMakeLists.txt
CMakeLists.txt.in
- (You can save these files by right-clicking the page and clicking
Save Page As...
)
-
In
CMakeLists.txt
, modify the last three lines to contain the executables and source files that you want. For example, if you want to make an executable calledruntests
, and your source file istests.cc
, you can change the last lines inCMakeLists.txt
from this:# Now simply link against gtest or gtest_main as needed. Eg add_executable(example example.cpp) target_link_libraries(example gtest_main) add_test(NAME example_test COMMAND example)
to this, for testing:
# Now simply link against gtest or gtest_main as needed. Eg add_executable(runtests tests.cc) target_link_libraries(runtests gtest_main) # NOTE: gtest_main, NOT gmock_main! add_test(NAME example_test COMMAND runtests)
Similarly, for mocking, you can change it to this:
# Now simply link against gtest or gtest_main as needed. Eg add_executable(runtests tests.cc) target_link_libraries(runtests gmock_main) # NOTE: gmock_main, NOT gtest_main! add_test(NAME example_test COMMAND runtests)
You can also link both libraries in the same file:
# Now simply link against gtest or gtest_main as needed. Eg add_executable(runtests tests.cc) target_link_libraries(runtests gtest_main gmock_main) # NOTE: one line! add_test(NAME example_test COMMAND runtests)
-
Once you've done that, you can run the following commands (requires
cmake
andmake
):$ cmake .
$ make
If your executable were
runtests
as mentioned above, you can run:$ ./runtests
That's it! I wish somewhere it was said to do that when I was figuring this out, so hopefully this can serve that purpose for others.
Running the example test from this repository
The commands for running the example code in this directory are the following:
$ cmake .
$ make
$ ./example
More examples
- Google Test Template
- My own personal example, a solution to a Leetcode problem
- Another example, also a solution to a Leetcode problem
All of the following are from Josh Lospinoso's C++ Crash Course: A Fast-Paced Introduction:
- Google Test example
- Google Mock example (In progress...)