A micro unit-testing library for C/C++

µ-test

A micro unit testing framework for C/C++ to get you up and running with unit-testing ASAP (even without libc).

Usage

Simply include the C and header file in your codebase and register/run your tests. Each test consists of:

  • A test funtion (int (*fn)(void)) that returns TEST_PASS or TEST_FAIL
  • Optional setup function (void (*fn)(void), NULL if not used)
  • Optional teardown function (void (*fn)(void), NULL if not used)
  • A name

In your entrypoint, register each test and then call run_tests().

µ-test works without the standard library, as well! Compile with -D NO_STD to avoid including stdlib/stdio. Then in your main routine, set the function pointer int (*utest_printer)(const char *, ...) to provide a replacement for printf(3).

By default, output (e.g. from printf(3)) is only echoed when a test fails. To turn off stdout/stderr capturing, set utest_capture = 0.

Provided assertions

  • ASSERT_EQ(A, B)
  • ASSERT_UNSIGNED_EQ(A, B)
  • ASSERT_PTR_EQ(A, B)
  • ASSERT_NEQ(A, B)
  • ASSERT_UNSIGNED_NEQ(A, B)
  • ASSERT_PTR_NEQ(A, B)
  • ASSERT_ALMOST_EQ(A, B, D)
  • ASSERT_UNSIGNED_ALMOST_EQ(A, B, D)
  • ASSERT_FLOAT_EQ(A, B, D)
  • ASSERT_NULL(A)
  • ASSERT_NOT_NULL(A)

Example

demo.c:

#include <stdlib.h>

static void *handle;

void lib_init(void) {
    if ((handle = malloc(512)) == NULL)
        exit(EXIT_FAILURE);
}

void lib_shutdown(void) {
    if (handle)
        free(handle);
}

int lib_fn(int a, int b) { return a + b; }

#ifdef TEST

#include "utest.h"

int test_should_pass(void) {
    ASSERT_EQ(5, lib_fn(2, 3));
    return TEST_PASS;
}

int test_should_fail(void) {
    ASSERT_EQ(7, lib_fn(2, 3));
    return TEST_PASS;
}

int test_explicit_fail(void) { FAIL(); }

int main(void) {
    register_test(test_should_pass, lib_init, lib_shutdown, "should pass");
    register_test(test_should_fail, lib_init, lib_shutdown, "should fail");
    register_test(test_explicit_fail, lib_init, lib_shutdown, "explicit fail");

    return run_tests();
}

#endif
$ gcc -o -c utest.o utest.c
$ gcc -D TEST -o -c demo.o demo.c
$ gcc -o demo demo.o utest.o
$ ./demo

should pass       ... ok
should fail       ... failed: demo.c+27: 7 != 5
explicit fail     ... failed: demo.c+32: explicit fail

Summary: 1 passed, 2 failed.
Owner
Trevor McKay
Cal Poly, SLO - Computer Engineering | RISC-V, Linux, math, systems programming
Trevor McKay
Similar Resources

📝 One of the difficult unit tester for ft_containers project

📝 One of the difficult unit tester for ft_containers project

ft_containers-unit-test About ft containers unit test is a complete testing for project of school 21/ecole 42 and allowing you test your containers: V

Oct 1, 2022

Various Framework to do Unit Test in C++

Various Framework to do Unit Test in C++

Unit Test in C++ There are many frameworks to performs unit test in C++, we will present the most popular ones and show how to use them. The testing f

Nov 18, 2021

Upp11 - C++11 lightweight single header unit test framework

upp11 Lightweight C++11 single header unit test framework To use framework: Copy upp11.h in you project dir. Create unit test source files or modify e

Apr 4, 2019

Header-only C++11 library for property-based testing.

autocheck Header-only C++11 library for QuickCheck (and later, SmallCheck) testing. Please consult the wiki for documentation. Install conan remote ad

Apr 18, 2022

The fastest feature-rich C++11/14/17/20 single-header testing framework

The fastest feature-rich C++11/14/17/20 single-header testing framework

master branch Windows All dev branch Windows All doctest is a new C++ testing framework but is by far the fastest both in compile times (by orders of

Sep 26, 2022

Googletest - Google Testing and Mocking Framework

GoogleTest OSS Builds Status Announcements Release 1.10.x Release 1.10.x is now available. Coming Soon Post 1.10.x googletest will follow Abseil Live

Sep 24, 2022

Practical mutation testing tool for C and C++

Mull Mull is a tool for Mutation Testing based on LLVM/Clang with a strong focus on C and C++ languages. For installation and usage please refer to th

Sep 15, 2022

C++ xUnit-like testing framework without macros

tst C++ testing framework. Installation, documentation, tutorials See WiKi. Features xUnit-like concepts minimal use of preprocessor macros declarativ

Sep 26, 2022

proftest is a C application for testing the quality of different operating system APIs for profiling.

proftest is a C application for testing the quality of different operating system APIs for profiling.

Jul 23, 2021
Related tags
A micro unit-testing library for C/C++

µ-test A micro unit testing framework for C/C++ to get you up and running with unit-testing ASAP (even without libc). Usage Simply include the C and h

Dec 8, 2021
UT: C++20 μ(micro)/Unit Testing Framework
UT: C++20 μ(micro)/Unit Testing Framework

"If you liked it then you "should have put a"_test on it", Beyonce rule UT / μt | Motivation | Quick Start | Overview | Tutorial | Examples | User Gui

Sep 28, 2022
A complete unit testing framework in a header

liblittletest A complete unit testing framework in a header liblittletest is an easy to use all-in-an-header testing framework; all you have to do in

Nov 11, 2021
Modern c++17 unit testing framework on Microsoft Windows, Apple macOS, Linux, iOS and android.
Modern c++17 unit testing framework on Microsoft Windows, Apple macOS, Linux, iOS and android.

tunit Modern c++17 unit testing framework on Windows, macOS, Linux, iOS and android. Continuous Integration build status Operating system Status Windo

Apr 5, 2022
Kernel-mode C++ unit testing framework in BDD-style

There is a lack of unit testing frameworks that work in OS kernel. This library closes that gap and is targeted for windows driver developers.

Jun 11, 2022
C unit tests with a small header-only library.
C unit tests with a small header-only library.

C unit tests Minimalistic unit tests in C. Uses the __attribute__((constructor)) which, as far as I know, is supported by GCC and clang. So this proba

Aug 28, 2022
A C++ micro-benchmarking framework

Nonius What is nonius? Nonius is an open-source framework for benchmarking small snippets of C++ code. It is very heavily inspired by Criterion, a sim

Apr 13, 2022
A modern, C++-native, header-only, test framework for unit-tests, TDD and BDD - using C++11, C++14, C++17 and later (or C++03 on the Catch1.x branch)
A modern, C++-native, header-only, test framework for unit-tests, TDD and BDD - using C++11, C++14, C++17 and later (or C++03 on the Catch1.x branch)

Catch2 v3 is being developed! You are on the devel branch, where the next major version, v3, of Catch2 is being developed. As it is a significant rewo

Sep 26, 2022
A dynamic mock tool for C/C++ unit test on Linux&MacOS X86_64

lmock 接口 替换一个函数,修改机器指令,用新函数替换旧函数,支持全局函数(包括第三方和系统函数)、成员函数(包括静态和虚函数)

Sep 20, 2022
A modern, C++11-native, single-file header-only, tiny framework for unit-tests, TDD and BDD (includes C++98 variant)

lest – lest errors escape testing This tiny C++11 test framework is based on ideas and examples by Kevlin Henney [1,2] and on ideas found in the CATCH

Sep 19, 2022