suggestion library for mocking C functions

Alan Somers asomers at freebsd.org
Sun Sep 24 16:22:31 UTC 2017


On Sun, Sep 24, 2017 at 1:05 AM, John-Mark Gurney <jmg at funkthat.com> wrote:
> Is there a recommended/supported library for mocking C functions?
>
> I want to write some test cases for functions that call other code
> in the same compilation unit that I want to be able to mock.
>
> There are a few out there, but was wondering if anyone has suggestions?
>
> Thanks.
>
> --
>   John-Mark Gurney                              Voice: +1 415 225 5579

There is no good C mocking library.  The language simply doesn't
provide a good way to do it.  CMockery is the best I've seen.
However, it's got some big limitations because it relies on the linker
to swap out function implementations.  Your other option is to use a
C++ mocking library.  It's usually possible to build your application
as C, but build the tests as C++.  However, you'll still need to make
a few modifications to the application source to be able to mock
functions.  You can probably hide it all in the preprocessor.  If you
go this route, I recommend googletest, which nowadays includes mocking
functionality too.

https://github.com/google/cmockery
https://github.com/google/googletest

-Alan


More information about the freebsd-testing mailing list