Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 24 Sep 2017 10:22:28 -0600
From:      Alan Somers <asomers@freebsd.org>
To:        John-Mark Gurney <jmg@funkthat.com>
Cc:        "freebsd-testing@freebsd.org" <freebsd-testing@freebsd.org>
Subject:   Re: suggestion library for mocking C functions
Message-ID:  <CAOtMX2iGwcLgYNbs0KUSOpnQ2kWSifY=cFekesVNBtudvpdPtA@mail.gmail.com>
In-Reply-To: <20170924070506.GQ64616@funkthat.com>
References:  <20170924070506.GQ64616@funkthat.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Sep 24, 2017 at 1:05 AM, John-Mark Gurney <jmg@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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAOtMX2iGwcLgYNbs0KUSOpnQ2kWSifY=cFekesVNBtudvpdPtA>