1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 01:09:47 +01:00
RIOT/tests/unittests
2024-11-16 16:39:32 +01:00
..
common tests/unittests: improve int size detection 2023-06-19 14:01:56 +02:00
tests
tests-analog_util tests/*: realign ENABLE_DEBUG 2020-10-23 11:27:48 +02:00
tests-base64 tests/unittests/tests-base64: enlarge test buffer for worst case 2021-11-29 13:24:40 +01:00
tests-bcd
tests-bhp_event unittests/bhp_event: add initial unittests 2022-08-11 10:57:54 +02:00
tests-bhp_msg treewide: remove THREAD_CREATE_STACKTEST from thread creation 2024-07-29 11:45:58 +02:00
tests-bitfield tests/unittests: add test for bf_clear_all() 2023-03-17 00:08:16 +01:00
tests-bloom tests/unittests: avoid float in tests-bloom 2023-06-08 15:53:03 +02:00
tests-bluetil tests/*: realign ENABLE_DEBUG 2020-10-23 11:27:48 +02:00
tests-checksum Fletcher32: Add incremental API 2023-10-18 13:22:44 +02:00
tests-chunked_ringbuffer tests/unittests: add tests for chunked_ringbuffer 2022-01-19 11:43:35 +01:00
tests-clif sys/clif: Fixing out of bounds read under certain conditions 2023-02-23 15:45:03 +01:00
tests-coding tests/coding: add tests for XOR codding 2023-02-07 14:09:39 +01:00
tests-color unittest/sys_color: extend test 2023-06-05 13:00:23 +02:00
tests-core build_system/xfa: change API to fix alignment 2024-11-07 16:30:01 +01:00
tests-credman tests/unittests/credman: add tests for key load functions 2021-09-20 08:11:01 +02:00
tests-div tests/unittests: fix compilation with newlib and GCC 13.2.1 2024-10-09 22:32:43 +02:00
tests-dns_cache tests/unittests/tests-dns_cache: add test case for TTL=0 2022-08-11 14:07:54 +02:00
tests-dns_msg unittests: add test for dns_msg 2024-09-27 17:40:11 +02:00
tests-ecc treewide: Remove excessive newlines 2021-08-13 19:50:38 +02:00
tests-fib unittests/tests-fib: Fix array size types 2024-01-15 11:26:36 +01:00
tests-fib_sr tests: Use size_t print format specifier 2023-12-21 12:02:37 +01:00
tests-fmt tests: Add cases to the scn_u32_hex unittest 2024-03-12 18:28:20 +01:00
tests-frac tests/unittests: fix compilation with newlib and GCC 13.2.1 2024-10-09 22:32:43 +02:00
tests-gcoap tests/unittests: coap_get_code() -> coap_get_code_decimal() 2023-11-28 13:11:11 +01:00
tests-gnrc_ipv6
tests-gnrc_ipv6_hdr
tests-gnrc_ipv6_nib gnrc_ipv6_nib: test: Force unspecified next hop addresses 2024-08-31 10:40:17 +02:00
tests-gnrc_mac_internal test/unittest: cleanup most vera++ complains 2022-10-27 16:31:59 +02:00
tests-gnrc_netif_pktq tests/unittests: extend tests for gnrc_netif_pktq_usage() 2021-03-15 15:13:59 +01:00
tests-gnrc_sixlowpan_frag_vrb
tests-gnrc_udp
tests-hashes sys/hashes: support for SHA-{384,512/{224,256}} 2024-03-25 12:35:10 +01:00
tests-ieee802154 tests/unittests: fix get_frame_hdr_len tests for hardened parsing 2022-10-29 00:58:35 +02:00
tests-inet_csum
tests-ipv4_addr unittests/tests-ipv4_addr: test_ipv4_addr_to_str__addr_NULL(): zero a 2021-07-05 16:50:44 +02:00
tests-ipv6_addr test/unittest: cleanup most vera++ complains 2022-10-27 16:31:59 +02:00
tests-ipv6_hdr tests/unittests/tests-ipv6_hdr: fix too short ipv6_hdr_t allocations 2021-07-05 22:35:29 +02:00
tests-kernel_defines examples, tests: Changes for the native64 board 2024-02-05 22:02:14 +01:00
tests-libc sys/libc/endian.h: fix magic constants 2024-01-31 14:46:24 +01:00
tests-luid
tests-matstat tests/*: realign ENABLE_DEBUG 2020-10-23 11:27:48 +02:00
tests-mtd unittests/tests-mtd: expect mtd_write() to always succeed 2023-12-13 16:50:41 +01:00
tests-nanocoap tests/unittests: Increase test coverage for coap_build_reply_header() 2024-11-04 21:18:08 +01:00
tests-nanocoap_cache unittests: Extend nanocoap_cache with option-only 2024-02-12 21:20:39 +01:00
tests-netopt treewide: Remove excessive newlines 2021-08-13 19:50:38 +02:00
tests-netreg gnrc_netreg: Use locks around netreg 2022-10-14 03:35:15 +02:00
tests-phydat sys/phydat: Remove deprecated print function 2024-04-03 15:29:19 +02:00
tests-pkt tests/unittests: add tests for new pkt operations 2020-10-29 18:06:35 +01:00
tests-pktbuf tests/unittests: use proper initializers for gnrc_pktsnip_t 2024-11-16 16:39:32 +01:00
tests-pktqueue tests/unittests: use proper initializers for gnrc_pktsnip_t 2024-11-16 16:39:32 +01:00
tests-printf_float
tests-priority_pktqueue tests/unittests: use proper initializers for gnrc_pktsnip_t 2024-11-16 16:39:32 +01:00
tests-rtc sys: move rtc utility functions to their own module 2021-12-20 13:04:20 +01:00
tests-rtt_rtc test/unittest: no auto ztimer_periph_rtt 2021-12-07 10:07:55 +01:00
tests-saul_reg tests/unittests/tests-saul_reg: remove saul_reg_rm test function 2022-01-08 11:49:06 +01:00
tests-scanf_float treewide: Remove excessive newlines 2021-08-13 19:50:38 +02:00
tests-seq
tests-sht1x tests: add missing includes 2022-11-22 13:39:35 +01:00
tests-sixlowpan treewide: Remove excessive newlines 2021-08-13 19:50:38 +02:00
tests-sixlowpan_ctx
tests-sixlowpan_sfr
tests-sock_util test/unittest: cleanup most vera++ complains 2022-10-27 16:31:59 +02:00
tests-timex
tests-tsrb sys/tsrb: add tsrb_clear 2021-12-06 11:53:59 +01:00
tests-turo unittests/turo: Fix turo to use check 2021-11-16 08:37:46 +01:00
tests-uri_parser tests/unittests/tests-uri_parser: Added PRIuSIZE 2024-01-12 17:17:33 +01:00
tests-ut_process tests/unittests: provide test cases for ut_process 2021-09-16 18:18:48 +02:00
tests-uuid treewide: Remove excessive newlines 2021-08-13 19:50:38 +02:00
tests-vfs sys/vfs: use atomic_utils rather C11 atomics 2024-03-27 11:26:15 +01:00
tests-zptr
tests-ztimer tests/unittests: add a unit test for ztimer 2023-10-23 15:44:52 +02:00
tests-ztimer64 unittests: add ztimer64 clear-on-remove regression test 2022-03-01 11:03:18 +01:00
main.c tests/unittests: report results to the environment on native 2024-03-04 14:43:31 +01:00
Makefile tests/unittests: report results to the environment on native 2024-03-04 14:43:31 +01:00
Makefile.ci boards/weact-g030f6: add new board 2024-02-05 00:45:42 +01:00
map.h
README.md tests/unittests/README: link to wiki updated 2024-03-22 16:09:45 +01:00

Unittests

Building and running tests

Tests can be built by calling:

cd tests/unittests
make

If there are tests for a module you even can build tests specifically for this module:

make tests-<module>
# e.g.
make tests-core

You then can run the tests by calling

make term

or flash them to your board as you would flash any RIOT application to the board (see Supported Boards).

You can debug your tests by running

make debug

and using GDB as usual.

Other output formats

Other output formats using embUnit's textui library are available by setting the environment variable OUTPUT:

  • Compiler: OUTPUT="COMPILER"
  • Text: OUTPUT="TEXT"
  • XML: OUTPUT="XML"
  • Color: OUTPUT="COLOR" (like default, but with red/green output)
  • Colored-Text: OUTPUT="COLORTEXT" (like TEXT, but with red/green output)

Compile example

OUTPUT="COMPILER" make tests-core
make term

(only outputs in case of test failures)

Text example

OUTPUT="TEXT" make tests-core
make term
- core_bitarithm_tests
1) OK test_SETBIT_null_null
2) OK test_SETBIT_null_limit
3) ...
- core_clist_tests
25) ...
- ...

OK (... tests)

XML example

OUTPUT="XML" make tests-core
make term
<?xml version="1.0" encoding='shift_jis' standalone='yes' ?>
<TestRun>
<core_bitarithm_tests>
<Test id="1">
<Name>test_SETBIT_null_null</Name>
</Test>
<Test id="2">
<Name>test_SETBIT_null_limit</Name>
</Test>
...
</core_bitarithm_tests>
<core_clist_tests>
<Test id="25">
<Name>test_clist_add_one</Name>
</Test>
...
</core_clist_tests>
<Statistics>
<Tests>...</Tests>
</Statistics>
</TestRun>

Writing unit tests

File structure

RIOT uses embUnit for unit testing. All unit tests are organized in tests/unittests and can be built module-wise, if needed. For each module there exists a tests-<modulename>/tests-<modulename>.h file, at least one C file in tests-<modulename>/ and a tests-<modulename>/Makefile. It is recommended to add a C file named tests-<modulename>/tests-<modulename>-<headername>.c for every header file that defines functions (or macros) implemented in the module. If there is only one such header file tests-<modulename>/tests-<modulename>.c should suffice.

Each *.c file should implement a function defined in tests-<modulename>/tests-<modulename>.h, named like

Test *tests_<modulename>_<headername>_tests(void);

/* or respectively */

Test *tests_<modulename>_tests(void);

Testing a module

To write new tests for a module you need to do three things:

  1. Create a Makefile: add a file tests-<modulename>/Makefile
  2. Define a test header: add a file tests-<modulename>/tests-<modulename>.h
  3. Implement tests: for each header file, that defines a function or macro implemented or related to the module, add a file tests-<modulename>/tests-<modulename>-<headername>.c or tests-<modulename>/tests-<modulename>.c if there is only one header.

Create a Makefile

The Makefile should have the following content:

include $(RIOTBASE)/Makefile.base

Define a test header.

The test header tests-<modulename>/tests-<module>.h of a module you add to tests/unittests/ should have the following structure

/*
 * Copyright (C) <year> <author>
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/**
 * @addtogroup  unittests
 * @{
 *
 * @file
 * @brief       Unittests for the ``module`` module
 *
 * @author      <author>
 */
#ifndef TESTS_<MODULE>_H
#define TESTS_<MODULE>_H
#include "embUnit/embUnit.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief   Generates tests for <header1>.h
 *
 * @return  embUnit tests if successful, NULL if not.
 */
Test *tests_<module>_<header1>_tests(void);

/**
 * @brief   Generates tests for <header2>.h
 *
 * @return  embUnit tests if successful, NULL if not.
 */
Test *tests_<module>_<header2>_tests(void);

/* ... */

#ifdef __cplusplus
}
#endif

#endif /* TESTS_<MODULE>_H */
/** @} */

Implement tests

Every tests-<modulename>/tests-<module>*.c file you add to tests/unittests/ should have the following structure:

/*
 * Copyright (C) <year> <author>
 *
 * This file is subject to the terms and conditions of the GNU Lesser
 * General Public License v2.1. See the file LICENSE in the top level
 * directory for more details.
 */

/* clib includes */

#include "embUnit.h"

#include "<header>.h"

#include "tests-<module>.h"

/* your macros */

/* your global variables */

static void set_up(void)
{
    /* omit if not needed */
}

static void tear_down(void)
{
    /* omit if not needed */
}

static void test_<function1>_<what1>(void) {
    /* ... */

    TEST_ASSERT(/* ... */);
}

static void test_<function1>_<what2>(void) {
    /* ... */

    TEST_ASSERT(/* ... */);
}

/* ... */

static void test_<function2>_<what1>(void) {
    /* ... */

    TEST_ASSERT(/* ... */);
}

static void test_<function2>_<what2>(void) {
    /* ... */

    TEST_ASSERT(/* ... */);
}

/* ... */

Test *tests_<module>_<header>_tests(void)
{
    EMB_UNIT_TESTFIXTURES(fixtures) {
        new_TestFixture(test_<function1>_<what1>),
        new_TestFixture(test_<function1>_<what2>),
        new_TestFixture(test_<function2>_<what1>),
        new_TestFixture(test_<function2>_<what2>),
        /* ... */
    };

    EMB_UNIT_TESTCALLER(<module>_<header>_tests, set_up, tear_down, fixtures);
    /* set up and tear down function can be NULL if omitted */

    return (Test *)&<module>_<header>_tests;
}

The following assertion macros are available via embUnit

Assertion Description
TEST_ASSERT_EQUAL_STRING(expected,actual) Assert that strings actual and expected are equivalent
TEST_ASSERT_EQUAL_INT(expected,actual) Assert that integers actual and expected are equivalent
TEST_ASSERT_NULL(pointer) Assert that pointer == NULL
TEST_ASSERT_NOT_NULL(pointer) Assert that pointer != NULL
TEST_ASSERT_MESSAGE(condition, message) Assert that condition is TRUE (non-zero) or output customized message on failure.
TEST_ASSERT(condition) Assert that condition is TRUE (non-zero)
TEST_FAIL(message) Register a failed assertion with the specified message. No logical test is performed.