(一)测试驱动开发技巧--环境设置


环境设置

前言

新开个坑,本系列主要是《C++程序设计实践与技巧:测试驱动开发》一书的学习记录,由于笔记是在 2020 年就完成了。本次将其进行了一些细节修订、图片优化后同步到 blog 上,因此有些时间会对不上,不要在意这些细节无伤大雅 😂。

图床我是自建在 github 上用的 CDN 加速,如果有图片刷不出来的情况可以邮件我,看到了我会处理的。

或者直接看我的笔记也可以,觉得不错的来个 star 吧 😊。

实例程序

书中主要使用了两种测试框架 GMock 与 CppUTest,但是学习过程中希望 Catch 替换掉 CppUTest 与 GMock 并存的方式。

  • Wait,没想到上来就有个大差异,最近 Catch2 已经发布 v3 了。一次史无前例的破坏性更新,本文还是继续使用 v2 lastest 稳定版本,如果想要尝试切换参考官方给出的移植建议

code_example

编译器

主要在 Ubuntu20.04 system 下,使用 Clang-11 为编译器前端,Gun 的 ld 为连接器。

  • 两年过去了,我也抛弃了 Ubuntu 改用 Mac 了。你变成了你最讨厌的样子。
  • 话说 LLVM 都更新到 14 了(此文编写日),不过不用担心编译器问题,本系列的代码没有需要特殊语法的地方,即使切换了 Catch2 v3 也只需要 C++14 标准即可。不过我还是要说 LLVM 永远的神。
~/WorkSpace
❯ ls /bin/ld -l
lrwxrwxrwx 1 root root 19 Apr  7  2020 /bin/ld -> x86_64-linux-gnu-ld

~/WorkSpace
❯ clang -v
clang version 11.0.0 (https://github.com/llvm/llvm-project.git 0160ad802e899c2922bc9b29564080c22eb0908c)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64

CMake

使用 CMake 支持跨平台编译。

~/WorkSpace
❯ cmake -version
cmake version 3.16.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Google Mock & Catch2

安装不在赘述,Google Mock 的版本信息如下:

/usr/local/lib/cmake/GTest
❯ ls
GTestConfig.cmake  GTestConfigVersion.cmake  GTestTargets-noconfig.cmake  GTestTargets.cmake

/usr/local/lib/cmake/GTest
❯ dpkg -l | grep googletest
ii  googletest                             1.10.0-2                            all          Google's C++ test framework sources

Catch2 版本信息如下:

/usr/local/lib/cmake/Catch2
❯ ls
Catch.cmake         Catch2ConfigVersion.cmake     Catch2Targets.cmake  ParseAndAddCatchTests.cmake
Catch2Config.cmake  Catch2Targets-noconfig.cmake  CatchAddTests.cmake

/usr/local/lib/cmake/Catch2
❯ vim Catch2ConfigVersion.cmake

>>> set(PACKAGE_VERSION "2.13.3")

搭建运行 GMock 与 Catch2 的环境

详见我的项目

  • 快瞧瞧这个青涩的项目,README 写的一塌糊涂,给个 star 吧,让我知道有人在看有动力翻新下。

Json

不打算使用 JsonCPP 切换为开源的 Json for modern c++

  • 爱用哪个都行,换成 MDJSON 纯粹是喜欢 Only Header。
Install the project...
-- Install configuration: ""
-- Up-to-date: /usr/local/include
-- Installing: /usr/local/include/nlohmann
-- Installing: /usr/local/include/nlohmann/json.hpp
-- Installing: /usr/local/lib/cmake/nlohmann_json/nlohmann_jsonConfig.cmake
-- Installing: /usr/local/lib/cmake/nlohmann_json/nlohmann_jsonConfigVersion.cmake
-- Installing: /usr/local/lib/cmake/nlohmann_json/nlohmann_jsonTargets.cmake
-- Installing: /usr/local/lib/pkgconfig/nlohmann_json.pc

libcurl

开源代码安装

~/Tools/curl-7.74.0/build
❯ sudo make install -j32
[ 73%] Built target libcurl
[100%] Built target curl
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/lib/libcurl.a
-- Installing: /usr/local/bin/curl
-- Installing: /usr/local/bin/curl-config
-- Installing: /usr/local/lib/pkgconfig/libcurl.pc
-- Up-to-date: /usr/local/include/curl
-- Up-to-date: /usr/local/include/curl/curl.h
-- Up-to-date: /usr/local/include/curl/curlver.h
-- Up-to-date: /usr/local/include/curl/easy.h
-- Up-to-date: /usr/local/include/curl/mprintf.h
-- Up-to-date: /usr/local/include/curl/multi.h
-- Up-to-date: /usr/local/include/curl/options.h
-- Up-to-date: /usr/local/include/curl/stdcheaders.h
-- Up-to-date: /usr/local/include/curl/system.h
-- Up-to-date: /usr/local/include/curl/typecheck-gcc.h
-- Up-to-date: /usr/local/include/curl/urlapi.h
-- Old export file "/usr/local/lib/cmake/CURL/CURLTargets.cmake" will be replaced.  Removing files [/usr/local/lib/cmake/CURL/CURLTargets-noconfig.cmake].
-- Installing: /usr/local/lib/cmake/CURL/CURLTargets.cmake
-- Installing: /usr/local/lib/cmake/CURL/CURLTargets-noconfig.cmake
-- Installing: /usr/local/lib/cmake/CURL/CURLConfigVersion.cmake
-- Installing: /usr/local/lib/cmake/CURL/CURLConfig.cmake

Boost

Boost 直接通过 apt-get 安装管理:

  • 被 apt 版本管理折磨的退坑,是我不配 😭。
~/Tools
❯ dpkg -l | grep boost
ii  libboost-atomic1.71-dev:amd64          1.71.0-6ubuntu6                     amd64        atomic data types, operations, and memory ordering constraints
ii  libboost-atomic1.71.0:amd64            1.71.0-6ubuntu6                     amd64        atomic data types, operations, and memory ordering constraints
ii  libboost-chrono1.71-dev:amd64          1.71.0-6ubuntu6                     amd64        C++ representation of time duration, time point, and clocks
ii  libboost-chrono1.71.0:amd64            1.71.0-6ubuntu6                     amd64        C++ representation of time duration, time point, and clocks
ii  libboost-container1.71-dev:amd64       1.71.0-6ubuntu6                     amd64        C++ library that implements several well-known containers - dev files
ii  libboost-container1.71.0:amd64         1.71.0-6ubuntu6                     amd64        C++ library that implements several well-known containers
ii  libboost-context1.71-dev:amd64         1.71.0-6ubuntu6                     amd64        provides a sort of cooperative multitasking on a single thread
ii  libboost-context1.71.0:amd64           1.71.0-6ubuntu6                     amd64        provides a sort of cooperative multitasking on a single thread
ii  libboost-coroutine1.71-dev:amd64       1.71.0-6ubuntu6                     amd64        provides a sort of cooperative multitasking on a single thread
ii  libboost-coroutine1.71.0:amd64         1.71.0-6ubuntu6                     amd64        provides a sort of cooperative multitasking on a single thread
ii  libboost-date-time1.71-dev:amd64       1.71.0-6ubuntu6                     amd64        set of date-time libraries based on generic programming concepts
ii  libboost-date-time1.71.0:amd64         1.71.0-6ubuntu6                     amd64        set of date-time libraries based on generic programming concepts
ii  libboost-exception1.71-dev:amd64       1.71.0-6ubuntu6                     amd64        library to help write exceptions and handlers
ii  libboost-fiber1.71-dev:amd64           1.71.0-6ubuntu6                     amd64        cooperatively-scheduled micro-/userland-threads
ii  libboost-fiber1.71.0:amd64             1.71.0-6ubuntu6                     amd64        cooperatively-scheduled micro-/userland-threads
ii  libboost-filesystem1.71-dev:amd64      1.71.0-6ubuntu6                     amd64        filesystem operations (portable paths, iteration over directories, etc) in C++
ii  libboost-filesystem1.71.0:amd64        1.71.0-6ubuntu6                     amd64        filesystem operations (portable paths, iteration over directories, etc) in C++
ii  libboost-graph-parallel1.71-dev        1.71.0-6ubuntu6                     amd64        generic graph components and algorithms in C++
ii  libboost-graph-parallel1.71.0          1.71.0-6ubuntu6                     amd64        generic graph components and algorithms in C++
ii  libboost-graph1.71-dev:amd64           1.71.0-6ubuntu6                     amd64        generic graph components and algorithms in C++
ii  libboost-graph1.71.0:amd64             1.71.0-6ubuntu6                     amd64        generic graph components and algorithms in C++
ii  libboost-iostreams1.71-dev:amd64       1.71.0-6ubuntu6                     amd64        Boost.Iostreams Library development files
ii  libboost-iostreams1.71.0:amd64         1.71.0-6ubuntu6                     amd64        Boost.Iostreams Library
ii  libboost-locale1.71-dev:amd64          1.71.0-6ubuntu6                     amd64        C++ facilities for localization
ii  libboost-locale1.71.0:amd64            1.71.0-6ubuntu6                     amd64        C++ facilities for localization
ii  libboost-log1.71-dev                   1.71.0-6ubuntu6                     amd64        C++ logging library
ii  libboost-log1.71.0                     1.71.0-6ubuntu6                     amd64        C++ logging library
ii  libboost-math1.71-dev:amd64            1.71.0-6ubuntu6                     amd64        Boost.Math Library development files
ii  libboost-math1.71.0:amd64              1.71.0-6ubuntu6                     amd64        Boost.Math Library
ii  libboost-mpi-python1.71-dev            1.71.0-6ubuntu6                     amd64        C++ interface to the Message Passing Interface (MPI), Python Bindings
ii  libboost-mpi-python1.71.0              1.71.0-6ubuntu6                     amd64        C++ interface to the Message Passing Interface (MPI), Python Bindings
ii  libboost-mpi1.71-dev                   1.71.0-6ubuntu6                     amd64        C++ interface to the Message Passing Interface (MPI)
ii  libboost-mpi1.71.0                     1.71.0-6ubuntu6                     amd64        C++ interface to the Message Passing Interface (MPI)
ii  libboost-numpy1.71-dev                 1.71.0-6ubuntu6                     amd64        Boost.Python NumPy extensions development files
ii  libboost-numpy1.71.0                   1.71.0-6ubuntu6                     amd64        Boost.Python NumPy extensions
ii  libboost-program-options1.71-dev:amd64 1.71.0-6ubuntu6                     amd64        program options library for C++
ii  libboost-program-options1.71.0:amd64   1.71.0-6ubuntu6                     amd64        program options library for C++
ii  libboost-python1.71-dev                1.71.0-6ubuntu6                     amd64        Boost.Python Library development files
ii  libboost-python1.71.0                  1.71.0-6ubuntu6                     amd64        Boost.Python Library
ii  libboost-random1.71-dev:amd64          1.71.0-6ubuntu6                     amd64        Boost Random Number Library
ii  libboost-random1.71.0:amd64            1.71.0-6ubuntu6                     amd64        Boost Random Number Library
ii  libboost-regex1.71-dev:amd64           1.71.0-6ubuntu6                     amd64        regular expression library for C++
ii  libboost-regex1.71.0:amd64             1.71.0-6ubuntu6                     amd64        regular expression library for C++
ii  libboost-serialization1.71-dev:amd64   1.71.0-6ubuntu6                     amd64        serialization library for C++
ii  libboost-serialization1.71.0:amd64     1.71.0-6ubuntu6                     amd64        serialization library for C++
ii  libboost-stacktrace1.71-dev:amd64      1.71.0-6ubuntu6                     amd64        library to capture and print stack traces - development files
ii  libboost-stacktrace1.71.0:amd64        1.71.0-6ubuntu6                     amd64        library to capture and print stack traces
ii  libboost-system1.71-dev:amd64          1.71.0-6ubuntu6                     amd64        Operating system (e.g. diagnostics support) library
ii  libboost-system1.71.0:amd64            1.71.0-6ubuntu6                     amd64        Operating system (e.g. diagnostics support) library
ii  libboost-test1.71-dev:amd64            1.71.0-6ubuntu6                     amd64        components for writing and executing test suites
ii  libboost-test1.71.0:amd64              1.71.0-6ubuntu6                     amd64        components for writing and executing test suites
ii  libboost-thread1.71-dev:amd64          1.71.0-6ubuntu6                     amd64        portable C++ multi-threadingii  libboost-thread1.71.0:amd64            1.71.0-6ubuntu6                     amd64        portable C++ multi-threadingii  libboost-timer1.71-dev:amd64           1.71.0-6ubuntu6                     amd64        C++ wall clock and CPU process timers
ii  libboost-timer1.71.0:amd64             1.71.0-6ubuntu6                     amd64        C++ wall clock and CPU process timers
ii  libboost-type-erasure1.71-dev:amd64    1.71.0-6ubuntu6                     amd64        C++ runtime polymorphism based on concepts
ii  libboost-type-erasure1.71.0:amd64      1.71.0-6ubuntu6                     amd64        C++ runtime polymorphism based on concepts
ii  libboost-wave1.71-dev:amd64            1.71.0-6ubuntu6                     amd64        C99/C++ preprocessor libraryii  libboost-wave1.71.0:amd64              1.71.0-6ubuntu6                     amd64        C99/C++ preprocessor libraryii  libboost1.71-all-dev                   1.71.0-6ubuntu6                     amd64        Boost C++ Libraries development files (ALL)
ii  libboost1.71-dev:amd64                 1.71.0-6ubuntu6                     amd64        Boost C++ Libraries development files
ii  libboost1.71-doc                       1.71.0-6ubuntu6                     all          Boost.org libraries documentation placeholder
ii  libboost1.71-tools-dev                 1.71.0-6ubuntu6                     amd64        Boost C++ Libraries development tools

总结

本文主要展示了后边文章中所需要的一些三方库、安装等示例,继续吐槽 C++ 没有统一的包管理器(对着 Cargo 流口水)。

  • 看到以前写的笔记还是感慨良多。有时看书实在太功利了,总想这去读对自己有用的,但是当能判断出这书有用了其实反而对于知识、眼界的扩展是有限的。
  • 在阅读本书时,无论工作内容还是什么其他方面完全没有测试代码的意识,在钢丝上行走小心翼翼的维护者脆弱的代码。随着经验的增加越来越能体会到测试代码的重要性,已经记不得当时学习这本书的动机了,但是以现在的眼光来看当时的决定对我的帮助无疑是巨大的。
  • 最后引用一句忘记了从那本书看到的话:你读过的每一本书,最后都会变成你的血与肉。

文章作者: Layton
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Layton !
  目录