cmocka  1.1.0
Macros | Functions
Running Tests

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

Collaboration diagram for Running Tests:

Macros

#define cmocka_unit_test(f)   { #f, f, NULL, NULL, NULL }
 Initializes a CMUnitTest structure. More...
 
#define cmocka_unit_test_prestate(f, state)   { #f, f, NULL, NULL, state }
 Initialize a CMUnitTest structure with given initial state. More...
 
#define cmocka_unit_test_prestate_setup_teardown(f, setup, teardown, state)   { #f, f, setup, teardown, state }
 Initialize a CMUnitTest structure with given initial state, setup and teardown function. More...
 
#define cmocka_unit_test_setup(f, setup)   { #f, f, setup, NULL, NULL }
 Initializes a CMUnitTest structure with a setup function. More...
 
#define cmocka_unit_test_setup_teardown(f, setup, teardown)   { #f, f, setup, teardown, NULL }
 Initialize an array of CMUnitTest structures with a setup function for a test and a teardown function. More...
 
#define cmocka_unit_test_teardown(f, teardown)   { #f, f, NULL, teardown, NULL }
 Initializes a CMUnitTest structure with a teardown function. More...
 
#define group_test_setup(setup)   { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
 Initializes a UnitTest structure for a group setup function. More...
 
#define group_test_teardown(teardown)   { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
 Initializes a UnitTest structure for a group teardown function. More...
 
#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

int cmocka_run_group_tests (const struct CMUnitTest group_tests[], CMFixtureFunction group_setup, CMFixtureFunction group_teardown)
 Run tests specified by an array of CMUnitTest structures. More...
 
int cmocka_run_group_tests_name (const char *group_name, const struct CMUnitTest group_tests[], CMFixtureFunction group_setup, CMFixtureFunction group_teardown)
 Run tests specified by an array of CMUnitTest structures and specify a name. More...
 
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. More...
 
int run_test (#function)
 Generic method to run a single test. More...
 
void skip (void)
 Forces the test to not be executed, but marked as skipped.
 

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 struct CMUnitTest tests[] = {
};
return cmocka_run_group_tests(tests, NULL, NULL);
}

Macro Definition Documentation

§ cmocka_unit_test

#define cmocka_unit_test (   f)    { #f, f, NULL, NULL, NULL }

Initializes a CMUnitTest structure.

§ cmocka_unit_test_prestate

#define cmocka_unit_test_prestate (   f,
  state 
)    { #f, f, NULL, NULL, state }

Initialize a CMUnitTest structure with given initial state.

It will be passed to test function as an argument later. It can be used when test state does not need special initialization or was initialized already.

Note
If the group setup function initialized the state already, it won't be overridden by the initial state defined here.

§ cmocka_unit_test_prestate_setup_teardown

#define cmocka_unit_test_prestate_setup_teardown (   f,
  setup,
  teardown,
  state 
)    { #f, f, setup, teardown, state }

Initialize a CMUnitTest structure with given initial state, setup and teardown function.

Any of these values can be NULL. Initial state is passed later to setup function, or directly to test if none was given.

Note
If the group setup function initialized the state already, it won't be overridden by the initial state defined here.

§ cmocka_unit_test_setup

#define cmocka_unit_test_setup (   f,
  setup 
)    { #f, f, setup, NULL, NULL }

Initializes a CMUnitTest structure with a setup function.

§ cmocka_unit_test_setup_teardown

#define cmocka_unit_test_setup_teardown (   f,
  setup,
  teardown 
)    { #f, f, setup, teardown, NULL }

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

Either setup or teardown can be NULL.

§ cmocka_unit_test_teardown

#define cmocka_unit_test_teardown (   f,
  teardown 
)    { #f, f, NULL, teardown, NULL }

Initializes a CMUnitTest structure with a teardown function.

§ group_test_setup

#define group_test_setup (   setup)    { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }

Initializes a UnitTest structure for a group setup function.

Deprecated:
This function was deprecated in favor of cmocka_run_group_tests

§ group_test_teardown

#define group_test_teardown (   teardown)    { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }

Initializes a UnitTest structure for a group teardown function.

Deprecated:
This function was deprecated in favor of cmocka_run_group_tests

§ unit_test

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

Initializes a UnitTest structure.

Deprecated:
This function was deprecated in favor of cmocka_unit_test

§ unit_test_setup

#define unit_test_setup (   test,
  setup 
)
Value:
_unit_test_setup(test, setup), \
unit_test(test), \
_unit_test_teardown(test, _unit_test_dummy)

Initializes a UnitTest structure with a setup function.

Deprecated:
This function was deprecated in favor of cmocka_unit_test_setup

§ unit_test_setup_teardown

#define unit_test_setup_teardown (   test,
  setup,
  teardown 
)
Value:
_unit_test_setup(test, setup), \
unit_test(test), \
_unit_test_teardown(test, teardown)

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

Either setup or teardown can be NULL.

Deprecated:
This function was deprecated in favor of cmocka_unit_test_setup_teardown

§ unit_test_teardown

#define unit_test_teardown (   test,
  teardown 
)
Value:
_unit_test_setup(test, _unit_test_dummy), \
unit_test(test), \
_unit_test_teardown(test, teardown)

Initializes a UnitTest structure with a teardown function.

Deprecated:
This function was deprecated in favor of cmocka_unit_test_teardown

Function Documentation

§ cmocka_run_group_tests()

int cmocka_run_group_tests ( const struct CMUnitTest  group_tests[],
CMFixtureFunction  group_setup,
CMFixtureFunction  group_teardown 
)

Run tests specified by an array of CMUnitTest structures.

Parameters
[in]group_tests[]The array of unit tests to execute.
[in]group_setupThe setup function which should be called before all unit tests are executed.
[in]group_teardownThe teardown function to be called after all tests have finished.
Returns
0 on success, or the number of failed tests.
static int setup(void **state) {
int *answer = malloc(sizeof(int));
if (*answer == NULL) {
return -1;
}
*answer = 42;
*state = answer;
return 0;
}
static int teardown(void **state) {
free(*state);
return 0;
}
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 struct CMUnitTest tests[] = {
cmocka_unit_test(null_test_success),
cmocka_unit_test_setup_teardown(int_test_success, setup, teardown),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
See also
cmocka_unit_test
cmocka_unit_test_setup
cmocka_unit_test_teardown
cmocka_unit_test_setup_teardown

§ cmocka_run_group_tests_name()

int cmocka_run_group_tests_name ( const char *  group_name,
const struct CMUnitTest  group_tests[],
CMFixtureFunction  group_setup,
CMFixtureFunction  group_teardown 
)

Run tests specified by an array of CMUnitTest structures and specify a name.

Parameters
[in]group_nameThe name of the group test.
[in]group_tests[]The array of unit tests to execute.
[in]group_setupThe setup function which should be called before all unit tests are executed.
[in]group_teardownThe teardown function to be called after all tests have finished.
Returns
0 on success, or the number of failed tests.
static int setup(void **state) {
int *answer = malloc(sizeof(int));
if (*answer == NULL) {
return -1;
}
*answer = 42;
*state = answer;
return 0;
}
static int teardown(void **state) {
free(*state);
return 0;
}
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 struct CMUnitTest tests[] = {
cmocka_unit_test(null_test_success),
cmocka_unit_test_setup_teardown(int_test_success, setup, teardown),
};
return cmocka_run_group_tests_name("success_test", tests, NULL, NULL);
}
See also
cmocka_unit_test
cmocka_unit_test_setup
cmocka_unit_test_teardown
cmocka_unit_test_setup_teardown

§ fail_msg()

void fail_msg ( const char *  msg,
  ... 
)

Forces the test to fail immediately and quit, printing the reason.

fail_msg("This is some error message for test");

or

char *error_msg = "This is some error message for test";
fail_msg("%s", error_msg);

§ run_test()

int run_test ( function)

Generic method to run a single test.

Deprecated:
This function was deprecated in favor of cmocka_run_group_tests
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);
}