Skip to content

bliutwo/google_test_guide

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 

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:

  1. 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 your main() 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
    }
  2. Download these two files to the same directory as your source file:

  3. 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 called runtests, and your source file is tests.cc, you can change the last lines in CMakeLists.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)
  4. Once you've done that, you can run the following commands (requires cmake and make):

    $ 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

All of the following are from Josh Lospinoso's C++ Crash Course: A Fast-Paced Introduction:

About

A quick-start guide to incorporating Google's C++ testing/mocking frameworks into your own projects.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published