Skip to content

Latest commit

 

History

History
180 lines (118 loc) · 3.26 KB

googletest.md

File metadata and controls

180 lines (118 loc) · 3.26 KB

Google Test

https://github.com/google/googletest

1. install

brew install googletest

1.1 Or you may want to install from source ...

build from source
  • first, do some modification...

    • CMakeList.txt, line:127, add quotes around the $(GOOGLETEST_VERSION} variable.
    set_target_properties(gtest PROPERTIES VERSION "${GOOGLETEST_VERSION}")
    cxx_library(gtest_main "${cxx_strict}" src/gtest_main.cc)
    set_target_properties(gtest_main PROPERTIES VERSION "${GOOGLETEST_VERSION}")
  • build

    cd googletest
    mkdir build
    cd build
    cmake ..
    make

2. includes & libs

$ brew --prefix googletest
/usr/local/opt/googletest
  • include
    • /usr/local/opt/googletest/include/gtest
  • lib
    • /usr/local/opt/googletest/lib/

2.1 includes & libs from source

include / lib from source
  • include
    • build/../include/gtest/
  • lib
    • build/lib/

copy to /usr/local/

cp -a ../include/gtest /usr/local/include

mkdir /usr/local/lib/gtest
cp -a lib/*.a /usr/local/lib/gtest

to check

ls /usr/local/include/gtest/
gtest-assertion-result.h gtest-message.h...

ls /usr/local/lib/gtest
libgtest.a      libgtest_main.a

3. most simple program

#include <gtest/gtest.h>

int main(int argc, char **argv) {

    testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}
$ c++ -std=c++17  -lgtest -lgtest_main  ./*.cpp && ./a.out

[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[  PASSED  ] 0 tests.

4. add a demo test

TEST(TestName, Subtest_1) { 
    ASSERT_TRUE(1 == 1); 
}

int main(...)
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TestName
[ RUN      ] TestName.Subtest_1
[       OK ] TestName.Subtest_1 (0 ms)
[----------] 1 test from TestName (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.

5. Assertions

  • Success
  • Non-Fatal-Failure
    • EXPECT_EQ()
    • EXPECT_NE()
    • EXPECT_LT()
    • EXPECT_LE()
    • ...
  • Fatal-Failure
    • ASSERT_EQ()
    • ...