test-beam

Test Beam

Minimal unit testing tool box written in C

About

What is this

Test Beam is a unit testing suite consisting of mostly C preprocessor macros for quick assertion-based unit tests. It is a single-file library consisting solely of the header file beam.h (see Usage for more information).

Credits and Support

All content within this repository is released into the public domain (license text). All kinds of use and redistribution are permitted, but no warranty is given, express or implied. As such, no support should be expected by the original author(s).

If you spot a bug or would like a feature to be added, you can open an Issue, however no kind of active support to this library should be expected.

If you wish to make changes you can issue a PR or redistribute a fork.

To Do

Potential extensions to this toolbox that can be implemented in the future are recorded in the file TODO.md.

Examples

Examples unit tests can be viewed in the examples/ directory.

Usage

Inclusion

The library consists of a single short header file, containing mostly macros, as well as some static global constants and some functions.

While it contains header guards, the function definitions are provided instead of prototype declarations. As such, re-inclusion could be potentially problematic to compilers. What this means is, that this was this header is meant to be included only once throughout the entire project.

If that does not suit your needs, you might have to defer the implementations to a source file or (better) add yourself a separate implementation guard like this:

#ifndef _BEAM_H
#define _BEAM_H
// ... move all macro definitions and forward declarations here


#ifdef BEAM_IMPL
// ... move function definitions here
#endif

#endif

This may require some thought about the static globals because they are used as memory storage for the test and assertion results. As such, it would be best to have them defined once only, not statically in every file that includes the header.

Structure

The framework features 2 concepts: Tests and Assertions. Assertions can either fail or pass, whereas tests can pass, fail or be skipped, the last result meaning that the test is neither successful nor insuccessful and as such should not be registered as either of them.

Assertions are supposed to be used to determine whether or not a test was successful or not, but this is not necessary: a test can be conducted in other ways since the criteria by which tests are judged successful or not is the user’s business.

At the end of the tests, B_SUMMARY() ends the test suite and initializes a new test suite. It also outputs the number of assertions and tests that were conducted.

When a test or assertion is evaluated, Test Beam logs the result to output.

Interface

1. External Access Functions

The macro-functions intended for external access are:

2. External Configuration Flags

The behavior of these macro-functions can be customised by defining the following macros constants which serve as flags before including the header (the value of the macro is not used as a switch, only the state of defined/undefined is). As such, it is probably best to leave their values as undefined.

3. Internal Functions

The following macros are used in the implementation, which means they are not intended to be used without the macro wrappers: