Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function mocking (for testing) in C?

I would like to write tests for a C library, in C. I'd like to mock out some functions for the test.

Suppose my library is compiled from the following source:

/* foo.h */
int myfunction(int x, int y);

/* foo.c */
#include "foo.h"

static int square(int x) { return x * x; }

int myfunction(int x, int y) {
    return square(x) + square(y);
}

I want to write a test like this:

/* foo_test.c */
#include "foo.h"

static int square(int x) { return x + 1; }

int main(void) {
    assert(myfunction(0, 0) == 2);
    return 0;
}

Is there any way I can compile so that myfunction will use the definition of square in foo_test.c, instead of the one in foo.c, only when linking the executable foo_test? That is, I want to compile foo.c into a library (let's call it libfoo.so), and then compile foo_test.c with libfoo.so and some magic so that I'll get an executable foo_test which uses the different implementation of square.

It would be helpful to hear solutions for when square is not declared static, but solving the above case would be even better.

EDIT: It seems hopeless, but here's an idea: Suppose I compile with -O0 -g so it's unlikely that square will get inlined and I should have symbols showing where the call was resolved. Is there a way to sneak into the object file and swap out the resolved reference?

like image 846
leif Avatar asked Jan 22 '12 05:01

leif


People also ask

What is a mock for testing?

Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

What is a stub vs mock?

Stub: a dummy piece of code that lets the test run, but you don't care what happens to it. Substitutes for real working code. Mock: a dummy piece of code that you verify is called correctly as part of the test.

Is mocking necessary in unit testing?

A unit test tests one unit (method or class) and uses other units only as much as necessary to achieve that goal. Mocking may be necessary, but it is not the point of the test. An integration test tests the interaction between different actual units.

What are mocks used for?

Mocking means creating a fake version of an external or internal service that can stand in for the real one, helping your tests run more quickly and more reliably. When your implementation interacts with an object's properties, rather than its function or behavior, a mock can be used.


1 Answers

It looks like you are using GCC, so you can use the weak attribute:

The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code, though it can also be used with non-function declarations. Weak symbols are supported for ELF targets, and also for a.out targets when using the GNU assembler and linker.

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

like image 67
David Grayson Avatar answered Sep 28 '22 09:09

David Grayson