1准备条件
1.1安装gcc
代码语言:javascript复制sudo apt-get update
sudo apt-get install g
1.2 安装CMake
CMake必须高于3.13 ,否则会报CMake 3.13 or higher is required. You are running version 3.XX.X。
1)下载cmake-3.20.3-linux-x86_64.tar.gz
代码语言:javascript复制wget https://cmake.org/files/v3.20/cmake-3.20.3-linux-x86_64.tar.gz
2)解压
代码语言:javascript复制tar xvzf cmake-3.20.3-linux-x86_64.tar.gz
3)移动到/usr/local/cmake目录
代码语言:javascript复制sudo mv cmake-3.20.3-linux-x86_64 /usr/local/cmake
4)编辑环境变量文件
代码语言:javascript复制gedit /etc/profile
export PATH=/usr/local/cmake/bin:$PATH
代码语言:javascript复制source /etc/profile
5)进入cmake目录
代码语言:javascript复制cd /usr/local/cmake
6)查看cmake版本
代码语言:javascript复制cmake –version
2 建立google Test
1)到https://github.com/google/googletest获取最新版本的googletest
代码语言:javascript复制wget https://github.com/google/googletest/archive/refs/heads/main.zip
2)解压
代码语言:javascript复制unzip main.zip
3)进入googletest目录
代码语言:javascript复制cd /home/jerry/googletest-main/
4)在CMakeLists.txt 中加入内容
代码语言:javascript复制gedit CMakeLists.txt
加入
set(CMAKE_CXX_STANDARD 14)
5)运行
代码语言:javascript复制cmake CMakeLists.txt
生成Makefile文件
6)执行make命令
make命令,生成四个静态库:libgtest.a libgtest_main.a,libgmlck.a, libgmock_main.a
代码语言:javascript复制find . -name "*.a"
7)安装gmock和gtest到系统:
代码语言:javascript复制sudo make install
ls -l /usr/local/lib
7)执行自带测试文件
代码语言:javascript复制cd googletest/samples/
g ../src/gtest_main.cc sample1.cc sample1_unittest.cc -o test -lgtest -lgmock -lpthread -std=c 14
l../src/gtest_main.cc 主测试文件
lsample1.cc 被测试文件
lsample1_unittest.cc:测试文件
ltest 运行生成文件
lc 14中的14表示C 14标准
8) 运行测试用例
代码语言:javascript复制./test
Running main() from ../src/gtest_main.cc
[==========] Running 6 tests from 2 test suites.
[----------] 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 (0 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 suites ran. (0 ms total)
[ PASSED ] 6 tests.