utest.h
Anyone who has used the googletest library with a complex C++ codebase will know how powerful this form of unit testing can be for productivity. While the library has a ton of features and is seriously powerful, there are a number of technical deficits that come with it:
- It only works with modern C++.
- It has a complex build system integration, with multiple source files and headers.
- The startup time of using googletest is terrible.
So with all this in mind I wrote my own library - utest.h. The library is:
- 9x faster than googletest.
- Works with C and C++.
- Works with mixing C and C++ in the same testing application (very useful if you want to test a C++ and C API to your code).
- Has test fixtures.
- Allows filtering of test cases.
- Compiles with
-Wall -Werror -Weverything
.
Lets look at an example application:
In a single file (C or C++, it doesn’t matter) you need to need to call the
macro UTEST_MAIN()
to create the main method for the application.
#include "utest.h"
UTEST_MAIN()
Note this doesn’t need to be in a separate C/C++ file to the actual tests!
Then we’ll have two files - a C and a C++ file just to show that it works.
#include "utest.h"
UTEST(from, c) {
ASSERT_TRUE(1);
}
And:
#include "utest.h"
UTEST(from, cpp) {
ASSERT_EQ(true, false);
}
Now we’ll compile this all together with clang and show the output.
Anyone who is familiar with googletest will notice that I’ve intentionally kept the output similar (even colour highlighted) to stop any mental gymnastics that could be involved.
Lastly the license - the code is as close to public domain as I could get in a license, using the unlicense to let any of you do basically anything you want with it.