C Unit Testing Framework
Welcome to c'est la vie (abbreviated as CLV), a unit testing framework for C.
This framework is made to emphasize the importance of unit testing to beginner programmers who are using C. I personally believe learning how to perform test-driven development is an invaluable skill as a programmer, and hence this framework was created.
This framework adopts a plug-and-play mindset when it comes to testing, believing that it should not require a lot of effort to begin writing tests. CLV aims to kickstart the testing process, so that you can immediately get back to producing more and safer code.
DISCLAIMER: This framework can really only be useful if you have written your C code in a modular way. This means your functions to be tested should be decoupled from your file that contains the
mainfunction of your binary.
- First, ensure that the following dependencies are satisfied:
- A C compiler (
gcc,clang) gitmake
- A C compiler (
- Clone this repository (or install a release).
git clone https://github.com/44mira/clv
cd clv- (OPTIONAL) Build
clvfrom source.
go build clv.go # requires go- (OPTIONAL) Add the
clvbinary to your systemPATH.
- Run
clv init(or the path to theclvbinary) in your project's root folder. This should create a directory namedtests/containing the library files for CLV.
NOTE:
clv initwill overwrite directories.
- Create test files in this directory for your usecase. These files should end in a
_test.cor_test.cppsuffix.- Alternatively, you can use
clv create <test_name> [... <test_name>]to create a test from a base template.- Brackets in this notation signify that
clv createcan create multiple test files with the same command.
- Brackets in this notation signify that
- Missing this step will cause an error in building the tests.
- Alternatively, you can use
- You can now write your tests!
- To run your tests, first compile them with
make testsand then simply run thetestbinary found in your project root folder.
NOTE:
make testswill temporarily create abuilddirectory and then clean it up afterwards, removing it. This will overwrite any existing directory namedbuild! If you do not want this behavior, you can edit it in the Makefile.
Example:
$ cd myProject
$ clv init
The tests/ directory has been created.
$ ls
my_code.c
my_code.h
tests/
Makefile
$ clv create smoke
The tests have been created.
$ ls tests/
clv.h
clv.c
clv_internals.h
smoke_test.c#include "clv.h"
#include <stdio.h>
#include "../my_code.h" /* decoupled business functions */
int main() {
SETUP; /* CLV requires that you add a SETUP and TEARDOWN at the end and beginning
of your program lifetime. */
ASSERT((5 == 5)); /* Pass a condition to `ASSERT` and it will document the test result. */
ASSERT_TRUE((5 == 5)); /* ASSERT can also be called as `ASSERT_TRUE` if you prefer a more explicit naming scheme. */
TEST_LABEL("4 is not equal to 5"); /* You can attach an optional test label to your tests by calling this macro before the assert! */
ASSERT_FALSE((4 != 5)); /* You can also use `ASSERT_FALSE` if you prefer it over ASSERT((!condition)) */
TEST_LABEL("4 is an odd number");
ASSERT(myIsOdd(4));
TEARDOWN;
}Make sure to edit the
Makefileso that your C file gets linked!
Output:
$ make tests
...
$ ./test
//=TESTS===========================================\\
OK | TEST #01 LINE 8
OK | TEST #02 LINE 10
FAIL | TEST #03 LINE 16 | 4 is not equal to 5
FAIL | TEST #04 LINE 20 | 4 is an odd number
\\=TESTS===========================================//Check out the wiki!
