2.5 KiB
I have been wanting to code properly with unit tests and continuous integration for a few years now and have never done it properly.
Development Process
Unit Testing
C++
In C++ we use the GTest Framework Framework.
Code Coverage
Code coverage testing basically compiles the code with profiling flags, which count the number of times a specific line has been executed. And since google test runs the source code we can test if we are testing all the code that we have written.
For C++ typically, a tool called gcovr is used. In order for it to work you need to compile the code with the following flags: -fprofile-arcs and -ftest-coverage
If the project uses CMake you can add the following to your CMakeLists.txt:
if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
endif()
Online Tools
In order to do continuous integration we need a place that automatically runs our unit tests whenever a commit is pushed.
GitLab
Gitlab is known for its Gitlab - CI CD (Continuous Integration / Continuous Deployment) pipeline. It allows to customize and automate the entire process very effectively. The setup process is also very easy and explained well on their website.
- You must have runners1 available
- Create a
.gitlab-ci.ymlfile at the root of your repository. The entire pipeline of automated tests is defined in this file.
A good example repo can be found here.
My own first gitlab-ci config file (.gitlab-ci.yml):
image: ubuntu:20.04
job:
script:
- export DEBIAN_FRONTEND=noninteractive
- apt-get update
- apt-get install -y cmake g++ build-essential git gcovr
- cd onesec3d_cpp
- mkdir build && cd build
- cmake -DBUILD_TESTING=ON ..
- cmake --build .
- export DATADIR=../test/sample_3d_files/
- ./test/onesec3d_cpp_test
- gcovr --exclude-directories '_deps' -r ..
Flashcards
#learning/cpp
which command to do code coverage testing;;gcovr
requirements for gcovr;; project must be built with flags: -fprofile-arcs and -ftest-coverage
Resources
- CI for robotics projects with ROS2: use Gazebo that is run on the gitlab server: http://moore-mike.com/bobble-ci.html
Footnotes
-
GitLab Runner is an application that can execute CI/CD jobs in a GitLab pipeline ↩︎