当前位置: 首页 > 工具软件 > Google Glog > 使用案例 >

63、ubuntu下使用googletest进行单元测试和googleglog日志输出

穆单鹗
2023-12-01

基本思想:使用googletest进行单元测试,并且使用googleglog进行日志输出,简单记录一下如何使用

第一步:测试googletest

命令行安装

ubuntu@ubuntu:~$ sudo apt-get install libgtest-dev

或者源码安装

ubuntu@ubuntu:~$ git clone https://github.com/google/googletest.git -b release-1.12.0
Cloning into 'googletest'...
remote: Enumerating objects: 24825, done.
remote: Counting objects: 100% (41/41), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 24825 (delta 15), reused 32 (delta 14), pack-reused 24784
Receiving objects: 100% (24825/24825), 10.66 MiB | 2.00 MiB/s, done.
Resolving deltas: 100% (18362/18362), done.
Note: switching to '15460959cbbfa20e66ef0b5ab497367e47fc0a04'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

ubuntu@ubuntu:~$ cd googletest/
ubuntu@ubuntu:~/googletest$ mkdir build
ubuntu@ubuntu:~/googletest/build$ make
Scanning dependencies of target gtest
[ 25%] Building CXX object googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 50%] Linking CXX static library ../lib/libgtest.a
[ 50%] Built target gtest
Scanning dependencies of target gtest_main
[ 75%] Building CXX object googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[100%] Linking CXX static library ../lib/libgtest_main.a
[100%] Built target gtest_main
ubuntu@ubuntu:~/googletest/build$ sudo make install
ubuntu@ubuntu:~/googletest/build$ sudo ldconfig

cmakelists.txt

cmake_minimum_required(VERSION 3.16)
project(untitled5)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(GTest REQUIRED)
set(CMAKE_CXX_STANDARD 11)

add_executable(untitled5 main.cpp)
target_link_libraries(untitled5 gtest pthread)

或者这样写

cmake_minimum_required(VERSION 3.16)
project(untitled5)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
find_package(GTest REQUIRED)
set(CMAKE_CXX_STANDARD 11)

add_executable(untitled5 main.cpp)
target_link_libraries(untitled5  GTest::Main)

main.cpp

#include <iostream>
#include <gtest/gtest.h>

int add(int a, int b) {
    return a + b;
}

TEST(MyTest, AddTest) {
    EXPECT_EQ(add(1, 2), 3);
}

int main(int argc, char *argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

测试结果

/home/ubuntu/CLionProjects/untitled5/cmake-build-debug/untitled5
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from MyTest
[ RUN      ] MyTest.AddTest
[       OK ] MyTest.AddTest (0 ms)
[----------] 1 test from MyTest (0 ms total)

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

Process finished with exit code 0

第二步:测试glog的使用

ubuntu@ubuntu:~/glog$ sudo apt-get install autoconf automake libtool
Reading package lists... Done
Building dependency tree       
Reading state information... Done
autoconf is already the newest version (2.69-11.1).
autoconf set to manually installed.
automake is already the newest version (1:1.16.1-4ubuntu6).
automake set to manually installed.
libtool is already the newest version (2.4.6-14).
libtool set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 446 not upgraded.
ubuntu@ubuntu:~/glog$ ls
AUTHORS      cmake           glog-config.cmake.in   src
bazel        CMakeLists.txt  glog-modules.cmake.in  WORKSPACE
BUILD.bazel  CONTRIBUTORS    libglog.pc.in
ChangeLog    COPYING         README.rst
ubuntu@ubuntu:~/glog$ mkdir build
ubuntu@ubuntu:~/glog$ cd build/
ubuntu@ubuntu:~/glog/build$ cmake ..
-- The CXX compiler identification is GNU 9.4.0
ubuntu@ubuntu:~/glog/build$ make
Scanning dependencies of target glogbase
[ 22%] Built target glogbase
[ 25%] Built target glogtest
-- Installing: /usr/local/lib/cmake/glog/glog-targets.cmake
-- Installing: /usr/local/lib/cmake/glog/glog-targets-noconfig.cmake

cmakelists.txt

cmake_minimum_required(VERSION 3.16)
project(untitled5)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled5 main.cpp)
target_link_libraries(untitled5 glog)

main.cpp

#include <glog/logging.h>

int main(int argc,char* argv[])
{
    LOG(INFO) << "Hello,GLOG!";
}

测试结果

/home/ubuntu/CLionProjects/untitled5/cmake-build-debug/untitled5
WARNING: Logging before InitGoogleLogging() is written to STDERR
I0806 11:49:51.195060  8234 main.cpp:5] Hello,GLOG!

Process finished with exit code 0

 类似资料: