Running gtests on your C++ programs using g++ on Ubuntu:
Install CMake:
Build it:
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
- Repository and Readme.md:
https://github.com/google/googletest - Build and install Google Test:
https://gist.github.com/massenz/41bb2c8375294f4d9927 - Primer:
https://github.com/google/googletest/blob/master/googletest/docs/Primer.md - C++ Unit Testing with Google Test Tutorial (Youtube):
https://www.youtube.com/watch?v=16FI1-d2P4E - 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
- Some more gtest samples:
https://github.com/google/googletest/blob/master/googletest/docs/Samples.md
Comments
Post a Comment