The C Unit Testing Library on GitHub is a library designed for easy unit testing in C

C Unit Testing Library

contributions welcome

The C Unit Testing Library on GitHub is a library designed for easy unit testing in C. It was written by Brennan Hurst for the purpose of providing a J-Unit-like testing framework within C for personal projects.

The library features basic comparisons such as:

  1. AssertEquals: which compares values to determine if they are equal.
  2. AssertArrayEquals: which compares arrays to determine if they are equal.
  3. AssertAddressEquals: which compares addresses to determine if they are equal.
  4. AssertNull: which determines if a value is null (still to be added).
  5. AssertTrue: which determines if a value is logically true (still to be added).
  6. AssertFalse: which determines if a value is logically false (still to be added).
  7. AssertStructEquals: which determines if two structs are equals (memory level, still to be added).

This document is split up into a number of subsections to help with ease of use. Those are as follows:

  1. Setting Up Inline Testing: focused on testing code using assertions within the code itself. This is useful for debugging, but should not be done in a production testing environment.
  2. Setting Up Unit Testing: focused on use of a separate function for unit testing. This is the best way to use this library.
  3. Development: discussion about the development of the unit testing library.
  4. License: the license this Git repository uses.

If you have any additional questions not covered by this documentation, please reach out Brennan-Stack on GitHub.


Inline Testing


This section discusses how you can use this library to test code inline (that is, without the need for separate functions). This is particularly useful in situations where creating separate test functions may not be possible or you just want to test someting quickly without needing to create a new test function. Note: This is not the proper way to use this repository, and it should be avoided if possible.

Getting Started
To get started with inline testing, all you have to do is import the module, create a new test, and begin executing assertions with that test. The best way to explain this is to show you an example:

#include <unit_test.h>

/*
*   This is an example of executing unit tests inline.
*/
int main()
{
    int a = 0;
    int b = 0;
    //Here we create a new unit_test struct by calling unit_test_init(
   
    );
   
    struct unit_test *test = unit_test_init("Example Inline Test");

    //we then print the header for the test, this is just to display the header
    unit_test_print_header(test);
    //we then execute some assertions inline
    unit_test_assert_int_equals(test, __FILE__, __LINE__, a, b);
    unit_test_assert_long_equals(test, __FILE__, __LINE__, 100L, 100L);
    //we then print the test summary 
    unit_test_print_summary(test);
}

And that's all you have to do to use inline tests. If you don't want to print the header, you don't have to.

Drawbacks to Inline Tests
There are a few drawbacks to inline tests. Specifically, they require printing of results as they run. Another drawback is they are directly in the code you are testing. This is good for quick tests, but would be a bad idea in production builds where you are trying to save space or keep code clean.

Unit Testing fixes these issues by providing the ability for tests to be run separately from the code.


Unit Testing


Unit testing is the correct way to use this repository. It allows the programmer to create individual unit tests and to run them all under conditions of their choice. The best way to show how this works is with code:

#include <unit_test.h>

/*
*   This is an example of a unit test. It is independent from the code which   
*   it executes and contains all of the unit tests. 
*/
int unit_test_1(struct unit_test *test)
{
    //create a dummy variable a
    int a = 0;
    //execute an assert_same_address on identical addresses &a and &a
    unit_test_assert_same_address(test, __FILE__, __LINE__, &a, &a);

    //create some dummy float arrays
    float floatarray1[2] = {0.0};
    float floatarray2[2] = {0.0};

    //execute an assert_float_array_equals on those arrays
    unit_test_assert_float_array_equals(test, __FILE__, __LINE__,
        floatarray1, sizeof(floatarray1), floatarray2, sizeof(floatarray2));
}

/*
*   This is an example of executing unit tests.
*/
int main()
{
    //here we create a new unit test struct using unit_test_init(
   
    );
   
    struct unit_test *test = unit__test_init("Test Unit Tests");

    //here we call unit_test_start which starts the unit test
    unit_test_start(test, &unit_test_1, NULL);

    //we use unit_test_print_total_summary() to print a summary of all unit tests
    unit_test_print_total_summary();
}

The above code does a few things in the main method. First, it creates a new unit_test which holds unit test information. It then starts the unit_test by calling unit_test_start(struct unit_test *test, void (*start)(), void (*print)()) which effectively handles the execution of the unit test pointed to by the function pointer *start and handles the print out of all assertions. The main then calls unit_test_print_total_summary() to print out an overall summary which shows results as a whole.


Development


Development is currently being done by Brennan Hurst. This repository was initially created for the purpose of his personal use. Any contributions and/or suggestions related to this repository are more than welcome.


License


This repository follows under the guidelines of the MIT License, as shown below:

MIT License

Copyright (c) 2021 Brennan-stack

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE

Similar Resources

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

End to end test framework designed for Juce applications

JUCE End to End test framework What is it? This package provides a mechanism to end-to-end test a JUCE application Prerequisites CMake. Must be 3.18 o

Sep 24, 2022

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
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 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
📝 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