C Unit Testing with Check

When I start on IT, several years ago(+15), C was the first language that I learned. But was not the first language I mastered and deployed in production which was...surprisingly Visual Basic 6.0. However, when I learned C or any language back on that old and arcane time noddy was into testing. However now is impossible to work professionally in any language without tests. C is a low-level language but still kicks ass and has several interesting libs/frameworks. People use to fear C a lot because of lack of syntactical sugar but once you get used to it is not that hard at all. Today I want to show how you can do Unit Testing with C. I will not show how to do Unit Testing with C++ but with C ANSI actually. We will be using Check a unit test lib for C ANSI.

Installing Check on Ubuntu

Currently, I run ubuntu 18.04 LTS on my laptop. So, first of all, let's install check on our machine.

Once you still check now we can get down to the code.

We will create the following directory structure. Please create these folders and touch this files.

Creating the Header file

We will build a simple Calculator solution in order to try out unit tests with Check. So if you are not familiar with OOP like programming in C you can check this out first. Our calculator will be pretty basic we will do basic arithmetic functions. So let's get started with the header file(calc.h).

Here we have a type definition(typedef) Calc which represents a Calculator. Here we basically have function pointers to all math operations(also known as methods) outside of the "Object" Struct we have the constructor and destructor for the type. Right now we have the header file and then we can move to the implementation.

Creating Implementation

Let's move to the C implementation code. Let's look the calc.c file.

Here we have the basic math operations and the destructor which pretty much calls free to release memory. We also have the constructor(newCalc) method which is responsible for creating the Struct instance in memory but also to link the function pointer to the concrete implementations.

Coding the Tests

Now we can move to the Unit Test. We will need to do 2 things here. First of all, we will need to create the uni tests that we want and then we will need to set up a test case and run the tests with the main function in C. Let's go.

Here we have the tests on the function: test_new_calc. There are some start and end tests macros here as well. We create a new calc instance and apply assertions like in any other language.

Finally, we need to set up the test suite e and run the tests. Basically, we create a test suite and we add your tests to the suite. If I want I could add more tests and I would not need more runners.

Running the Tests

Before running the tests we need a simple script in order to properly link the tests(we need the link with check framework) then we need run and also delete the tests binary file. Few free to discard my script and use Makefile straight if you like.

Here we use GCC in order to build the code.  There are some dependencies here like Thread and Check(-lcheck) we need include in order to do proper linking. Now we can run $ ./build.sh we you shall see some result like this:

That's it we create simple unit tests with C using Check.

Diego Pacheco

Popular posts from this blog

Podman in Linux

Java Agents

Manage Work not People