+0
日增长
1,003
stars
75
forks
14
watchers
146
prs
50
issues
1
releases
README
🧪 utest.h
A simple one header solution to unit testing for C/C++.
Usage
Just include "utest.h" in your code!
The current supported platforms are Linux, macOS and Windows.
The current supported compilers are gcc, clang, MSVC's cl.exe, and clang-cl.exe.
It also works with tcc but with a caveat: the latest release of the tcc compiler (version 0.9.27) lacks a feature for UTEST to work. Make sure to use a tcc that is patched with the constructor attribute extension. Recent Ubuntu and Debian Linux distros ship tcc with that patch already included. If you compile tcc yourself, use the trunk version and it will work as expected.
Command Line Options
utest.h supports some command line options:
--help to output the help message
--filter= will filter the test cases to run (useful for re-running one
particular offending test case).
--list-tests will list testnames, one per line. Output names can be passed to --filter.
--output= will output an xunit XML file with the test results (that
Jenkins, travis-ci, and appveyor can parse for the test results).
--enable-mixed-units will enable the per-test output to contain mixed units (s/ms/us/ns).
--random-order[=] will randomize the order that the tests are ran in. If the optional argument is not provided, then a random starting seed is used.
Design
UTest is a single header library to enable all the fun of unit testing in C and
C++. The library has been designed to provide an output similar to Google's
googletest framework:
[==========] Running 1 test cases.
[ RUN ] foo.bar
[ OK ] foo.bar (631ns)
[==========] 1 test cases ran.
[ PASSED ] 1 tests.
UTEST_MAIN
In one C or C++ file, you must call the macro UTEST_MAIN:
c
UTEST_MAIN()
This will call into utest.h, instantiate all the testcases and run the unit test
framework.
Alternatively, if you want to write your own main and call into utest.h, you can
instead, in one C or C++ file call:
c
UTEST_STATE();
And then when you are ready to call into the utest.h framework do:
c
int main(int argc, const char const argv[]) {
// do your own thing
return utest_main(argc, argv);
}
Define a Testcase
To define a test case to run, you can do the following;
c
include "utest.h"
UTEST(foo, bar) {
ASSERT_TRUE(1);
}
The UTEST macro takes two parameters - the first being the set that the test
case belongs to, the second being the name of the test. This allows tests to be
grouped for convenience.
Define a Fixtured Testcase
A fixtured testcase is one in which there is a struct that is instantiated that
can be shared across multiple testcases.
c
struct MyTestFixture {
char c;
int i;
float f;
};
UTEST_F_SETUP(MyTestFixture) {
utest_fixture-c = 'a';
utest_fixture-i = 42;
utest_fixture-f = 3.14f;
// we can even assert and expect in setup!
ASSERT_EQ(42, utest_fixture-i);
EXPECT_TRUE(true);
}
UTEST_F_TEARDOWN(MyTestFixture) {
// and also assert and expect in teardown!
ASSERT_EQ(13, utest_fixture-i);
}
Top issues
#44Color print on Visual Studio older than 20199 comments · 2020-10-31#170--list-tests show different case order between Linux and Windows4 comments · 2025-05-21#141Test does not run from inside statically linked library (gcc)4 comments · 2023-12-02#66work like doctest for c++?3 comments · 2021-07-04#80error: double-'free' of '<unknown>' [CWE-415] [-Werror=analyzer-double-free]1 comments · 2021-11-05
Latest release
pre-ai pre-ai 2026-06-04I've taken the decision to allow AI tool use in PRs. I can't guarantee that every person before this point made their modifications without the use of AI, but I can guarantee that I did not use AI for any commit from this point or before. I know AI use is a real thorny issue for people, so I wanted to give y'all an opt-out to use a known blessed older version of the library if you so choose.