Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
Ztest assertion macros

Macros

#define zassert(cond, default_msg, msg, ...)
 Fail the test, if cond is false. More...
 
#define zassert_unreachable(msg, ...)
 Assert that this function call won't be reached. More...
 
#define zassert_true(cond, msg, ...)
 Assert that cond is true. More...
 
#define zassert_false(cond, msg, ...)
 Assert that cond is false. More...
 
#define zassert_ok(cond, msg, ...)
 Assert that cond is 0 (success) More...
 
#define zassert_is_null(ptr, msg, ...)
 Assert that ptr is NULL. More...
 
#define zassert_not_null(ptr, msg, ...)
 Assert that ptr is not NULL. More...
 
#define zassert_equal(a, b, msg, ...)
 Assert that a equals b. More...
 
#define zassert_not_equal(a, b, msg, ...)
 Assert that a does not equal b. More...
 
#define zassert_equal_ptr(a, b, msg, ...)
 Assert that a equals b. More...
 
#define zassert_within(a, b, d, msg, ...)
 Assert that a is within b with delta d. More...
 
#define zassert_mem_equal(...)    zassert_mem_equal__(__VA_ARGS__)
 Assert that 2 memory buffers have the same contents. More...
 
#define zassert_mem_equal__(buf, exp, size, msg, ...)
 Internal assert that 2 memory buffers have the same contents. More...
 

Detailed Description

This module provides assertions when using Ztest.

Macro Definition Documentation

◆ zassert

#define zassert (   cond,
  default_msg,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
do { \
bool _ret = z_zassert(cond, msg ? ("(" default_msg ")") : (default_msg), \
__FILE__, __LINE__, __func__, \
msg ? msg : "", ##__VA_ARGS__); \
if (!_ret) { \
/* If kernel but without multithreading return. */ \
COND_CODE_1(KERNEL, \
(COND_CODE_1(CONFIG_MULTITHREADING, (), (return;))), \
()) \
} \
} while (0)
#define COND_CODE_1(_flag, _if_1_code, _else_code)
Insert code depending on whether _flag expands to 1 or not.
Definition: util_macro.h:156

Fail the test, if cond is false.

You probably don't need to call this macro directly. You should instead use zassert_{condition} macros below.

Note that when CONFIG_MULTITHREADING=n macro returns from the function. It is then expected that in that case ztest asserts will be used only in the context of the test function.

Parameters
condCondition to check
msgOptional, can be NULL. Message to print if cond is false.
default_msgMessage to print if cond is false

◆ zassert_equal

#define zassert_equal (   a,
  b,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert((a) == (b), \
#a " not equal to " #b, \
msg, ##__VA_ARGS__)
#define zassert(cond, default_msg, msg,...)
Fail the test, if cond is false.
Definition: ztest_assert.h:100

Assert that a equals b.

a and b won't be converted and will be compared directly.

Parameters
aValue to compare
bValue to compare
msgOptional message to print if the assertion fails

◆ zassert_equal_ptr

#define zassert_equal_ptr (   a,
  b,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert((void *)(a) == (void *)(b), #a " not equal to " #b, \
msg, ##__VA_ARGS__)

Assert that a equals b.

a and b will be converted to void * before comparing.

Parameters
aValue to compare
bValue to compare
msgOptional message to print if the assertion fails

◆ zassert_false

#define zassert_false (   cond,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert(!(cond), #cond " is true", \
msg, ##__VA_ARGS__)

Assert that cond is false.

Parameters
condCondition to check
msgOptional message to print if the assertion fails

◆ zassert_is_null

#define zassert_is_null (   ptr,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert((ptr) == NULL, \
#ptr " is not NULL", \
msg, ##__VA_ARGS__)
void * ptr
Definition: printk.c:79

Assert that ptr is NULL.

Parameters
ptrPointer to compare
msgOptional message to print if the assertion fails

◆ zassert_mem_equal

#define zassert_mem_equal (   ...)     zassert_mem_equal__(__VA_ARGS__)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Assert that 2 memory buffers have the same contents.

This macro calls the final memory comparison assertion macro. Using double expansion allows providing some arguments by macros that would expand to more than one values (ANSI-C99 defines that all the macro arguments have to be expanded before macro call).

Parameters
...Arguments, see zassert_mem_equal__ for real arguments accepted.

◆ zassert_mem_equal__

#define zassert_mem_equal__ (   buf,
  exp,
  size,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, \
msg, ##__VA_ARGS__)
int memcmp(const void *m1, const void *m2, size_t n)

Internal assert that 2 memory buffers have the same contents.

Note
This is internal macro, to be used as a second expansion. See zassert_mem_equal.
Parameters
bufBuffer to compare
expBuffer with expected contents
sizeSize of buffers
msgOptional message to print if the assertion fails

◆ zassert_not_equal

#define zassert_not_equal (   a,
  b,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert((a) != (b), \
#a " equal to " #b, \
msg, ##__VA_ARGS__)

Assert that a does not equal b.

a and b won't be converted and will be compared directly.

Parameters
aValue to compare
bValue to compare
msgOptional message to print if the assertion fails

◆ zassert_not_null

#define zassert_not_null (   ptr,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert((ptr) != NULL, \
#ptr " is NULL", msg, \
##__VA_ARGS__)

Assert that ptr is not NULL.

Parameters
ptrPointer to compare
msgOptional message to print if the assertion fails

◆ zassert_ok

#define zassert_ok (   cond,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert(!(cond), #cond " is non-zero", \
msg, ##__VA_ARGS__)

Assert that cond is 0 (success)

Parameters
condCondition to check
msgOptional message to print if the assertion fails

◆ zassert_true

#define zassert_true (   cond,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert(cond, #cond " is false", \
msg, ##__VA_ARGS__)

Assert that cond is true.

Parameters
condCondition to check
msgOptional message to print if the assertion fails

◆ zassert_unreachable

#define zassert_unreachable (   msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert(0, "Reached unreachable code", \
msg, ##__VA_ARGS__)

Assert that this function call won't be reached.

Parameters
msgOptional message to print if the assertion fails

◆ zassert_within

#define zassert_within (   a,
  b,
  d,
  msg,
  ... 
)

#include <subsys/testsuite/ztest/include/ztest_assert.h>

Value:
zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), \
#a " not within " #b " +/- " #d, \
msg, ##__VA_ARGS__)
irp nz macro MOVR cc d
Definition: asm-macro-32-bit-gnu.h:11

Assert that a is within b with delta d.

Parameters
aValue to compare
bValue to compare
dDelta
msgOptional message to print if the assertion fails