[update] : fix README

This commit is contained in:
2026-03-24 20:58:42 +01:00
parent d01f7afe3e
commit 3975d92651
+84 -52
View File
@@ -1,16 +1,20 @@
# y_test_h # y_test_h
It is a copy of a project of test like gtest but write in C only. This is a C-only testing library, similar to gtest but written entirely in C.
To use it, simply copy the y_test_h.h file into your project, include it in your test files, and use the following macros:
But here, it suffice to copy one file "y_test_h.h" and include it in the test file, -`IMPLEMENTATION_FTEST()` to generate all test functions.
and add somme macro functions like `IMPLEMENTATION_FTEST()` to generate all ftest functions
and `IMPLEMENTATION_FMOCK()` to generate all fmock functions. -`IMPLEMENTATION_FMOCK()` to generate all mock functions.
# How to create test (ftest) # How to create test (ftest)
## Example ## Example
Example for this file `test.c`: Example for this file `test.c`:
``` ```
C
```
```
#include "y_test_h.h" #include "y_test_h.h"
IMPLEMENTATION_FTEST() IMPLEMENTATION_FTEST()
@@ -24,23 +28,29 @@ TEST(){
EXPECT_TRUE(true); EXPECT_TRUE(true);
} }
``` ```
### requirement ### Requirements
I write this program to work on linux, I do not test it on windows or mac! This program was developed for Linux. It has not been tested on Windows or macOS yet.
```
Bash
```
``` ```
$ ls $ ls
$ test.c y_test_h.h test.c y_test_h.h
``` ```
### compile ### Compilation
`gcc -o exectest test.c` `gcc -o exectest test.c`
Normaly, it works with `gcc` and `clang` ! Its typically works with `gcc` and `clang`.
### launch ### Running test
`./exectest` `./exectest`
## In function main ## The main function
### If using options: ### Using command-line options:
```
C
```
``` ```
int main(int arc, char** argv){ int main(int arc, char** argv){
run_all_tests_args(argc, argv); run_all_tests_args(argc, argv);
@@ -49,43 +59,52 @@ int main(int arc, char** argv){
``` ```
We can add execution option like: We can add execution option like:
`./exectest -h` to print help `./exectest -h` : Print help
`./exectest -p 5` to parallelize tests, using 5 threads. `./exectest -p 5` : Parallelize tests using 5 threads.
`./exectest` launch sequential test (1 thread). `./exectest` : Run tests sequentially (1 thread).
### If using only sequential tests ### Using only sequential tests
```
C
```
``` ```
int main(){ int main(){
run_all_tests(); run_all_tests();
return 0; return 0;
} }
``` ```
`./exectest` does not read args! Note: In this case, `./exectest` does not read command-line arguments.
### If using only parrallel tests ### Using only parrallel tests
```
C
```
``` ```
int main(){ int main(){
run_all_tests_parallel(4); /* to use 4 threads */ run_all_tests_parallel(4); /* to use 4 threads */
return 0; return 0;
} }
``` ```
`./exectest` does not read args! Note: In this case, `./exectest` does not read command-line arguments.
# FMOCK # FMOCK
We can create and predefined return function regarding the call and condition. You can create functions with predefined return values based on specific calls and conditions to simulate the behavior of real functions.
To simulate response of a function call !
## How to create fmock ## How to create fmock
```
C
```
``` ```
#include "y_test_h.h" #include "y_test_h.h"
IMPLEMENTATION_FMOCK() IMPLEMENTATION_FMOCK()
``` ```
outside all functions: outside of any function:
```
C
```
``` ```
MOCK_FUNC( MOCK_FUNC(
returnType, returnType,
@@ -96,24 +115,28 @@ MOCK_FUNC(
/* use (returnType) in parathesis if the returType has more than 1 words /* use (returnType) in parathesis if the returType has more than 1 words
for example (long int) or (struct someStruct) */ for example (long int) or (struct someStruct) */
``` ```
For example, to create a function mock as signature: ## Example
To mock a function with the signature `int f_mock(int a,int b);`
``` ```
int f_mock(int a,int b); C
``` ```
we use
``` ```
MOCK_FUNC(int, f_mock,(int a,int b),(a,b)) MOCK_FUNC(int, f_mock,(int a,int b),(a,b))
``` ```
args: ## Arguments breakdown
``` ```
returnType: int, returnType: int,
name_function_mock: f_mock, function_name: f_mock,
args prototype with paranthesis: (int a,int b), Prototype: (int a,int b),
args variable names with parathesis (same variable names as prototype): (a,b) Variables : (a,b)
```
## Printing variables
You can define a function to print the mock function's variables for logging purposes. The macro uses similar arguments to `MOCK_FUNC`, but the return type is always `char*`.
Example for `f_mock` :
```
C
``` ```
## 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)){ STR_PRINT_CUR_VAR(f_mock, (int a,int b),(a,b)){
char *ret=malloc(150); char *ret=malloc(150);
@@ -121,34 +144,43 @@ STR_PRINT_CUR_VAR(f_mock, (int a,int b),(a,b)){
return ret; return ret;
} }
``` ```
## define expect call ## Define an expected call
```
C
```
``` ```
EXPECT_MOCK_CALL(int, f_mock, (int a,int b), (a<b), 3){ EXPECT_MOCK_CALL(int, f_mock, (int a,int b), (a<b), 3){
return a+b; return a+b;
} }
``` ```
args: ### Arguments:
``` ```
returnType: int, returnType: int,
name_function_mock: f_mock, function_name: f_mock,
arg prototype of the function with paranthesis: (int a,int b), Prototype : (int a,int b),
conditions to check on args before calling the function:(bool expression): (a<b) Condition : (a<b) (boolean expression checked before execution)
number of repetition response (number of call times): 3 Repetition : 3 (number of times this response is expected)
```
## Define a "will" call
```
C
``` ```
## define will call
``` ```
WILL_MOCK_CALL(int, f_mock, (int a,int b), (a==b), 1){ WILL_MOCK_CALL(int, f_mock, (int a,int b), (a==b), 1){
return a*b; 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. The arguments are the same as `EXPECT_MOCK_CALL`. The difference is that `EXPECT_MOCK_CALL` must be triggered during the test, whereas `WILL_MOCK_CALL` is optional.
## init call and call ## Initializing and calling mocks
in TEST environement, we may use macro `INIT_CALLER_MOCK(f_mock);` before calling `f_mock` to have explicit logs again! In a `TEST` environment, you should use the `INIT_CALLER_MOCK(f_mock)`; macro before calling the mock to get explicit logs.
Call function mock is the same as other normal functions. Calling a mock function is done exactly like a normal functions.
Example: ### Example:
```
C
```
``` ```
TEST(f_mock_test){ TEST(f_mock_test){
INIT_CALLER_MOCK(f_mock); INIT_CALLER_MOCK(f_mock);
@@ -157,13 +189,13 @@ TEST(f_mock_test){
} }
``` ```
# PRINTF # 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. While you can use the standard `printf`, this library provides a `PRINTF` macro. It allows logs to be recorded in files and ensures that logs remain ordered when running parallel tests.
The arguments are identical to the standard `stdio.h` `printf`.
args are the same as `printf` stdio.h function.
example:
``` ```
PRINTF("hello\n"); PRINTF("hello\n");
``` ```
I introduce also an alias `LOG` for `PRINTF` Note : `LOG` is also available as an alias for `PRINTF`