170 lines
3.8 KiB
Markdown
170 lines
3.8 KiB
Markdown
# y_test_h
|
|
It is a copy of a project of test like gtest but write in C only.
|
|
|
|
|
|
But here, it suffice to copy one file "y_test_h.h" and include it in the test file,
|
|
and add somme macro functions like `IMPLEMENTATION_FTEST()` to generate all ftest functions
|
|
and `IMPLEMENTATION_FMOCK()` to generate all fmock functions.
|
|
|
|
|
|
# How to create test (ftest)
|
|
## Example
|
|
Example for this file `test.c`:
|
|
```
|
|
#include "y_test_h.h"
|
|
IMPLEMENTATION_FTEST()
|
|
|
|
TEST(nametest){
|
|
EXPECT_EQ(1,1);
|
|
}
|
|
/* allow empty nametest, e.g*/
|
|
TEST(){}
|
|
/* allow duplicate name test, to have suit test */
|
|
TEST(){
|
|
EXPECT_TRUE(true);
|
|
}
|
|
```
|
|
### requirement
|
|
I write this program to work on linux, I do not test it on windows or mac!
|
|
```
|
|
$ ls
|
|
$ test.c y_test_h.h
|
|
```
|
|
|
|
### compile
|
|
`gcc -o exectest test.c`
|
|
|
|
Normaly, it works with `gcc` and `clang` !
|
|
|
|
### launch
|
|
`./exectest`
|
|
|
|
## In function main
|
|
### If using options:
|
|
```
|
|
int main(int arc, char** argv){
|
|
run_all_tests_args(argc, argv);
|
|
return 0;
|
|
}
|
|
```
|
|
We can add execution option like:
|
|
|
|
`./exectest -h` to print help
|
|
|
|
`./exectest -p 5` to parallelize tests, using 5 threads.
|
|
|
|
`./exectest` launch sequential test (1 thread).
|
|
|
|
|
|
### If using only sequential tests
|
|
```
|
|
int main(){
|
|
run_all_tests();
|
|
return 0;
|
|
}
|
|
```
|
|
`./exectest` does not read args!
|
|
|
|
### If using only parrallel tests
|
|
```
|
|
int main(){
|
|
run_all_tests_parallel(4); /* to use 4 threads */
|
|
return 0;
|
|
}
|
|
```
|
|
`./exectest` does not read args!
|
|
|
|
# FMOCK
|
|
We can create and predefined return function regarding the call and condition.
|
|
|
|
To simulate response of a function call !
|
|
|
|
## How to create fmock
|
|
|
|
```
|
|
#include "y_test_h.h"
|
|
IMPLEMENTATION_FMOCK()
|
|
```
|
|
outside all functions:
|
|
```
|
|
MOCK_FUNC(
|
|
returnType,
|
|
name_function_mock,
|
|
(prototype of the function with paranthesis),
|
|
(args when call the funct with parathesis)
|
|
)
|
|
/* use (returnType) in parathesis if the returType has more than 1 words
|
|
for example (long int) or (struct someStruct) */
|
|
```
|
|
For example, to create a function mock as signature:
|
|
```
|
|
int f_mock(int a,int b);
|
|
```
|
|
we use
|
|
```
|
|
MOCK_FUNC(int, f_mock,(int a,int b),(a,b))
|
|
```
|
|
args:
|
|
```
|
|
returnType: int,
|
|
name_function_mock: f_mock,
|
|
args prototype with paranthesis: (int a,int b),
|
|
args variable names with parathesis (same variable names as prototype): (a,b)
|
|
```
|
|
## print variables
|
|
We may define a function to print variables of the mock function, it is usefull in logs, the macro has almost the same args as MOCK_FUNC, without returnType wich is always `char*`.
|
|
For example with `f_mock` we define:
|
|
```
|
|
STR_PRINT_CUR_VAR(f_mock, (int a,int b),(a,b)){
|
|
char *ret=malloc(150);
|
|
sprintf(ret,"(int)a: %d, (int)b: %d",a,b);
|
|
return ret;
|
|
}
|
|
```
|
|
## define expect call
|
|
```
|
|
EXPECT_MOCK_CALL(int, f_mock, (int a,int b), (a<b), 3){
|
|
return a+b;
|
|
}
|
|
```
|
|
args:
|
|
```
|
|
returnType: int,
|
|
name_function_mock: f_mock,
|
|
arg prototype of the function with paranthesis: (int a,int b),
|
|
conditions to check on args before calling the function:(bool expression): (a<b)
|
|
number of repetition response (number of call times): 3
|
|
```
|
|
## define will call
|
|
```
|
|
WILL_MOCK_CALL(int, f_mock, (int a,int b), (a==b), 1){
|
|
return a*b;
|
|
}
|
|
```
|
|
same args as EXPECT_MOCK_CALL, the difference is, the EXPECT_MOCK_CALL has to be called by the test earlier, but not WILL_MOCK_CALL.
|
|
|
|
## init call and call
|
|
in TEST environement, we may use macro `INIT_CALLER_MOCK(f_mock);` before calling `f_mock` to have explicit logs again!
|
|
|
|
Call function mock is the same as other normal functions.
|
|
|
|
Example:
|
|
```
|
|
TEST(f_mock_test){
|
|
INIT_CALLER_MOCK(f_mock);
|
|
PRINTF(" first call: %d\n",f_mock(2,3));
|
|
PRINTF(" second call: %d\n",f_mock(3,3));
|
|
}
|
|
```
|
|
# PRINTF
|
|
We may use standard printf function, but I provide a macro PRINTF to allow us record logs in files and also ordered logs when we use parallel tests.
|
|
|
|
args are the same as `printf` stdio.h function.
|
|
example:
|
|
```
|
|
PRINTF("hello\n");
|
|
```
|
|
I introduce also an alias `LOG` for `PRINTF`
|
|
|
|
|