cmocka – a unit testing framework for C

flattr this!

I’m a big fan of unit testing frameworks. When I developed csync, a bidirectional file synchronizer, I used check to write unit tests from the start. check was ok, but it were running valgrind on your testcases to find memleaks in your code the mode reports were about check. So I needed to add valgrind suppressions to get rid of them. When I started to work on libssh, a library implementing the SSH protocol, I wrote unit tests with check too. libssh is multi platform and also works on Windows and with Visual Studio. So we needed a new unit testing framework which is platform independent and has better code quality. I stumbled upon cmockery, a unit testing framework from Google. It was easy to use, the code looked good and it worked with Visual Studio. The build system sucked, so I added CMake support to produce a NSIS installer for Windows. I sent all my patches upstream but nothing happened. I fixed more bugs and added all patches people posted in their bug tracking system. I tried to talk with friends at Google, but in the end I needed to fork it.

cmocka is a fork and the successor of cmockery. I started to fix a lot of bugs, got all examples working and wrote API documentation with doxygen. The result is this first release version 0.2.0.

cmocka is a great unit testing framework with support for mock objects. Mock objects are simulated objects that mimic the behavior of real objects in a controlled way. Instead of calling the real objects, the tested object calls a mock object that merely asserts that the correct methods were called, with the expected parameters, in the correct order. It is really easy to write a unit test, take a look at the API an get started.

Example:

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

/* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
(void) state; /* unused */
}
int main(void) {
const UnitTest tests[] = {
unit_test(null_test_success),
};
return run_tests(tests);
}

10 thoughts on cmocka – a unit testing framework for C

  1. the installation instructions at github say install it with ./configure make make install etc but there isn’t a configure script – so how do you install it?

  2. Sorry – I was referring to https://github.com/akikoskinen/cmocka/blob/master/INSTALL ..

    Using Cmake – if I do:
    $wget https://open.cryptomilk.org/attachments/download/7/cmocka-0.2.0.tar.gz
    $tar -xvf cmocka-0.2.0.tar.gz
    $mkdir build
    $cd build/
    $cmake ../cmocka-0.2.0/
    $make

    I get: CMake Error at cMakeLists.txt:64(install):
    install TARGETS given NO LIBRARY DESTINATION for shared library target “cmocka_shared”

    Forgive me but I’m a physician not a programmer

    Can you kindly advise me please?

  3. Pingback Raspberry Pi Rehab » Getting things ready for C Programming – sort of …
  4. I had the same problem on my Ubuntu 12.04.2 and cmake 2.8.7. You should replace your ‘download’ version with this master.tar.gz.
    Thanks!

Leave a Reply


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post comment

What is Persona?