Skip to content
arnaudbrejeon edited this page Sep 13, 2010 · 22 revisions

CSpec : Behavior-driven development in C

Overview

CSpec is a Behavior-driven Development framework for C.
It provides a spec framework for describing the behavior of the functions of your system.
The syntax is inspired from RSpec to be as legible as possible.
The source code is as portable and as light as possible to make it easy to run the library on embedded devices.

Spec framework sample

Here is some sample code to describe strcmp from the C standard library:

#include <stdio.h>
#include <string.h>
#include "cspec.h"
#include "cspec_output_verbose.h"

DESCRIBE(strcmp, "int strcmp ( const char * str1, const char * str2 )")

	IT( "returns 0 only when strings are equal" )
		SHOULD_EQUAL( strcmp("hello", "hello"), 0)
		SHOULD_NOT_EQUAL( strcmp("hello", "world"), 0)
	END_IT

	IT( "returns a negative integer when str1 is less than str2"  )
		SHOULD_BE_TRUE( strcmp("hello", "world") < 0 )
		SHOULD_BE_TRUE( strcmp("0123", "1321431") < 0 )
	END_IT

	IT( "returns a positive integer if str1 is greater than str2"  )
		SHOULD_BE_TRUE( strcmp("yellow", "world") > 0 )
		SHOULD_BE_TRUE( strcmp("9", "789") > 0 )
	END_IT

END_DESCRIBE

void main()
{
	CSpec_Run( DESCRIPTION( strcmp ), CSpec_NewOutputVerbose() );
}

Directory sample_skip contains an example the specifications of a small skip list library.

Documentation

CSpec is mostly based on the macros defined in cspec.h . The macros can be differentiated in two groups: structural macros that define the structure of the specification and expectation macros that describe the tests to be evaluated.

Writing the spec of a function is quite straightforward:

  1. Enclose your spec in between the macros DESCRIBE and END_DESCRIBE with function name and caption
  2. Enclose each part of the spec between the macros IT and END_IT with the caption
  3. For each part of the specification, the tests should be evaluated with one of the expectation macros

That’s it. Your specification is ready to be run.

Running a spec is done by calling CSpec_Run with the spec name (using macro DESCRIPTION ) and selecting an output type.

There are currently 3 basic outputs provided in CSpec:

  • OutputVerbose : provides much information as possible: captions, evaluated tests and results
  • OutputUnit : provides less information, concentrated more one the number of failed tests (similar to unit tests)
  • OutputHeader : provides information that does not depend on the tests result and can be used as function header

It is easy to define other outputs depending on your needs and platform constraints.

TODO: explain how to make new output.

How to get it

git clone git://github.com/arnaudbrejeon/cspec.git 

or directly download:

Future & ideas

  • Put in place a story framework
  • Check how to mock
  • Automake autoconf

Links about BDD

Drop me an email in case you have ideas, requests, found bugs, or just if you like the project.

Arnaud Brejeon

arnaud.brejeon@laposte.net