MoonUnit
 All Data Structures Variables Enumerations Enumerator Groups Pages
Definition

Macros to define unit tests. More...

Macros

#define MU_TEST(suite_name, test_name)
 Defines a unit test. More...
 
#define MU_LIBRARY_SETUP()
 Define library setup routine. More...
 
#define MU_LIBRARY_TEARDOWN()
 Define library teardown routine. More...
 
#define MU_FIXTURE_SETUP(suite_name)
 Define test fixture setup routine. More...
 
#define MU_FIXTURE_TEARDOWN(suite_name)
 Define test fixture teardown routine. More...
 
#define MU_LIBRARY_CONSTRUCT()
 Define library construct routine. More...
 
#define MU_LIBRARY_DESTRUCT()
 Define library destruct routine. More...
 
#define MU_LIBRARY_INFO(info_key, info_value)
 Define library metadata. More...
 
#define MU_LIBRARY_NAME(lib_name)
 Define library name. More...
 

Detailed Description

This module contains macros to define unit tests as well as library and fixture setup and teardown routines.

Macro Definition Documentation

#define MU_FIXTURE_SETUP (   suite_name)

Defines the setup routine for a test fixture – an environment common to all tests in a particular suite. This routine will be run immediately before each test in the given suite. The setup routine may signal success or failure before the test itself is executed; in this case, the test itself will not be run.

This macro should be followed by the body of the setup routine enclosed in curly braces.

Example:

static int x;
static int y;
MU_FIXTURE_SETUP(Arithmetic)
{
x = 2;
y = 3;
}
Parameters
suite_namethe unquoted name of the test suite for which the setup routine is being defined
#define MU_FIXTURE_TEARDOWN (   suite_name)

Defines the teardown routine for a test fixture – an environment common to all tests in a particular suite. This routine will be run immediately after each test in the given suite. A teardown routine may signal failure, causing the test to be reported as failing even if the test itself did not. This may be used to enforce a common postcondition for a suite of tests, free resources acquired in the setup routine, etc.

This macro should be followed by the body of the setup routine enclosed in curly braces.

Note
If tests are run in a separate process, explicit deallocation of resources is not necessary.

Example:

static void* data;
{
MU_ASSERT(data != NULL);
free(data);
}
Parameters
suite_namethe unquoted name of the test suite for which the setup routine is being defined
#define MU_LIBRARY_CONSTRUCT ( )

Defines an optional construct routine which will be executed exactly once by the test harness when this library is loaded. It should be followed by a curly brace-enclosed code block. Library constructors are designed to allow initialization of state and resources once as opposed to each time a test within the library is run. This may be useful for certain kinds of external state, e.g. setting up files on the filesystem or starting up a server application which will be used by unit tests.

Warning
A routine defined in this manner will not be executed in a separate process by the harness. Care must be taken to avoid crashing or otherwise interfering with the operation of the harness.

Example:

{
organize_filesystem();
}
#define MU_LIBRARY_DESTRUCT ( )

Defines an optional destruct routine which will be executed exactly once by the test harness when this library is unloaded. It should be followed by a curly brace-enclosed code block. Library destructors are designed to allow cleanup of state and resources once as opposed to each time a test within the library is run. This may be useful for certain kinds of external state, e.g. deleting temporary files on the filesystem or shutting down a server application which was used by unit tests.

Warning
A routine defined in this manner will not be executed in a separate process by the harness. Care must be taken to avoid crashing or otherwise interfering with the operation of the harness.

Example:

{
cleanup_filesystem();
}
#define MU_LIBRARY_INFO (   info_key,
  info_value 
)

This macro allows library metadata to be defined in an extensible manner. The only key presently supported is "name", which sets the internal name of the test library – see MU_LIBRARY_NAME() for more information.

In the future, additional keys may be available to take advantage of new features or settings in the test loader.

Only one instance of MU_LIBRARY_INFO() should appear in a library for a given key.

Example:

MU_LIBRARY_INFO(name, "FooBarTests");
Parameters
info_keythe unquoted metadata key to set
info_valuethe quoted value to assign to the key
#define MU_LIBRARY_NAME (   lib_name)

This macro sets the name of the test library to the given string. The library name is used by MU_RESOURCE() during resource lookup, by test loggers during logging, and by the moonunit program itself when specifying the desired subset of tests to run. Extensions or naming conventions of dynamic shared objects may differ between platforms; MU_LIBRARY_NAME() allows you to establish a consistent internal name for your test library.

This setting is optional. If an explicit name is not specified, it will default to the file name of the shared object with the file extension removed.

This macro is equivalent to MU_LIBRARY_INFO(name, lib_name)

Example:

MU_LIBRARY_NAME("FooBarTests");
Parameters
lib_namethe quoted name of this library
#define MU_LIBRARY_SETUP ( )

Defines an optional setup routine which will be executed before each test in the library. It should be followed by a curly brace-enclosed code block. Only one instance of this macro should appear in a given library.

Example:

{
library_init();
}
#define MU_LIBRARY_TEARDOWN ( )

Defines an optional teardown routine which will be executed once after each test in the library. It should be followed by a curly brace-enclosed code block. Only one instance of this macro should appear in a given library.

Example:

{
library_free_resources();
}
#define MU_TEST (   suite_name,
  test_name 
)

This macro defines a unit test; it must be followed by the test body enclosed in curly braces.

Example:

MU_TEST(Arithmetic, add)
{
MU_ASSERT(1 + 1 == 2);
}
Parameters
suite_namethe unquoted name of the test suite which this test should be part of
test_namethe unquoted name of this test