Google C++ Unit test sample

用户克隆了GoogleTest的v1.8.x分支,并通过CMake配置和构建了库,然后使用pkg-config获取编译和链接gtest及gtest_main所需的选项。在samples目录下,用户编译并运行了一个简单的测试用例,成功验证了GoogleTest的基本断言功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
[lake@localhost source]$ git clone  -b    v1.8.x    --depth 1      https://github.com/google/googletest.git
Cloning into 'googletest'...
remote: Enumerating objects: 369, done.
remote: Counting objects: 100% (369/369), done.
remote: Compressing objects: 100% (330/330), done.
remote: Total 369 (delta 124), reused 78 (delta 27), pack-reused 0
Receiving objects: 100% (369/369), 1.06 MiB | 596.00 KiB/s, done.
Resolving deltas: 100% (124/124), done.
[lake@localhost source]$ cd googletest/
[lake@localhost googletest]$ ls
appveyor.yml  ci              configure.ac     googlemock  LICENSE      README.md
BUILD.bazel   CMakeLists.txt  CONTRIBUTING.md  googletest  Makefile.am  WORKSPACE

[lake@localhost mytest]$  cmake -version
cmake version 3.5.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).
[lake@localhost mytest]$

[lake@localhost googletest]$ git log -p
commit 2fb1240f4422a8ca561c0af5dbc79ead4a24f4a8 (HEAD -> v1.8.x)
Author: Lake Hu  
Date:   Tue Sep 14 13:58:54 2021 +0800

    default install prefix in cmake

diff --git a/.gitignore b/.gitignore
index 73cdd2c..8648cda 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
+googlemock/libgmock*.a
+/install_manifest.txt
+/CMakeCache.txt
 # Ignore CI build directory
 build/
 xcuserdata
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 33fd8c5..216cc6f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,6 +4,9 @@ if (POLICY CMP0048)
   cmake_policy(SET CMP0048 NEW)
 endif (POLICY CMP0048)

+## must set before project
+SET(CMAKE_INSTALL_PREFIX $ENV{HOME}/usr)
+
 project(googletest-distribution)
 set(GOOGLETEST_VERSION 1.8.2)
[lake@localhost googletest]$  
[lake@localhost googletest]$ cmake    .
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/lib64/ccache/cc
-- Check for working C compiler: /usr/lib64/ccache/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/lib64/ccache/c++
-- Check for working CXX compiler: /usr/lib64/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: /usr/bin/python (found version "2.7.5")
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lhu3/extra/source/googletest
[lake@localhost googletest]$ make  -j8
Scanning dependencies of target gtest
[ 12%] Building CXX object googlemock/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 25%] Linking CXX static library libgtest.a
[ 25%] Built target gtest
Scanning dependencies of target gmock
Scanning dependencies of target gtest_main
[ 37%] Building CXX object googlemock/gtest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[ 50%] Building CXX object googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 62%] Linking CXX static library libgtest_main.a
[ 62%] Built target gtest_main
[ 75%] Linking CXX static library libgmock.a
[ 75%] Built target gmock
Scanning dependencies of target gmock_main
[ 87%] Building CXX object googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[100%] Linking CXX static library libgmock_main.a
[100%] Built target gmock_main
[lake@localhost googletest]$ make install
[ 25%] Built target gtest
[ 50%] Built target gmock
[ 75%] Built target gmock_main
[100%] Built target gtest_main 
[lake@localhost googletest]$ make install
[ 25%] Built target gtest
[ 50%] Built target gmock
[ 75%] Built target gmock_main
[100%] Built target gtest_main
Install the project...
-- Install configuration: ""
-- Up-to-date: /home/lhu3/usr/include
-- Up-to-date: /home/lhu3/usr/include/gmock
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-actions.h
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-generated-function-mockers.h.pump
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-generated-actions.h
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-generated-nice-strict.h.pump
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-more-actions.h
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-more-matchers.h
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-generated-actions.h.pump
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-cardinalities.h
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock.h
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-generated-matchers.h
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-matchers.h
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-generated-function-mockers.h
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-generated-matchers.h.pump
-- Up-to-date: /home/lhu3/usr/include/gmock/gmock-spec-builders.h
-- Up-to-date: /home/lake/usr/include/gmock/internal
-- Up-to-date: /home/lake/usr/include/gmock/internal/gmock-generated-internal-utils.h.pump
-- Up-to-date: /home/lake/usr/include/gmock/internal/gmock-generated-internal-utils.h
-- Up-to-date: /home/lake/usr/include/gmock/internal/gmock-port.h
-- Up-to-date: /home/lake/usr/include/gmock/internal/custom
-- Up-to-date: /home/lake/usr/include/gmock/internal/custom/README.md
-- Up-to-date: /home/lake/usr/include/gmock/internal/custom/gmock-generated-actions.h
-- Up-to-date: /home/lake/usr/include/gmock/internal/custom/gmock-port.h
-- Up-to-date: /home/lake/usr/include/gmock/internal/custom/gmock-generated-actions.h.pump
-- Up-to-date: /home/lake/usr/include/gmock/internal/custom/gmock-matchers.h
-- Up-to-date: /home/lake/usr/include/gmock/internal/gmock-internal-utils.h
-- Up-to-date: /home/lake/usr/include/gmock/gmock-generated-nice-strict.h
-- Up-to-date: /home/lake/usr/lib64/libgmock.a
-- Up-to-date: /home/lake/usr/lib64/libgmock_main.a
-- Up-to-date: /home/lake/usr/lib64/pkgconfig/gmock.pc
-- Up-to-date: /home/lake/usr/lib64/pkgconfig/gmock_main.pc
-- Up-to-date: /home/lake/usr/lib64/cmake/GTest/GTestTargets.cmake
-- Up-to-date: /home/lake/usr/lib64/cmake/GTest/GTestTargets-noconfig.cmake
-- Up-to-date: /home/lake/usr/lib64/cmake/GTest/GTestConfigVersion.cmake
-- Up-to-date: /home/lake/usr/lib64/cmake/GTest/GTestConfig.cmake
-- Up-to-date: /home/lake/usr/include
-- Up-to-date: /home/lake/usr/include/gtest
-- Up-to-date: /home/lake/usr/include/gtest/gtest-typed-test.h
-- Up-to-date: /home/lake/usr/include/gtest/gtest-spi.h
-- Up-to-date: /home/lake/usr/include/gtest/gtest-param-test.h
-- Up-to-date: /home/lake/usr/include/gtest/gtest_pred_impl.h
-- Up-to-date: /home/lake/usr/include/gtest/gtest-death-test.h
-- Up-to-date: /home/lake/usr/include/gtest/gtest.h
-- Up-to-date: /home/lake/usr/include/gtest/gtest-printers.h
-- Up-to-date: /home/lake/usr/include/gtest/gtest-message.h
-- Up-to-date: /home/lake/usr/include/gtest/gtest_prod.h
-- Up-to-date: /home/lake/usr/include/gtest/gtest-test-part.h
-- Up-to-date: /home/lake/usr/include/gtest/internal
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-tuple.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-tuple.h.pump
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-string.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-param-util-generated.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-filepath.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-port.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-port-arch.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-param-util.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-death-test-internal.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-internal.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-linked_ptr.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-type-util.h.pump
-- Up-to-date: /home/lake/usr/include/gtest/internal/custom
-- Up-to-date: /home/lake/usr/include/gtest/internal/custom/README.md
-- Up-to-date: /home/lake/usr/include/gtest/internal/custom/gtest-port.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/custom/gtest.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/custom/gtest-printers.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-type-util.h
-- Up-to-date: /home/lake/usr/include/gtest/internal/gtest-param-util-generated.h.pump
-- Up-to-date: /home/lake/usr/include/gtest/gtest-param-test.h.pump
-- Up-to-date: /home/lake/usr/lib64/libgtest.a
-- Up-to-date: /home/lake/usr/lib64/libgtest_main.a
-- Up-to-date: /home/lake/usr/lib64/pkgconfig/gtest.pc
-- Up-to-date: /home/lake/usr/lib64/pkgconfig/gtest_main.pc
[lake@localhost googletest]$  
[lake@localhost googletest]$  pkg-config --cflags --libs  gtest
-DGTEST_HAS_PTHREAD=1 -pthread -I/home/lake/usr/include  -pthread -L/home/lake/usr/lib64 -lgtest
[lake@localhost googletest]$ pkg-config --cflags --libs  gtest_main
-DGTEST_HAS_PTHREAD=1 -pthread -I/home/lake/usr/include  -pthread -L/home/lake/usr/lib64 -lgtest_main -lgtest

[lake@localhost googletest]$ ls
appveyor.yml  CMakeCache.txt       CMakeLists.txt   CTestTestfile.cmake  install_manifest.txt  Makefile.am
BUILD.bazel   CMakeFiles           configure.ac     googlemock           LICENSE               README.md
ci            cmake_install.cmake  CONTRIBUTING.md  googletest           Makefile              WORKSPACE



[lake@localhost samples]$ pwd
/home/lhu3/extra/source/googletest/googletest/samples
[lake@localhost samples]$

[lake@localhost samples]$ g++  -o sample1_unittest1     sample1_unittest.cc     sample1.cc      `pkg-config --cflags --libs  gtest_main`
[lake@localhost samples]$ ./sample1_unittest1
Running main() from /home/lhu3/extra/source/googletest/googletest/src/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 (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 cases ran. (0 ms total)
[  PASSED  ] 6 tests.




[lake@localhost googletest]$ mkdir  mytest
[lake@localhost googletest]$ cd mytest/
[lake@localhost mytest]$ vim hellotest.cc


[lake@localhost mytest]$ cat hellotest.cc
#include <gtest/gtest.h>

// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {
  // Expect two strings not to be equal.
  EXPECT_STRNE("hello", "world");
  // Expect equality.
  EXPECT_EQ(7 * 6, 42);
}


[lake@localhost mytest]$ vim hellotest.cc
[lake@localhost mytest]$ g++  hellotest.cc  `pkg-config --cflags --libs  gtest_main`
[lake@localhost mytest]$ ls
a.out  hellotest.cc
[lake@localhost mytest]$ ./a.out
Running main() from /home/lake/extra/source/googletest/googletest/src/gtest_main.cc
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from HelloTest
[ RUN      ] HelloTest.BasicAssertions
[       OK ] HelloTest.BasicAssertions (0 ms)
[----------] 1 test from HelloTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.
[lake@localhost mytest]$

Example

// g++  hellotest.cc  `pkg-config --cflags --libs  gtest_main`

#include <gtest/gtest.h>

// Demonstrate some basic assertions.
TEST(HelloTest, BasicAssertions) {
  // Expect two strings not to be equal.
  EXPECT_STRNE("hello", "world");
  // Expect equality.
  EXPECT_EQ(7 * 6, 42);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值