Introducing utest.h!
So as you may (or may not) already know - I’ve written two tiny C libraries - utf8.h and json.h. One of the questions that most plagued me when writing these libraries was ‘How should I write the tests for them?’ I could put them in a separate repository, I could put them within the repository, should I create a new testing framework every time? It would be useful to be able to have test cases spread across multiple files… and the thoughts went on and on.
Basically what I wanted was a single header variant of googletest - and for it to be able to work in C. I scoured the interwebs a bit, but I couldn’t really find exactly what I wanted. When in doubt, write your own I say!
So I’m introducing utest.h - a single header C/C++ unit tester. It’s licensed under the public domain (via the unlicense), and it works and is tested on Mac OSX, Linux and Windows, with both C and C++ files in the same tested executable. I’ve tried to mimic googletest‘s command line output as much as possible, even using the coloured text output where possible.
To start testing with utest.h, the absolute minimum you need is a TESTCASE(set, name) and UTEST_MAIN() to be in one of your source files. UTEST_MAIN() defines an int main(…) entry point function, so don’t try and define your own! For example;
I use the gcc/clang extension __attribute__((constructor)) to allow multiple files to register test cases pre-main, and an even uglier MSVC workaround to mimic __attribute__((constructor)) on Windows. The bottom line though - it works, and everyone can test across C/C++ to their heart’s delight.
Future work will include having command line arguments to select what test cases to run, and being able to output an xunit/junit xml file so that continuous integrations can easily pick up the results of the test cases. Stay tuned!