A first step to get started with :
- Unit tests in C++
- cmake : a generator of compilation scripts
Two ways to get boost.test library :
- you can download boost.test with package manager
- or you can choose to let cmake download and install boost.test while fizzbuzz's cmake configuation step.
So, in more details :
- By package manager :
- On Linux :
$ sudo apt-get install libboost-all-dev
- On Windows, prefer using vcpkg, so you should have vcpkg installed on your machine.
\> cd D:\path\to\your\vcpkg \> vcpkg install boost-test:x64-windows
- By cmake : pay attention to the fizzbuzz configuration cmake step given below.
Get a cmake of which version is > 3.5
The following commands generate build files for fizzbuzz program, then build it and eventually run its unit tests. In the process of installing fizzbuzz, the only command that differ over systems (Windows or linux) and boost.test installation strategies (using package manager or letting cmake do everything) is the cmake configuration step. All other commands are the same, no matter your system or your boost.test installation stategy :
$ cd /path/to/your/FizzBuzz/work/dir
$ <cmake configuration step>
$ cmake --build build [--config Release]
$ cd build
$ ctest -C Release|Debug
Remarks :
- As already said about package manager :
- On linux (Ubuntu), we use native apt-get.
- On Windows, we use vcpkg.
- There are 3 important variables about running unit tests when configuring with cmake : CMAKE_TOOLCHAIN_FILE, BUILD_TESTING and BUILD_DEPS.
- CMAKE_TOOLCHAIN_FILE : only on Windows, causes cmake to find boost.test in vcpkg. This variable is given to cmake as a command line argument and must be valued with the path of your vcpkg's cmake file (vcpkg.cmake).
- BUILD_TESTING : not given or turned to OFF as a command line argument of cmake, unit tests won't be compiled (you won't be able to run them). If turned to ON, cmake will try to compile unit tests program and find boost.test to link against it.
- BUILD_DEPS : if turned to ON, cmake will try to download boost.test and install it at configuration step.
Let's look at the cmake depending on your system and choice for boost.test installation.
$ cmake -S . -B build -DCMAKE_CXX_FLAGS="-Werror" [-DCMAKE_BUILD_TYPE=Release] [-DBUILD_TESTING=ON] [-DBUILD_DEPS=ON]
Let vcpkg_path be the variable containing the absolute path to your vcpkg.
\> cmake -G "Visual Studio 16 2019" -A x64 [-DCMAKE_BUILD_TYPE=release] [-DCMAKE_TOOLCHAIN_FILE=${vcpkg_path}/scripts/buildsystems/vcpkg.cmake] [-DBUILD_TESTING=ON] [-DBUILD_DEPS=ON] . -B build
Remark :
- Most of the time, everything that is generated and built by actions described above can be thoroughly undone by removing the content of the build directory.
- There is an exception though : if you choose to let cmake download boost.test for unit tests, you have to remove boost.test as well. To do that, remove the following directory :
${path-to-your-FizzBuzz-work-dir}/ext/boost