- Other languages: 中文
- The cmock is a MOCK tool, which is just for C language.
- The cmock can call/mock any glocal function (including static functions).
- Now, cmock is just working on GNU/Linux of arch AMD64.
- It needs objdump installed which is provided in binutils of Debian’s packages.
GPLv3
The cmock is perl5 prog. To copy cmock to /usr/bin and change mode。
sudo cp cmock /usr/bin && sudo chmod +x /usr/bin/cmock
cmock <Object File>
The cmock analysises <base>.o and makes out <base>_cmock.h, <base>_cmock.c and <base>_cmock.lds . The file <base>_cmock.h supports CMOCK macros. The file <base>_cmock.c supports cmock functions body. The file <base>_cmock.lds supports ld-script for linker.
In the example, some.c is the subject file and test.c is unit testing code. in test.c, you should:
#include "some_cmock.h"
- Phase 1, to compile some.c:
gcc -c -g -Wall -fPIC -o some.o some.c
- Phase 2, using cmock to generate some_cmock.h, some_cmock.c and some_cmock.lds .
cmock some.o
- Phase 3, to compile test.c and some_cmock.c
gcc -c -g -Wall -fPIC -o test.o test.c
gcc -c -g -Wall -fPIC -o some_cmock.o some_cmock.c
- Phase 4, to link all and make out program
gcc -g -Wall -o test_some.out some.o some_cmock.o test.o \
-ldl -lm \
-Wl,-T,some_cmock.lds \
-Wl,--unresolved-symbols=ignore-all
(–unresolved-symbols is optional)
- function void cmock_restore()
To restore relocation to real functions.
- macro CMOCK_CALL(rt, func, args)
To call the real func, return type is rt, argments is args. Example:
int v = CMOCK_CALL(int, foobar, (3));
- macro CMOCK_FUNC_VAL(func, val) To mock func, set return value is val.
CMOCK_FUNC_VAL(foobar, 10);
- macro CMOCK_FUNC(rt, func)
To mock func, return type is rt, set args and body.
CMOCK_FUNC(int, foobar) (int a)
{
return a * 2;
}
- Environment Variable V To set verbose output
V=1 ./test_some.out
- function int cmock_result() To get the result of whole testing.
- macro CMOCK_RESULT(expr) To set the result of this case.
- macro CMOCK_INFO(fmt, …) and CMOCK_ERROR(fmt, …) To print text. The CMOCK_ERROR also sets error result.
- macro CMOCK_CASE(name)
To provide a head of test-case function.
- macro CMOCK_RUN_CASE(name) To run a test case. You should run test cases in main.
Example:
CMOCK_CASE(some)
{
...
CMOCK_RESULT(1);
}
CMOCK_CASE(other)
{
if (foobar() == 0)
CMOCK_INFO("foobar return OK");
else
CMOCK_ERROR("foobar return error");
}
int main()
{
CMOCK_RUN_CASE(some);
CMOCK_RUN_CASE(other);
return cmock_result();
}