2 * This file is part of mutest, a simple micro unit testing framework for C.
4 * mutest was written by Leandro Lucarella <llucax@gmail.com> and is released
5 * under the BOLA license, please see the LICENSE file or visit:
6 * http://blitiri.com.ar/p/bola/
8 * This header file should be included in the source files that will make up
9 * a test suite. It's used for both C and Python implementation, but when
10 * using the Python implementation you should define the MUTEST_PY macro.
11 * If you implement your mu_run_suites() function yourself, you probably will
12 * need to include this header too (see mkmutest).
14 * Please, read the README file for more details.
17 #include <stdio.h> /* fprintf() */
23 /* verbosity level (each level shows all the previous levels too) */
25 MU_QUIET = 0, /* be completely quiet */
26 MU_ERROR, /* shows errors only */
27 MU_SUMMARY, /* shows a summary */
28 MU_SUITE, /* shows test suites progress */
29 MU_CASE, /* shows test cases progress */
30 MU_CHECK /* shows the current running check */
33 /* print a message according to the verbosity level */
34 #define mu_print(level, ...) \
36 if (mutest_verbose_level >= level) { \
37 if (mutest_verbose_level == MU_ERROR) \
38 fprintf(stderr, __VA_ARGS__); \
40 fprintf(stdout, __VA_ARGS__); \
44 /* print an error message */
45 #define mu_printerr(name, action) \
46 mu_print(MU_ERROR, __FILE__ ":%d: " name " failed, "\
47 action " test case\n", __LINE__);
49 /* modify the internal state so a failure gets counted */
50 #define mutest_count_err ++mutest_failed_checks; mutest_case_failed = 1;
52 /* modify the internal state so a success gets counted */
53 #define mutest_count_suc ++mutest_passed_checks;
55 /* check that an expression evaluates to true, continue if the check fails */
56 #define mu_check_base(exp, name, action, final) \
58 mu_print(MU_CHECK, "\t\t* Checking " name "(" #exp ")...\n"); \
59 if (exp) mutest_count_suc \
62 mu_printerr(name "(" #exp ")", action); \
67 /* check that an expression evaluates to true, continue if the check fails */
68 #define mu_check(exp) mu_check_base(exp, "mu_check", "resuming", continue)
71 * ensure that an expression evaluates to true, abort the current test
72 * case if the check fails
74 #define mu_ensure(exp) mu_check_base(exp, "mu_ensure", "aborting", return)
76 #ifndef MUTEST_PY /* we are using the C implementation */
79 * this function implements the test suites execution, you should generate
80 * a module with this function using mkmutest, or take a look to that script
81 * if you want to implement your own customized version */
84 /* macro for running a single initialization function */
86 #define mu_run_init(name) \
90 mu_print(MU_CASE, "\t+ Executing initialization function " \
91 "'" #name "'...\n"); \
93 mu_print(MU_ERROR, "%s:" #name ": initialization " \
94 "function failed (returned %d), " \
95 "skipping test suite...\n", \
96 mutest_suite_name, r); \
97 ++mutest_skipped_suites; \
101 #endif /* mu_run_init */
103 /* macro for running a single test case */
105 #define mu_run_case(name) \
107 mu_print(MU_CASE, "\t* Executing test case '" #name "'...\n");\
108 mutest_case_name = #name; \
111 if (mutest_case_failed) { \
112 ++mutest_failed_cases; \
113 mutest_suite_failed = 1; \
114 } else ++mutest_passed_cases; \
115 mutest_case_failed = 0; \
117 #endif /* mu_run_case */
119 /* macro for running a single termination function */
121 #define mu_run_term(name) \
123 mu_print(MU_CASE, "\t- Executing termination function '" \
128 #endif /* mu_run_term */
131 * mutest exported variables for internal use, do not use directly unless you
132 * know what you're doing.
134 extern const char* mutest_suite_name;
135 extern int mutest_failed_suites;
136 extern int mutest_passed_suites;
137 extern int mutest_skipped_suites;
138 extern int mutest_suite_failed;
140 extern const char* mutest_case_name;
141 extern int mutest_failed_cases;
142 extern int mutest_passed_cases;
143 extern int mutest_case_failed;
145 extern int mutest_failed_checks;
146 extern int mutest_passed_checks;
148 extern int mutest_verbose_level;
150 #else /* MUTEST_PY is defined, using the Python implementation */
152 /* this increments when the "API" changes, it's just for sanity check */
153 int mutest_api_version = 1;
155 int mutest_case_failed; /* unused, for C implementation compatibility */
157 int mutest_passed_checks;
158 int mutest_failed_checks;
159 void mutest_reset_counters() {
160 mutest_passed_checks = 0;
161 mutest_failed_checks = 0;
164 int mutest_verbose_level = MU_ERROR;
165 void mutest_set_verbose_level(int val) {
166 mutest_verbose_level = val;
169 #endif /* MUTEST_PY */