Skip to main content

googletest on Ubuntu

Running gtests on your C++ programs using g++ on Ubuntu:

Installation


Install CMake:
  • sudo apt install cmake
  • Switch to home directory and:
  • git clone --depth 1 https://github.com/google/googletest.git

Build it:
  • cd ~/googletest
  • mkdir build
  • cd build
  • cmake -Dgtest_build_samples=ON -Dgtest_build_tests=ON ~/googletest
  • make
  • make test
  • ./googlemock/gtest/sample1_unittest


Install the googletest and googlemock .a and .h files:
  • sudo cp -r -v ~/googletest/googletest/include/gtest /usr/local/include
  • sudo cp -r -v ~/googletest/googlemock/include/gmock /usr/local/include
  • sudo cp -v ~/googletest/build/googlemock/lib*.a /usr/local/lib
  • sudo cp -v ~/googletest/build/googlemock/gtest/lib*.a /usr/local/lib



TEST() and code in same file - Test Fail

To compile and run:
  • g++ 01sqrt_fail.cpp -lgtest -lpthread -o 01sqrt_fail
  • ./01sqrt_fail
Output:




TEST() and code in same file - Test Pass

To compile and run:
  • g++ 01sqrt_pass.cpp -lgtest -lpthread -o 01sqrt_pass
  • ./01sqrt_pass
Output:




TEST_F() and code in same file - Test Pass

To compile and run:
  • g++ 02bankacc_pass.cpp -lgtest -lpthread -o 02bankacc_pass
  • ./02bankacc_pass
Output:




TEST_P() and code in same file - Test Fail

To compile and run:
  • g++ 03bankacc_fail.cpp -std=c++11 -lgtest -lpthread -o 03bankacc_fail
  • ./03bankacc_fail
Output:




TEST_P() and code in same file - Test Fail and Formatted Output

To compile and run:
  • g++ 03bankacc_format.cpp -std=c++11 -lgtest -lpthread -o 03bankacc_format
  • ./03bankacc_format
Output:

References

  1. Repository and Readme.md:
    https://github.com/google/googletest

  2. Build and install Google Test:
    https://gist.github.com/massenz/41bb2c8375294f4d9927

  3. Primer:
    https://github.com/google/googletest/blob/master/googletest/docs/Primer.md

  4. C++ Unit Testing with Google Test Tutorial (Youtube):
    https://www.youtube.com/watch?v=16FI1-d2P4E

  5. StackOverflow: How to configure and setup google test framework in linux https://stackoverflow.com/questions/19810731/how-to-configure-and-setup-google-test-framework-in-linux

  6. Some more gtest samples:
    https://github.com/google/googletest/blob/master/googletest/docs/Samples.md

Link to google doc containing above blog post

Comments