1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/tests/unittests
Keith Packard e215261ced picolibc: Use most NEWLIB code with picolibc
In most places, picolibc and newlib are the same, so use
the existing newlib code when compiling with picolibc.

Signed-off-by: Keith Packard <keithp@keithp.com>
2020-08-24 08:26:16 -07:00
..
common
tests tests: use check_unittests func from testrunner 2020-03-19 10:39:34 +01:00
tests-analog_util tests: make use of ARRAY_SIZE macro 2019-08-06 19:43:54 +02:00
tests-base64 tests/unittests: Extend & clean up base64 tests 2020-07-02 12:23:43 +02:00
tests-bcd
tests-bitfield tests/unittests: add tests for missing bitfield functions 2019-11-21 12:38:31 +01:00
tests-bloom tests/unittests: fix typos 2019-11-23 22:39:37 +01:00
tests-bluetil tests/unittests/bluetil: adapt to byteorder fixes 2019-08-28 16:49:21 +02:00
tests-checksum tests/checksum: add tests for crc8 2019-12-13 14:50:33 +01:00
tests-clif tests: Add unit tests for clif module 2019-10-04 11:35:52 +02:00
tests-color tests/unittests: test black corner case for color_rgb2hsv() 2019-09-27 09:35:39 +02:00
tests-core Merge pull request #14368 from jia200x/pr/byteorder_bebuftohs 2020-07-13 12:31:43 +02:00
tests-credman net/credman : Move 'CREDMAN_MAX_CREDENTIALS' to 'CONFIG_' 2020-07-14 21:26:19 +05:30
tests-div tests: make use of ARRAY_SIZE macro 2019-08-06 19:43:54 +02:00
tests-ecc
tests-fib tests/unittests: fix typos 2019-11-23 22:39:37 +01:00
tests-fib_sr tests/unittests: fix typos 2019-11-23 22:39:37 +01:00
tests-flashpage
tests-fmt sys/fmt: add fmt_hex_bytes size probing 2020-05-12 17:39:25 +02:00
tests-frac frac: Integer fraction scaling library 2019-12-06 09:36:33 +01:00
tests-gcoap net/gcoap: Remove gcoap_finish 2020-07-17 16:33:19 +02:00
tests-gnrc_ipv6 tests/unittests/tests-gnrc_ipv6: fix NDEBUG compile problem 2020-03-12 17:40:46 +01:00
tests-gnrc_ipv6_hdr
tests-gnrc_ipv6_nib gnrc/nib: Move GNRC_IPV6_NIB_ABR_NUMOF to 'CONFIG_' namespace 2020-03-31 18:07:13 +02:00
tests-gnrc_mac_internal tests/unittests/mac : Added check for macro if set via Kconfig 2020-06-15 12:29:00 +05:30
tests-gnrc_sixlowpan_frag_vrb net/gnrc/sixlowpan/frag/vrb:Move timeout macro to 'CONFIG_' namespace 2020-01-17 18:09:19 +01:00
tests-gnrc_udp
tests-hashes unittests/hashes: Mark input strings as static const 2020-06-25 15:05:26 +02:00
tests-ieee802154
tests-inet_csum many typo fixes 2019-11-23 22:39:07 +01:00
tests-ipv4_addr
tests-ipv6_addr tests/unittests/ipv6_addr: Add tests for ipv6_addr_split_prefix 2019-11-15 14:35:30 +01:00
tests-ipv6_hdr tests: provide unittests for ipv6_ext_frag 2019-06-04 16:24:28 +02:00
tests-luid unittests: add test for luid module 2019-11-04 21:08:33 +01:00
tests-matstat tests: make use of ARRAY_SIZE macro 2019-08-06 19:43:54 +02:00
tests-mtd mtd: Change API to return 0 on success 2020-05-06 20:24:27 +02:00
tests-nanocoap nanocoap: Added token length validation. 2020-05-25 22:44:50 +02:00
tests-netopt
tests-netreg
tests-phydat tests: added unittests for phydat 2020-05-20 15:29:24 +02:00
tests-pkt unittests: Qualifier cast for pktsnip test initializers 2020-06-25 16:08:39 +02:00
tests-pktbuf unittests: Qualifier cast for pktsnip test initializers 2020-06-25 16:08:39 +02:00
tests-pktqueue unittests: Qualifier cast for pktsnip test initializers 2020-06-25 16:08:39 +02:00
tests-printf_float
tests-priority_pktqueue unittests: Qualifier cast for pktsnip test initializers 2020-06-25 16:08:39 +02:00
tests-rtc tests: remove obsolete msp430 blacklists 2020-08-05 14:32:58 +02:00
tests-rtt_rtc tests/unittests/tests-rtt_rtc: fix implicit integer conversions 2020-08-05 14:32:58 +02:00
tests-saul_reg
tests-scanf_float tests/unittests: add tests for scanf_float. 2019-05-28 12:10:39 +02:00
tests-seq
tests-sht1x unittests/sht1x: decrease the amount of tested values on board 2019-08-21 14:54:35 +02:00
tests-sixlowpan
tests-sixlowpan_ctx unittests: tests-sixlowpan_ctx: fix prefix check 2020-02-25 17:49:47 +01:00
tests-sixlowpan_sfr tests: add unittests for net/sixlowpan/sfr.h helpers 2019-10-01 08:45:19 +02:00
tests-sock_util tests/unittests: amend sock_util tests for interface parsing 2020-05-19 14:57:41 +02:00
tests-timex
tests-tsrb unittests: add tsrb tests 2019-06-05 17:54:56 +02:00
tests-uri_parser Merge pull request #13804 from chrysn-pull-requests/uri-test-isabsolute 2020-04-05 13:42:11 +02:00
tests-uuid
tests-vfs picolibc: Use most NEWLIB code with picolibc 2020-08-24 08:26:16 -07:00
tests-zptr tests/unittests: add basic zptr test 2020-02-11 13:59:59 +01:00
tests-ztimer tests/ztimer_mock: cleanup leftover debug messages 2020-03-16 15:21:22 +01:00
main.c tests: add test_utils_interactive_sync when possible 2019-11-27 15:07:42 +01:00
Makefile tests/unittests: increase main stacksize to THREAD_STACKSIZE_LARGE 2020-06-25 16:12:57 +02:00
Makefile.ci tests, examples: remove chronos references 2020-08-05 14:32:58 +02:00
map.h
README.md tests/unittests: fix typos 2019-11-23 22:39:37 +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 board documentation|RIOT-Platforms).

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>;
}

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.