A unit testing framework written in C.
KritiC is a lightweight, fast, and portable unit testing framework for C, C++, and Assembly projects. It aims to provide clean syntax and minimal setup.
Write your tests:
#include "kritic.h"
KRITIC_TEST(math, addition) {
KRITIC_ASSERT_EQ(2 + 2, 4);
}
KRITIC_TEST(math, subtraction) {
KRITIC_ASSERT_EQ(5 - 3, 2);
}
KRITIC_TEST(attributes, dependency) {
KRITIC_ASSERT(1);
}
KRITIC_TEST(attributes, dependent,
KRITIC_DEPENDS_ON(attributes, dependency))
{
KRITIC_ASSERT(1);
}
- Define tests with
KRITIC_TEST(suite, name)
- Use attributes in tests:
KRITIC_DEPENDS_ON(suite, name)
: marks a test as dependent on another test's success; if the dependency fails or is skipped, the current test is skipped automatically
- Make assertions:
KRITIC_ASSERT(expr)
: asserts thatexpr
is trueKRITIC_ASSERT_NOT(expr)
: asserts thatexpr
is falseKRITIC_ASSERT_EQ(x, y)
: asserts thatx
equalsy
(generic: supportsint
,long long
,float
,double
, andconst char*
)KRITIC_ASSERT_NE(x, y)
: asserts thatx
does not equaly
(generic: supportsint
,long long
,float
,double
, andconst char*
)KRITIC_ASSERT_EQ_INT(x, y)
: asserts thatx
andy
are equal using integer comparisonKRITIC_ASSERT_NE_INT(x, y)
: asserts thatx
andy
are not equal using integer comparisonKRITIC_ASSERT_EQ_FLOAT(x, y)
: asserts thatx
andy
are approximately equal using float/double comparison (with delta tolerance)KRITIC_ASSERT_NE_FLOAT(x, y)
: asserts thatx
andy
are not approximately equal using float/double comparisonKRITIC_ASSERT_EQ_STR(x, y)
: asserts thatx
andy
are equal using string comparisonKRITIC_ASSERT_NE_STR(x, y)
: asserts thatx
andy
are not equal using string comparisonKRITIC_FAIL()
: forces a test failure
- Skip tests conditionally with
KRITIC_SKIP(reason)
- Tests are discovered automatically without manual registration
- All printers (assertions, summaries, pre/post test messages) can be overridden
- Standard output can be automatically formatted/redirected during test execution
- Measure the precise runtime (nanosecond precision) of individual tests
- Timers and stdout redirection can be fully disabled at compile time by defining the
KRITIC_DISABLE_TIMER
andKRITIC_DISABLE_REDIRECT
macros/flags - Floating-point usage in default printers (primarily for the timer output) can be disabled by defining the
KRITIC_DEFAULT_PRINTERS_NO_FLOAT
macro/flag
To use KritiC in freestanding cases, start by defining KRITIC_DISABLE_TIMER
and KRITIC_DISABLE_REDIRECT
, and by providing custom print functions by overriding the macros in src/defaults.h, namely: kritic_error_printer
, kritic_error_printerf
, kritic_printer
, kritic_printerf
, and kritic_snprintf
with your own implementations. Alternatively, you can override the members in the kritic_override_pointers
struct, for which you can find the implementations in src/kritc.c.
Furthermore, systems that do not support floating-point have the option to disable its usage in default printers (primarily used for timer outputs) with KRITIC_DEFAULT_PRINTERS_NO_FLOAT
flag. Note that this does not disable printing floats in case of an assertion, nor does it disable KRITIC_ASSERT_EQ_FLOAT
/KRITIC_ASSERT_NE_FLOAT
assertions.
Currently, KritiC is designed to be added to your project as a Git submodule.
git submodule add https://github.com/Wrench56/KritiC.git extern/kritic
cd extern/kritic
make static
#include "kritic.h"
KRITIC_TEST(hello, world) {
KRITIC_ASSERT(1);
}
clang -Iextern/kritic -o tests tests.c extern/kritic/build/libkritic.a