57 lines
2.5 KiB
Markdown
57 lines
2.5 KiB
Markdown
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|GoogleTest]] 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:
|
|
```cmake
|
|
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|CICD]] (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](https://docs.gitlab.com/ee/ci/quick_start/).
|
|
|
|
1. You must have runners[^1] available
|
|
2. Create a `.gitlab-ci.yml` file 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](https://github.com/pothitos/gtest-demo-gitlab/tree/master).
|
|
My own first gitlab-ci config file (`.gitlab-ci.yml`):
|
|
```yaml
|
|
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`
|
|
<!--SR:!2023-12-21,14,290-->
|
|
requirements for `gcovr`;; project must be built with flags: `-fprofile-arcs` and `-ftest-coverage`
|
|
<!--SR:!2023-12-08,1,210-->
|
|
|
|
# Resources
|
|
- CI for robotics projects with [[ROS2]]: use [[Gazebo]] that is run on the gitlab server: http://moore-mike.com/bobble-ci.html
|
|
# Footnotes
|
|
[^1]: GitLab Runner is an application that can execute CI/CD jobs in a GitLab pipeline |