I searched for googletests using muon, but it looks like ubuntu doesn't have packages for it. Do I need to install using sources?
2 Answers
New information:
It is worth noting libgtest0 no longer exists. As of 2013 or so (I am not sure of the date of the change) see this question:
Why no library files installed for google test?
Old answer prior to 2012:
It is in the Ubuntu repositories
sudo apt-get install libgtest0 libgtest-dev
See also man gtest-config
- 104,528
Minimal runnable example
Since Debian/Ubuntu refuse to pack a prebuilt as mentioned at: Why no library files installed for google test? I'll just clone and build it myself (or in a real project, add it as a submodule):
git clone https://github.com/google/googletest
cd googletest
git checkout b1fbd33c06cdb0024c67733c6fdec2009d17b384
mkdir build
cd build
cmake ..
make -j`nproc`
cd ../..
then I use it with my test file main.cpp:
g++ \
-Wall \
-Werror \
-Wextra \
-pedantic \
-O0 \
-I googletest/googletest/include \
-std=c++11 \
-o main.out \
main.cpp \
googletest/build/lib/libgtest.a \
-lpthread \
;
main.cpp
#include <gtest/gtest.h>
int myfunc(int n) {
return n + 1;
}
TEST(asdfTest, HandlesPositiveInput) {
EXPECT_EQ(myfunc(1), 2);
EXPECT_EQ(myfunc(2), 3);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
to get the expected output:
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from asdfTest
[ RUN ] asdfTest.HandlesPositiveInput
[ OK ] asdfTest.HandlesPositiveInput (0 ms)
[----------] 1 test from asdfTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
Alternatively, you can also remove the main function from the main.cpp file and instead use the default one provided by libgtest_main.a:
g++ \
-Wall \
-Werror \
-Wextra \
-pedantic \
-O0 \
-I googletest/googletest/include \
-std=c++11 \
-o main.out \
main.cpp \
googletest/build/lib/libgtest.a \
googletest/build/lib/libgtest_main.a \
-lpthread \
;
Tested on Ubuntu 20.04.
- 31,312