一、下载并编译googletest
1、下载:https://github.com/google/googletest
2、编译方式:https://github.com/google/googletest/tree/master/googletest
(1)我使用的是ubuntu18.04,解压下载文件到当前路径。
(2)在当前路径执行
g++ -isystem ./include -I. -pthread -c ./src/gtest-all.cc
ar -rv libgtest.a gtest-all.o
(3)如果提示以下错误
g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-7/README.Bugs> for instructions.
这个原因是内存不足,在linux下增加临时swap空间
step 1:
#sudo dd if=/dev/zero of=/home/swap bs=64M count=16
注释:of=/home/swap,放置swap的空间; count的大小就是增加的swap空间的大小,64M就是块大小,这里是64MB,所以总共空间就是bs*count=1024MB.这里分配空间的时候需要一点时间,等待执行完毕。
step 2:
# sudo mkswap /home/swap (可能会提示warning: don't erase bootbits sectorson whole disk. Use -f to force,不用理会)
注释:把刚才空间格式化成swap各式
step 3:
#sudo swapon /home/swap
注释:使刚才创建的swap空间
然后重新执行步骤(2),如果创建了临时空间仍然提示 "g++: internal compiler error: Killed (program cc1plus)",可能分配的空间不够大,可继续分配更大的空间。
成功执行步骤(2)之后释放该临时空间
sudo swapoff /home/swap
sudo rm /home/swap
(4)测试
g++ -isystem ./include -pthread path/to/your_test.cc libgtest.a -o your_test
其中path/to/your_test.cc是你的测试文件。
(5)测试成功说明系统环境没有安装问题。make
cd ./make
sudo apt install make
sudo make
./sample1_unittest
运行sample1_unittest得到结果
Running main() from gtest_main.cc
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from FactorialTest
[ RUN ] FactorialTest.Negative
[ OK ] FactorialTest.Negative (0 ms)
[ RUN ] FactorialTest.Zero
[ OK ] FactorialTest.Zero (0 ms)
[ RUN ] FactorialTest.Positive
[ OK ] FactorialTest.Positive (0 ms)
[----------] 3 tests from FactorialTest (1 ms total)
[----------] 3 tests from IsPrimeTest
[ RUN ] IsPrimeTest.Negative
[ OK ] IsPrimeTest.Negative (0 ms)
[ RUN ] IsPrimeTest.Trivial
[ OK ] IsPrimeTest.Trivial (0 ms)
[ RUN ] IsPrimeTest.Positive
[ OK ] IsPrimeTest.Positive (0 ms)
[----------] 3 tests from IsPrimeTest (0 ms total)
[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (1 ms total)
[ PASSED ] 6 tests.