Minunit - Minimum C Language Test Framework
Introduction Source Code Installing a Test Example Conclusion License Appendix: Why use do {} while?
Introduction
The unit test template is quite popular in object-oriented programming languages. Available in templates such as JUnit, Sunit and CPPUnit. However, so many features will enable the developers of the unit test (for example, developers using C language to develop embedded systems) into the inner environment. The focus of unit test is the test instead of test template. Miniunit is an extremely simple C language unit test template. He has no memory allocation, so it can be used in almost any environment including Romable code.
Source code
/ * File: minunit.h * /
#define MU_ASSERT (Message, Test) DO {IF (! (test)) Return Message;} While (0)
#define mu_run_test (test) do {char * message = test (); tests_run ; /
IF (Message) Return Message;} while (0)
EXTERN INT TESTS_RUN;
It only has three lines of code.
Install a test instance
A Minunit test instance is a function that returns 0 (null) by testing. If the test fails, this function should return a test failure description string. MU_ASSERT is a macro that returns a string when the expression fails. Macro MU_RUNTEST repeats the test instance until the test instance fails. This is all of Minunit.
example
The following example runs two tests, a success, one failure.
/ * File minUnit_example.c * /
#include
#include "minloadit.h"
INT tests_run = 0;
INT foo = 7;
INT bar = 4;
Static char * test_foo () {
MU_ASSERT ("Error, Foo! = 7", FOO == 7);
Return 0;
}
Static char * test_bar () {
MU_ASSERT ("Error, Bar! = 5", bar == 5);
Return 0;
}
Static char * all_tests () {
MU_RUN_TEST (TEST_FOO);
MU_RUN_TEST (TEST_BAR);
Return 0;
}
INT main (int Argc, char ** argv) {
Char * result = all_tests ();
IF (Result! = 0) {
Printf ("% s / n", result);
}
Else {
Printf ("all tests passed / n");
}
Printf ("Tests Run:% D / N", Tests_Run;
Return Result! = 0;
}
in conclusion
People generally think that writing a unit test template is complicated. But actually you just need to write a few lines of code. Of course, if you touch the full-featured unit test template as JUnit, you should use all the means. If you don't, you can still use a simple template like MinUnit, otherwise your time will float. This is not the reason for the discarding unit test.
license
You can use this document with this article without any reason.