Skip to content

haruInDisguise/libtest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Test framework

Yet another tiny unit testing framework for C.

Features

  • xUnit unit testing style
    • Every test case is part of a suit, that can define a 'setup' and 'teardown' function.
  • Header only, STB style library
  • Automatic test/suit registration
  • Filter tests/suits using basic glob patterns
  • Lightweight and should (hopefully) be easily extendable/hackable.

Planned features:

  • Timing related information

Usage

Since this is an STB style library, meaning you can just include the test header directly. The TEST_IMPLEMENATION macro must only be defined in a single source file.

At least one suit and test case must be defined. Failure to do so will result in a linker error.

A usage pattern could look like the following:

/* file: main.c */
#define TEST_IMPLEMENTATION
#include <test/test.h>

int main(int argc, char **argv) {
    if(!test_setup(argc, argv)) {
        return false;
    }

    test_run_all();
    test_exit();
}

/* file: test_stuff.c */
#include <test/test.h>

void example_setup(void) {
    puts("I am executed before every test of the suit 'example_suit'!");
}

void example_teardown(void) {
    puts("I run after every test!");
}

SUIT(example_suit, example_setup, example_teardown);

TEST(example_suit, test_one) {
    char *some_string = "Hello";
    assert_eq("Hello", some_string);
}

To compile this is example, you would write:

cc -Iinclude/test main.c test_stuff.c -o <binary_name>

The resulting binary accepts the following options:

Usage
      <test_binary> [OPTIONS] ... -- [ARGS]

ARGS
      Arguments that will be ignored by this library

OPTIONS
      The binary build with this library accepts the following options:

      --help
        Display this message

      --list
        Print all registered test cases. Respectes filters.

      --filter <filters>
        A list of filter patterns to selectively execute tests/suits.
        Patterns can contain '*' (matches zero or more characters),
        '?' (matches a single character) or the following characters:
            A-Z, a-z, 0-9, ':', '_'
        To specify multiple patterns, separated them by commas.

      --output <file>
        Redirect library output to a new file.

      --colored (auto|always|never)
        Colorize the output.

Resouces

Existing frameworks that inspired and helped me:

About

A small unit testing framework for C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published