A commandline tools that automatically generates table driven tests boilerplate for C source files. Highly inspired by gotests.
Install required dependencies:
- universal-ctags
- libreadtags
- JSON dependency (pick one)
- mustach (Install the libs of json according to the one installed earlier)
Download the latest package release from the release page:
Extract and compile cgentest
$ tar -xf cgentest-x.x.x.tar.gz
$ cd cgentest-x.x.x
$ ./configure
$ make
$ make install # might require superuser role
Source code
int simple(bool is_active, char *name, int age) {}
Generated boilerplate
#include "example.c"
#include <stdlib.h>
#include <stdio.h>
void simple_test(void) {
struct {
char name[100];
struct {
bool is_active;
char * name;
int age;
} parameters;
int expected;
} tests[] = {
};
size_t length = sizeof(tests) / sizeof(tests[0]);
for (size_t idx = 0; idx < length; idx++) {
printf("Running simple_test: %s\n", tests[idx].name);
if (tests[idx].expected == simple(tests[idx].parameters.is_active, tests[idx].parameters.name, tests[idx].parameters.age)) {
printf("\t=== Success ===\n");
} else {
printf("\t=== Failure ===\n");
}
}
}
Require autotools >= 2.71
$ autoreconf -i
If you prefer one json library over the others, you can specify it when running configuration
Options |
---|
--with-jsonc |
--with-cjson |
--with-jansson |
example:
$ ./configure --with-jsonc
If there's no option specified, the configuration will for check available json library and use the first one that it found. Libraries search will be done with this order:
json-c
cJSON
jansson
By default, cgentest will require an input file name and output the boilerplate to stdout.
$ cgentest [options] INPUT_FILE
Available options are as follow.
Long Options | Short Options | Argument type | Description |
---|---|---|---|
version | V | none | Display current cgentest version. |
help | h | none | Display help. |
verbose | v | none | Set verbosity, may be added multiple times. |
output | o | string | Target generated output. Will skip already available function unless force option is given. [default: stdout] |
force | F | none | Force function generation even if the function is already available in target. |
only | O | regex string | Only generate function that match this regex. |
exclude | e | regex string | Exclude generate function that match this regex. Takes precedence over only . |
template | t | string | Path to custom template. |
bin | b | string | Path to universal-ctags binary. If not specified, it'll use the default installed in the system. |
- Generate from
simple.c
and output it to stdout
$ cgentest simple.c
- Generate to
simple_test.c
$ cgentest -o simple_test.c simple.c
- Generate with highest verbosity level
$ cgentest -vvv -o simple_test.c simple.c
- Force generate test (available functions won't be skipped)
$ cgentest -o simple_test.c --force simple.c
- Generate test that match
yes
and skip that matchno
$ cgentest -O yes -e no simple.c
- Generate test using custom template
$ cgentest -t path/to/template simple.c
For boilerplate usage example, you can refer to tests suite directory.
You can use your own mustache template with cgentest.
The json contract is as follow.
Name | Type | Description |
---|---|---|
use_header | boolean | Indicates whether the header will be generated |
source | string | Source of the generated test |
metadata_list | array of metadata | List of functions metadata |
metadata
Name | Type | Description |
---|---|---|
name | string | Name of the function |
parameters | array of parameter | List of parameter of the function |
is_void | boolean | Whether the function is a void function |
is_primitive | boolean | Whether the function is returning a primitive data type |
parameter
Name | Type | Description |
---|---|---|
name | string | Name of the parameter |
type | string | Type of the parameter |
is_last | boolean | Whether it's the last parameter |
{
"use_header": true,
"source": "single_parameter.c",
"metadata_list": [
{
"name": "simple",
"type": "void",
"is_primitive": true,
"is_void": true,
"parameters": [
{
"name": "is_active",
"type": "bool",
"is_last": true
}
]
}
]
}
For custom template example, you can refer to custom template used in this project test suite.