English | δΈζ
A modern, lightweight, and professional C unit testing framework designed for simplicity and elegance.
cunit provides a clean, structured API for writing and organizing unit tests in C, with both modern macro-based syntax and traditional function calls for maximum flexibility.
- ποΈ Structured API: Modern macro-based syntax with
CUNIT_SUITE_BEGIN
/CUNIT_SUITE_END
blocks - π Easy Integration: CMake support with FetchContent and CPM.cmake
- π― ANSI C99: Standard-compliant code works everywhere
#include "cunit.h"
void test_math_operations(void) {
assert_int_eq(2 + 2, 4);
assert_true(5 > 3);
assert_false(1 > 2);
}
void test_string_operations(void) {
assert_str_eq("hello", "hello");
assert_str_ne("world", "hello");
}
int main(void) {
cunit_init();
CUNIT_SUITE_BEGIN("Math Tests", NULL, NULL)
CUNIT_TEST("Basic Operations", test_math_operations)
CUNIT_SUITE_END()
CUNIT_SUITE_BEGIN("String Tests", NULL, NULL)
CUNIT_TEST("String Comparisons", test_string_operations)
CUNIT_SUITE_END()
return cunit_run();
}
#include "cunit.h"
#include <stdlib.h>
static int *test_array;
static size_t array_size;
void setup_array_tests(void) {
array_size = 5;
test_array = malloc(array_size * sizeof(int));
for (size_t i = 0; i < array_size; i++) {
test_array[i] = (int)i * 2;
}
}
void teardown_array_tests(void) {
free(test_array);
test_array = NULL;
}
void test_array_access(void) {
assert_not_null(test_array);
assert_int_eq(test_array[0], 0);
assert_int_eq(test_array[2], 4);
}
void test_array_bounds(void) {
assert_int_eq(array_size, 5);
assert_int_eq(test_array[array_size - 1], 8);
}
int main(void) {
cunit_init();
CUNIT_SUITE_BEGIN("Array Tests", setup_array_tests, teardown_array_tests)
CUNIT_TEST("Array Access", test_array_access)
CUNIT_TEST("Array Bounds", test_array_bounds)
CUNIT_SUITE_END()
return cunit_run();
}
cmake_minimum_required(VERSION 3.12)
project(MyProject)
include(FetchContent)
FetchContent_Declare(
cunit
GIT_REPOSITORY https://github.com/tayne3/cunit.git
GIT_TAG v0.2.0
)
FetchContent_MakeAvailable(cunit)
# Create your test executable
add_executable(my_tests test_main.c)
target_link_libraries(my_tests PRIVATE cunit::cunit)
# Enable testing
enable_testing()
add_test(NAME my_tests COMMAND my_tests)
include(cmake/CPM.cmake)
CPMAddPackage("gh:tayne3/[email protected]")
add_executable(my_tests test_main.c)
target_link_libraries(my_tests PRIVATE cunit::cunit)
git clone https://github.com/tayne3/cunit.git
cd cunit
mkdir build && cd build
cmake ..
make
Function | Description |
---|---|
cunit_init() |
Initialize the framework |
cunit_cleanup() |
Clean up resources |
cunit_suite(name, setup, teardown) |
Create a test suite |
cunit_test(name, func) |
Add a test to current suite |
cunit_run() |
Run all tests |
cunit_run_suite(name) |
Run specific suite |
Macro | Description |
---|---|
CUNIT_SUITE_BEGIN(name, setup, teardown) |
Begin suite definition |
CUNIT_TEST(name, func) |
Add test to current suite |
CUNIT_SUITE_END() |
End suite definition |
Function | Description |
---|---|
cunit_test_count() |
Get total number of tests |
cunit_failure_count() |
Get number of failed tests |
cunit_suite_count() |
Get number of test suites |
assert_true(condition);
assert_false(condition);
assert_bool(expected, actual);
assert_int_eq(expected, actual);
assert_int_ne(expected, actual);
assert_int_lt(expected, actual);
assert_int_gt(expected, actual);
assert_int_le(expected, actual);
assert_int_ge(expected, actual);
assert_str_eq(expected, actual);
assert_str_ne(expected, actual);
assert_str_case(expected, actual); // Case-insensitive
assert_str_n(expected, actual, n); // First n characters
assert_ptr_eq(expected, actual);
assert_ptr_ne(expected, actual);
assert_null(ptr);
assert_not_null(ptr);
assert_float32_eq(expected, actual);
assert_float64_eq(expected, actual);
// Also: _ne, _lt, _gt, _le, _ge variants