Search code examples
cunit-testingautotoolsautomakecmock

How do you integrate CMock/Unity with Automake?


This is my first time asking a question here, so please let me know if I can improve my question..

I am looking for guidance on how to cleanly build unit tests that require replacement of source files with mocked versions of the source file generated by CMock. This is needed because CMock requires that unit test code using a mock be linked with the mock source instead of the original source.

For example, if I am testing function test_me() that calls foo() in a test source file, test.c, then to mock foo() I will have to compile test_test_me: test_me.c, Mockfoo.c, and test.c.

test_me() is normally compiled as test_me: test_me.c and foo.c

Mockfoo.c is generated using a Ruby script provided by CMock using foo.h.

As you can see, foo.c must be replaced with Mockfoo.c in order to create the test program, test_test_me. Mockfoo.c is needed so test_test_me can link to mocked calls of foo, rather than the original foo call. Replacing the source becomes difficult when the number of dependencies increase.

For example, if test_me uses foo, bar, and baz from library libfoo.a, which is composed of foo.o, bar.o, and baz.o, and I want to just mock calls to foo, then I need to build libfoo.a with compilation units Mockfoo.o, bar.o, and baz.o to then link to my test.o that uses the mock foo calls. This gets even more complicated when you have permutations of mocks (e.g. Mockfoo, Mockbar, but not baz) and a large source tree with different automake rules, flags, etc.

Furthermore, the addition of mocks must be simple since mocks are often needed for our unit tests.

Some context:

I am interested in using CMock/Unity to add unit tests in cases where code is refactored or new code is added to a large C project to which I have just joined.

The project currently uses Automake as its build tool and it is here to stay.

I am new to Automake. I am familiar with SCons and CMock.


Solution

  • The question was posted quite a while ago. I will post my solution for anyone else who faces a similar question.

    The most reliable way to interface with CMock/Unity is to use the rake, the Ruby based build tool. rake can be called from your Makefile.am. Targets can be created to call rake for each phony target specified to build your unit tests.

    In my particular case, I was not able to use rake because of the build environment limitations. Instead, I chose to use cmocka which has functionality provided in CMock/Unity and is easily integrated with automake.