cmocka  0.3.2
Macros | Functions
Running Tests

This is the way tests are executed with CMocka. More...

Macros

#define unit_test(f)   { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
 Initializes a UnitTest structure. More...
 
#define unit_test_setup(test, setup)
 Initializes a UnitTest structure with a setup function. More...
 
#define unit_test_setup_teardown(test, setup, teardown)
 Initialize an array of UnitTest structures with a setup function for a test and a teardown function. More...
 
#define unit_test_teardown(test, teardown)
 Initializes a UnitTest structure with a teardown function. More...
 

Functions

void fail (void)
 Forces the test to fail immediately and quit.
 
void fail_msg (const char *msg,...)
 Forces the test to fail immediately and quit, printing the reason.
 
int run_test (#function)
 Generic method to run a single test. More...
 
int run_tests (const UnitTest tests[])
 Run tests specified by an array of UnitTest structures. More...
 

Detailed Description

This is the way tests are executed with CMocka.

The following example illustrates this macro's use with the unit_test macro.

* void Test0(void **state);
* void Test1(void **state);
*
* int main(void)
* {
* const UnitTest tests[] = {
* unit_test(Test0),
* unit_test(Test1),
* };
*
* return run_tests(tests);
* }
*

Macro Definition Documentation

#define unit_test (   f)    { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }

Initializes a UnitTest structure.

#define unit_test_setup (   test,
  setup 
)
Value:
_unit_test_setup(test, setup), \
#define unit_test(f)
Initializes a UnitTest structure.
Definition: cmocka.h:1300

Initializes a UnitTest structure with a setup function.

#define unit_test_setup_teardown (   test,
  setup,
  teardown 
)
Value:
_unit_test_setup(test, setup), \
unit_test(test), \
_unit_test_teardown(test, teardown)
#define unit_test(f)
Initializes a UnitTest structure.
Definition: cmocka.h:1300

Initialize an array of UnitTest structures with a setup function for a test and a teardown function.

Either setup or teardown can be NULL.

#define unit_test_teardown (   test,
  teardown 
)
Value:
unit_test(test), \
_unit_test_teardown(test, teardown)
#define unit_test(f)
Initializes a UnitTest structure.
Definition: cmocka.h:1300

Initializes a UnitTest structure with a teardown function.

Function Documentation

int run_test ( function)

Generic method to run a single test.

Parameters
[in]functionThe function to test.
Returns
0 on success, 1 if an error occured.
* // A test case that does nothing and succeeds.
* void null_test_success(void **state) {
* }
*
* int main(void) {
* return run_test(null_test_success);
* }
*
int run_tests ( const UnitTest  tests[])

Run tests specified by an array of UnitTest structures.

Parameters
[in]tests[]The array of unit tests to execute.
Returns
0 on success, 1 if an error occured.
* static void setup(void **state) {
* int *answer = malloc(sizeof(int));
* assert_non_null(answer);
*
* *answer = 42;
*
* *state = answer;
* }
*
* static void teardown(void **state) {
* free(*state);
* }
*
* static void null_test_success(void **state) {
* (void) state;
* }
*
* static void int_test_success(void **state) {
* int *answer = *state;
* assert_int_equal(*answer, 42);
* }
*
* int main(void) {
* const UnitTest tests[] = {
* unit_test(null_test_success),
* unit_test_setup_teardown(int_test_success, setup, teardown),
* };
*
* return run_tests(tests);
* }
*
See Also
unit_test
unit_test_setup
unit_test_teardown
unit_test_setup_teardown