Intro to CI/CD Part 3: Getting Started with Unit Testing | DigiKey
Continuous Integration and Continuous Delivery (CI/CD) is the process of automating the testing and deployment of a software project. It is often used in large-scale, multi-person projects, but it can be useful in smaller projects and embedded firmware development. This tutorial shows how to use CppUTest to build unit tests for C/C++ applications. From there, we automate the testing process using Docker and GitHub Actions.
You can view a written version of this tutorial here: https://www.digikey.com/en/maker/projects/writing-cc-unit-tests-with-cpputest/7776121323b74ae7b20725cf06163537
Code for this tutorial can be found here: https://github.com/ShawnHymel/c-unit-test
Previously, we looked at running modularized containers with Docker (https://www.youtube.com/watch?v=1nxGcfIm-TU) as well as building simple GitHub Actions workflows (https://www.youtube.com/watch?v=8pyqbYDYkRs). We can use these automation tools to automatically test our code. Unit tests make up an important part of the CI/CD pipeline, as it ensures most of our code is tested prior to deployment. Note that to achieve good code coverage with your tests, you must design your project structure so that your code is broken down into independent components that can be tested individually.
If your code relies on drivers unique to a particular set of hardware (e.g. I2C drivers on a microcontroller), you might need to construct “test doubles” so that your software components can run inside of your virtualized test framework (e.g. in a Docker container). We will not cover test doubles in this series, but note that it might be necessary for some projects.
While you can build your own test framework, it’s often helpful to use existing frameworks to avoid reinventing the wheel. Such test frameworks can help manage interconnected dependencies among software components, which can complicate your test setup. We will specifically use CppUTest (http://cpputest.github.io/) to build simple unit tests.
In the video, we demonstrate how to write a simple application around a component that averages the elements in an array. We then build a unit test for this component with CppUTest and run the tests locally. From there, we construct a Docker image that runs the tests without needing to install or build the testing framework on a host machine. Finally, we automate the process by building a simple GitHub Actions workflow that runs the Docker image each time new code is checked into the repository.

