Testing C with Cucumber

I had to write a small c program for a course at university. The program is quite simple, it takes arguments from the command line and output a result.

Naturally I wanted to automate the process of compiling and testing different result from the command line. So I decided to use Cucumber (with a few rspec helper) to do it.

I will show an example with a simple hello world C program.

Getting Started

First, we need to install the cucumber and rspec gem

gem install cucumber && gem install rspec

I began by creating a hello.feature file inside a features directory.

Here is what I wrote inside.

Liquid error: No such file or directory - posix_spawnp

The two scenario are pretty straightforward, I want to compile and execute the program with and without an argument.

We can run our test by simply entering cucumber in the command line. If you do so now, you should get an output with pending test.

To implement our test, we need to create a new directory inside our feature directory called step_definitions. We then create a file named hello_steps.rb Here is the content of this file:

Liquid error: No such file or directory - posix_spawnp

We can run our test and watch it fails. Now all we need to do is to write the implementation in c

Here is my c code

Liquid error: No such file or directory - posix_spawnp

Of course this is an extremly basic example but it should be a good foundation to build upon.