Oscar Forner Martinez bio photo

Oscar Forner Martinez

I am software engineer at VCA Technology. Learning Linux Kernel, passionate about GNU/Linux and OSS, enjoying algorithms and data structures and coding in C and C++.

Email Twitter Google+ LinkedIn Github

Unity is one of the available frameworks to create unit tests for C. In this example, I will use CMake to configure the project and build.

All the code and configuration files used in this post are available in this repo in GitHub.

Can you do unit test in C? And what is Unity?

Some people do not know you can do unit tests in C and people must do unit tests in any language. My choice to do unit tests in C is Unity for several reasons:

  • No need to install any package in your distro. Just add the three files to your project and start using it.
  • Great documentation and examples in the repo and their website.
  • Awesome course about unit test in embedded systems on Udemy. I did it myself, totally worth it if you are interested in these topics.

Structure of the project

In the root of the project we have the CMakeLists.txt file and three folders: lib, src and unittest.

  • lib: Contains the Unity files.
  • src: Contains the code we want to test. In this case, a functions header file.
  • unittest: Contains the test we want to run.

The project to test

The configuration will be done in the CMakeLists.txt file:

The code we want to test is:

Finally, the unit test: As you can see in the code above, you need a main in the unit test file that contains: UNITY_BEGIN and UNITY_END calls and a RUN_TEST call for each test we want to run.

Conclusion

Everybody should be doing unit tests, it does not matter what language do they use. I do use this framework among others (that I will introduce in future posts) for testing C applications. Unity is a must have tool for any C developer, even for embedded software. You can check it in their website and course.