Contiki-NG
Data Structures | Macros | Typedefs
unit-test.h File Reference

 A tool for unit testing Contiki software.
More...

#include "sys/rtimer.h"

Go to the source code of this file.

Data Structures

struct  unit_test
 The unit_test structure describes the results of a unit test. More...
 

Macros

#define UNIT_TEST_REGISTER(name, descr)   static unit_test_t unit_test_##name = {descr, __FILE__, unit_test_success, 0, 0, 0}
 Register a unit test. More...
 
#define UNIT_TEST(name)   static void unit_test_function_##name(unit_test_t *utp)
 Define a unit test. More...
 
#define UNIT_TEST_BEGIN()
 Mark the starting point of the unit test function.
 
#define UNIT_TEST_END()
 Mark the ending point of the unit test function.
 
#define UNIT_TEST_PRINT_REPORT(name)   UNIT_TEST_PRINT_FUNCTION(&unit_test_##name)
 Print a report of the execution of a unit test. More...
 
#define UNIT_TEST_RUN(name)
 Execute a unit test and print a report on the results. More...
 
#define UNIT_TEST_SUCCEED()
 Report that a unit test succeeded. More...
 
#define UNIT_TEST_FAIL()
 Report that a unit test failed. More...
 
#define UNIT_TEST_ASSERT(expr)
 Report failure if an expression is false. More...
 
#define UNIT_TEST_RESULT(name)   (unit_test_##name.result)
 Obtain the result of a certain unit test. More...
 

Typedefs

typedef struct unit_test unit_test_t
 The unit_test structure describes the results of a unit test. More...
 

Detailed Description

 A tool for unit testing Contiki software.
Author
Nicolas Tsiftes nvt@s.nosp@m.ics..nosp@m.se

Definition in file unit-test.h.

Macro Definition Documentation

◆ UNIT_TEST

#define UNIT_TEST (   name)    static void unit_test_function_##name(unit_test_t *utp)

Define a unit test.

This macro defines the function that will be executed when conducting a unit test. The name that is passed as a parameter must have been registered with the UNIT_TEST_REGISTER() macro.

The function defined by this macro must start with a call to the UNIT_TEST_BEGIN() macro, and end with a call to the UNIT_TEST_END() macro.

The standard test function template produced by this macro will ensure that the unit test keeps track of the result, the time taken to execute (in rtimer ticks), and the exit point of the test. The latter corresponds to the line number at which the test was determined to be a success or failure.

Parameters
nameThe name of the unit test.

Definition at line 91 of file unit-test.h.

◆ UNIT_TEST_ASSERT

#define UNIT_TEST_ASSERT (   expr)
Value:
do { \
if(!(expr)) { \
UNIT_TEST_FAIL(); \
} \
} while(0)

Report failure if an expression is false.

Parameters
exprThe expression to evaluate.

Definition at line 170 of file unit-test.h.

◆ UNIT_TEST_FAIL

#define UNIT_TEST_FAIL ( )
Value:
do { \
utp->exit_line = __LINE__; \
utp->result = unit_test_failure; \
goto unit_test_end; \
} while(0)

Report that a unit test failed.

This macro is used to signal that a unit test failed to execute. The line number at which this macro was called is stored in the unit test descriptor.

Definition at line 159 of file unit-test.h.

◆ UNIT_TEST_PRINT_REPORT

#define UNIT_TEST_PRINT_REPORT (   name)    UNIT_TEST_PRINT_FUNCTION(&unit_test_##name)

Print a report of the execution of a unit test.

Parameters
nameThe name of the unit test.

Definition at line 123 of file unit-test.h.

◆ UNIT_TEST_REGISTER

#define UNIT_TEST_REGISTER (   name,
  descr 
)    static unit_test_t unit_test_##name = {descr, __FILE__, unit_test_success, 0, 0, 0}

Register a unit test.

This macro allocates unit test descriptor, which is a structure of type unit_test_t. The descriptor contains information about a unit test, and the results from the last execution of the test.

Parameters
nameThe name of the unit test.
descrA string that briefly describes the unit test.

Definition at line 70 of file unit-test.h.

◆ UNIT_TEST_RESULT

#define UNIT_TEST_RESULT (   name)    (unit_test_##name.result)

Obtain the result of a certain unit test.

If the unit test has not yet been executed, this macro returns unit_test_failed. Otherwise it returns the result of the last execution of the unit test.

Parameters
nameThe name of the unit test.

Definition at line 185 of file unit-test.h.

◆ UNIT_TEST_RUN

#define UNIT_TEST_RUN (   name)
Value:
do { \
unit_test_function_##name(&unit_test_##name); \
UNIT_TEST_PRINT_REPORT(name); \
} while(0)

Execute a unit test and print a report on the results.

Parameters
nameThe name of the unit test.

Definition at line 130 of file unit-test.h.

◆ UNIT_TEST_SUCCEED

#define UNIT_TEST_SUCCEED ( )
Value:
do { \
utp->exit_line = __LINE__; \
goto unit_test_end; \
} while(0)

Report that a unit test succeeded.

This macro is useful for writing tests that can succeed earlier than the last execution point of the test, which is specified by a call to the UNIT_TEST_END() macro.

Tests can usually be written without calls to UNIT_TEST_SUCCEED(), since it is implicitly called at the end of the test—unless UNIT_TEST_FAIL() has been called.

Definition at line 147 of file unit-test.h.

Typedef Documentation

◆ unit_test_t

typedef struct unit_test unit_test_t

The unit_test structure describes the results of a unit test.

Each registered unit test statically allocates an object of this type.