Skip to content

nguyenhuy0905/simple-utest

Repository files navigation

Simple Unit Test

A straightforward unit test utility for the C language

Output demo

Syntax demo

Disclaimer

  • This is mainly designed for Linux.
    • If you're on MacOS or any POSIX-compliant OS, you probably still can use this well
    • If you're on Windows, I have bad news for you
  • Due to the use of __attribute__ and 0b in the code, this is only compatible with compilers that support such features (gcc, clang, and perhaps more)
  • Well, technically also a convenient utility for my other, left-behind project.
  • If you want something that is as straightforward but much more featureful, check out Criterion. This, for now, is meant for my learning this language.
  • Due to the design of this utility, running tests concurrently or multi-threadedly is very difficult. You can still spawn threads and run your program however, just be sure that what is passed into assertions is the final result of the multi-thread process.

Requirements

  • clang or gcc or any gcc-compliant C compilers.
  • C23. This is important for parameterized tests to work.
    • The utility does utilize the macro __VA_OPT__

Installation: build from source

  • Make sure you have clang (version 17 or higher) installed. (Older versions may work but it's not a guarantee) (gcc will work as well, but will need a tiny extra step)
  • Clone this repo and build
git clone https://github.com/nguyenhuy0905/simple-utest.git
cd ./simple-utest
# if you only want to build the library
make 
# or make CC=gcc if you use gcc
# or make CC=gcc OPTS=-g if you want to include debug flags 
# for the library (which is only necessary if you need to test out the library)
  • Optionally, to compile and run the demo files, run this command instead:
make test
# or make test OPTS=-g if you want to include debug flags 
# in the library and the test
  • If you're on Windows, or if you're just lazy managing all dependencies of this project, simply use the provided Docker builds to run the demo test files:
    • Make sure you have installed Docker
    • Create a file named output.txt.format, placing it in the same directory as the Makefile
    • Run the following:
    docker build -t simpletest .
    docker compose up -d
    cat output.txt.format
    

Installation: download package

  • Download a release binary here, preferably the latest release. Note: as of now, the releases are not ready for "professional" use.
  • Unzip to the directory of the project you would like to write tests on, (optionally,) rename the folder to simple-utest.

How to use

/* simply include it */
#include "/path/to/simple-utest/simple-utest.h"
  • Then, when compiling:
# or gcc
clang -o your-test-executable your-file-name.c -L/path/to/simple-utest/ -lsimple-utest
  • To execute the test file:
LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/path/to/simple-utest/ /path/to/your-test-executable
  • You can combine a few test files into one test executable to better utilize the 128 max-test cap.
  • Or, of course, you can just combine each test file as its own executable.

Plans

  • Write some documentation
  • Finish documentation
  • Release a binary package
  • Allow user-defined assertion
  • Implement parameterized tests
  • Let users define more complex test with multiple comparisons in one
  • Add pseudo-cross-platform functionality ;)

Issues

  • Float or double assertions returning wrong results, especially if mathematical expressions are included/executed. This is due to how the variable types work. I will modify the method to request for degree of precision.

Changelog

About

A pretty straightforward test utility for the C programming language.

Topics

Resources

License

Stars

Watchers

Forks