cmocka – a unit testing framework for C

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);
}

You may also like...

27 Responses

  1. Patrick Young says:

    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. Github? I don’t have cmocka github repository. You need cmake to configure the project …

  3. Patrick Young says:

    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?

  4. Which cmake vesrsion do you use and on which operating system are you?

  5. Patrick Young says:

    CMake 2.8.9
    Raspberry Pi – Raspbian wheezy raspbian/2013-02-09

  6. Patrick Young says:

    That worked flawlessly. Thank you, sir. You are a gentleman.

  7. Alexander Grubnikov says:

    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!

  8. Philip says:

    Hi this a very much a beginner question – I’ve downloaded cmocka for windows. I’ve created a simple Test.c file which includes cmocka.h, I’ve made a CMakeLists.txt file with the following:
    project (Test)
    add_executable(Test Test.c)
    How can I add cmocka to my build? I am very new to cmake.
    Thanks!

  9. Philip says:

    Never mind – I guess I just copy the following files to my source directory, cmocka.dll, cmocka.lib and cmocka.h

  10. mrudulakeerthiraja says:

    How to use cmocka in my C Project ? or How to integrate cmocka with my C Project?

  11. Dean says:

    Hi Andreas,

    Ive #included CUnit however my compiler complains of undefined reference to the CUnit functions. What could be wrong?

  12. This blog post is about https://cmocka.org and not CUnit. CUinit is a different unit testing framework. You should ask questions there … 🙂

  13. erwin says:

    hi, i’m totally new in CMake and i want to add cmocka to my new project in C. (my OS is windows 10 and i want to create project in vs code or vs 2017)
    can someone explain me in step-by-step (as for fool 🙂 ) what do i need to do?

  14. alok says:

    How do you install or build the cmokca in Redhat/Solaris ?
    Also is it possible ti use make instead of CMake

  15. cmake and/or cmocka raw available in the EPEL repository for Red Hat. cmake was available on the Solaris versions I used, OpenIndiana and OmniOS. But you can also easily build cmake yourself.

  16. Tarun says:

    Does Cmocka have a concept of test suites?

    Also, is it possible to produce one exe/bin for all the tests which would execute all defined unit tests for a project?

  17. Tarun says:

    Ok could you please provide an example? How do you create a test suite?

    Example of creating one exe for all the tests?

  18. I don’t know what you mean. Just write one C file which calls cmake_run_group_tests() and adds all your tests …

  19. Tarun says:

    What if I have tests spread across multiple files? How do I include all tests located in different files in one file with one main method and one call to cmake_run_group_tests()?

    All your examples have one test runner per test file. This means that one exe gets generated per test file

  20. Haven’t include files been invented for that?

  21. Joseph says:

    It doesn’t appear that I can mock functions in macOS with cmocka. Apple’s ld does not support –wrap. Is my only option to run a linux distro in a virtual machine and compile there? I’m researching lld on macOS and see if I can just use a different linker. Any suggestions?

  22. You need a linker which supports it.

  23. Marina says:

    Hi , I started use cmocka and i have a problem with 2 defines, its not defined at all HAVE__SNPRINTF and HAVE_VSNPRINTF , where it must be defined?
    Is it up to the user to define?

  1. Monday, March 18, 2013

    […] Andreas Schneider says: Monday, March 18, 2013 at 10:08 […]

Leave a Reply

Your email address will not be published. Required fields are marked *