Minunit is a minimal unit testing framework for C/C++ with a modular design and colored output support.
I've included the original code written by John Brewer when he described his idea for a mininal C unit test framework that uses no memory allocation and should "work fine under almost any circumstance" and which could be used to do unit testing in a constrained environment such as an embedded system written in C such as a BIOS, EEPROM, etc. Link: https://jera.com/techinfo/jtns/jtn002
My changes add explicit feature test macro definitons and moved extended functionality to extension files. I've included previous versions in order to display how a simple unit test framwork can evlolve and be customized. My changes add verbose output with color coding based on check50 from the CS50 Intro to Computer Science course at Harvard. This is a learning project.
- Single header core (
minunit.h) - Extensions (created by David Sewell):
- Modular extensions system
- Colored verbose output mode
- JTN002 original code from John Brewer
- Built-in timing utilities
- Cross-platform support
- No external dependencies
minunit/
├── examples/ # Example test files
│ ├── minunit_example.c # Basic usage example
│ ├── verbose_example.c # Verbose output example
│ └── jtn002_example.c # JTN002 compatibility example
├── extensions/ # Modular extensions
│ ├── assertions/ # Enhanced assertion macros
│ ├── os/ # OS-specific functionality
│ ├── timing/ # Timer utilities
│ └── verbose/ # Verbose test output
├── minunit.h # Core header file
└── Makefile # Build system
- Copy
minunit.hto your project - Include any desired extensions
- Write your tests
- Compile and run
# Build all examples
make all
# Run all examples
make run
# Clean build artifacts
make clean#include "minunit.h"
/* Define a test */
MU_TEST(test_check) {
mu_check(5 == 5);
}
/* Define a test suite */
MU_TEST_SUITE(test_suite) {
MU_RUN_TEST(test_check);
}
int main(int argc, char *argv[]) {
UNUSED(argc); /* Silence unused parameter warning */
UNUSED(argv); /* Silence unused parameter warning */
MU_RUN_SUITE(test_suite);
MU_REPORT();
return MU_EXIT_CODE;
}For more detailed, colored output:
#include "minunit.h"
#include "extensions/verbose/minunit_verbose.h"
/* Define a verbose test */
MU_TEST_VERBOSE(test_check_verbose) {
mu_check_verbose(5 == 5);
return NULL; /* Success */
}
/* Define a test suite */
MU_TEST_SUITE(test_suite) {
MU_RUN_TEST_VERBOSE(test_check_verbose);
}
int main(int argc, char *argv[]) {
UNUSED(argc);
UNUSED(argv);
MU_RUN_SUITE_VERBOSE(test_suite);
MU_REPORT_VERBOSE();
return MU_EXIT_CODE;
}For compatibility with the original JTN002 style:
#include "jtn002.h"
static char * test_foo() {
mu_assert("error, foo != 7", foo == 7);
return 0;
}
static char * all_tests() {
mu_run_test(test_foo);
return 0;
}
int main(int argc, char **argv) {
UNUSED(argc);
UNUSED(argv);
char *result = all_tests();
printf("%s\n", result ? result : "ALL TESTS PASSED");
printf("Tests run: %d\n", tests_run);
return result != 0;
}mu_check(condition)mu_fail(message)mu_assert(condition, message)mu_assert_int_eq(expected, result)mu_assert_double_eq(expected, result)mu_assert_string_eq(expected, result)
mu_check_verbose(condition)mu_fail_verbose(message)mu_assert_verbose(condition, message)mu_assert_int_eq_verbose(expected, result)mu_assert_double_eq_verbose(expected, result)mu_assert_string_eq_verbose(expected, result)
MU_TEST(test_name)MU_TEST_VERBOSE(test_name)MU_TEST_SUITE(suite_name)
MU_RUN_TEST(test)MU_RUN_TEST_VERBOSE(test)MU_RUN_SUITE(suite_name)MU_RUN_SUITE_VERBOSE(suite_name)
MU_SUITE_CONFIGURE(setup_fun, teardown_fun)
UNUSED(x)- Silence unused parameter warnings
The project includes a Makefile with the following targets:
make all # Build all examples
make minunit_example # Build basic example
make verbose_example # Build verbose example
make jtn002_example # Build JTN002 example
make run # Build and run all examples
make clean # Remove build artifactsDavid Siñuela Pastor [email protected]
David Sewell [email protected]
MIT License - See MIT-LICENSE.txt