1. Download googletest, which contain googletest and googlemock
: https://github.com/google/googletest
2. install both googletest and googlemock
cd googletest/build
cmake ..
sudo make install
It is expected that both gtest and gmock have been installed in /usr/local/
3.
square.h
#include <math.h> double squareroot (const double aa) { return sqrt(aa); }
test.cpp
#include "square.h" #include "gtest/gtest.h" TEST (SquareRootTest, PositiveNos) { EXPECT_EQ (18.0, squareroot (324.0)); } TEST (SquareRootTest, ZeroAndNegativeNos) { ASSERT_EQ (0.0, squareroot (0.0)); } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
g++ -pthread ./test.cpp /usr/local/lib/libgtest.a -o test
Reference:
A quick introduction to the Google C++ Testing Framework, IBM