:coffee: Simple test framework for C projects with zero dependencies :coffee:
A minimalist testing framework for projects written with C or C++ programming language.
Install globally | Install as dev dependency | Add to your project |
---|---|---|
npm i -g frappu | npm i --save-dev frappu | npm i frappu --save |
How does it work
This tool reads your C/C++ file, attempts to compile and run it, then displays each test status neatly.
Suppose you have the following c function in a file called is_pair.c
inside a src
folder in your project:
// ./src/is_pair.c int
To test this function, you need to write a tester that uses this script:
// ./test/test_is_pair.c int int
At this point you could compile and execute this test yourself and make sure it returns 0 (indicating that nothing is wrong), but that's where this tool comes in: You run frappu
at the root of your project and watch each test be executed and asserted automatically.
Frappu tries to guess you'll want to compile your code with gcc -o file file.c
, but this very often isn't the case and that's why you can specify the compile directives in the tester:
// ./test/test_is_pair.c int int
If you run this tool again, nothing will have changed since the directives are the same as the default ones. (If you are on windows, then the executable filename has changed since the default guesses your executable extension).
Anyways, with these directives you can change how your code is compiled and executed, even executing it multiple times with different shell commands:
// ./test/test_is_pair.c int int
The above code will compile the test once and execute it three times, with different parameters.
You can also change the compile command to whatever you want, including libraries, changing executable (maybe g++ or riscv-gnu-gcc), etc.
Directives
In-source directives are identifier at tester files that help this tool know how to handle your test file. They begin with #define
followed by an uppercase string starting with FRAPPU_ and then their parameter(s).
At the moment, there are 3 directives:
- FRAPPU_COMPILE_COMMAND: A string to compile the tester file.
- FRAPPU_RUN_COMMAND: An array of strings to execute the test.
- FRAPPU_SHOULD_BE_SILENT: Wether to consider the test failed if it tries to write to stdout/stderr.
More details about each directive can be found at their dedicated file.
Installing and Usage
To start, you must have Node v10+ installed with npm v6+ which you can check by running node --version
and npm --version
in your command line.
There are a few ways this tool can be installed and used. There is a list below ordered from the most recommended to the lea:
Create a new npm project and add frappu
to it (If you don't have a npm project)
Add to your existing npm project
Installing it and using it globally
Clone it and execute it (This not recommended as it is an outdated way of doing things)
You can also use the --help
parameter to get basic usage:
Frappu is a framework
Size
This tool is as lightweight as I could make it. Using it in your project takes up 23kb (uncompressed) only.
Security
This tools takes no attempt to rid you of security problems. Running compiled executables can be dangerous especially if you don't know what it does.
So if you are testing a "wipe-out-my-default-hard-drive" function or a "send-my-credit-card-info-to-someone-by-email", it will most likely not be safe to run it.
Use this tool at your own risk. Trust people who write your tests and always make sure they weren't maliciously edited by someone from the internets.
Minimal Example
A basic test can be placed at the folder ./basic-test/basic_test.c
:
int
To test it you need to supply the folder for the tool to find it since it's not the default test
folder:
frappu basic-test
Your terminal should output something like this:
[ OK ] Basic Test
1 passed tests
0 failed tests
That means your test worked! Go ahead and change your source file to make the test fail to see the failure mechanism in action.
Motivation
I feel like C projects need modern testing frameworks.