1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/tests/unittests
2018-10-03 10:44:14 +02:00
..
common *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests tests: move testrunner import up 2018-08-13 14:11:24 +02:00
tests-analog_util unittests: Add analog_util tests 2018-04-16 13:53:49 +02:00
tests-base64 cppcheck: Provide consistent reason formatting for all cppcheck-suppresses 2017-10-10 10:25:20 -07:00
tests-bcd tests: provide unittests for bcd 2017-07-06 20:43:30 +02:00
tests-bitfield tests: unittests: add some bitfield unit tests 2015-07-07 17:49:18 +02:00
tests-bloom *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-checksum tests: provide unittests for ucrc16 2016-12-27 00:58:11 +01:00
tests-cn_cbor tests/unittests: remove invisible unicode chars 2018-06-22 11:25:31 +02:00
tests-color sys/color: added RGB inversion and complementary color 2016-03-16 11:43:26 +01:00
tests-core tests/unittests/core: add byteorder_bebuftohs(), byteorder_htobebufs() tests 2018-08-23 10:34:59 +02:00
tests-cpp_ctors unittests: Add C++ constructors test 2017-04-10 09:31:13 +02:00
tests-crypto crypto/ccm: fix _fits_in_nbytes function 2018-05-07 15:08:00 +02:00
tests-devfs devfs: Dynamic file system for device nodes 2017-03-07 17:59:59 +01:00
tests-div *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-ecc unittests/tests-ecc: generalize ECC test 2018-09-28 08:53:25 +02:00
tests-fib cppcheck: Provide consistent reason formatting for all cppcheck-suppresses 2017-10-10 10:25:20 -07:00
tests-fib_sr cleanup: apply headerguard script output 2017-05-24 17:54:02 +02:00
tests-flashpage cleanup: apply headerguard script output 2017-05-24 17:54:02 +02:00
tests-fmt fmt: add scn_u32_hex() 2018-10-03 10:44:14 +02:00
tests-gcoap {examples,tests}/gcoap: Remove unnecessary type casts 2018-07-30 09:01:30 +03:00
tests-gnrc_ipv6 *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-gnrc_ipv6_hdr *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-gnrc_ipv6_nib unittests: fix gnrc_ipv6_nib test 2017-11-29 21:11:08 +01:00
tests-gnrc_mac_internal cleanup: apply headerguard script output 2017-05-24 17:54:02 +02:00
tests-gnrc_udp doc: update IPv6 RFC references 2018-04-10 15:40:18 +02:00
tests-hacl Initial HACL* package and tests 2018-04-14 13:43:27 +02:00
tests-hashes hashes:sha3: Integrate SHA-3 into RIOT API 2018-03-06 16:18:18 +01:00
tests-heatshrink unittests: fix heatshrink compiler error on macOS 2017-01-20 14:34:35 +01:00
tests-ieee802154 *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-inet_csum *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-ipv4_addr *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-ipv6_addr tests: provide test for off-by-x error in ipv6_addr 2017-04-25 19:19:27 +02:00
tests-ipv6_hdr *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-libcose tests/unittests: remove invisible unicode chars 2018-06-22 11:25:31 +02:00
tests-littlefs unittests/tests-littlefs: fix dummy mtd 2018-03-05 11:04:34 +01:00
tests-matstat unittests: Add tests for matstat library 2018-04-28 08:03:35 +02:00
tests-mtd mtd_native: fix overlapping pages write check 2018-02-26 15:32:06 +01:00
tests-nanocoap net/nanocoap: use correct type param for _bulid_hdr() 2018-09-26 13:00:39 +02:00
tests-netopt *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-netreg Revert "unittests: adapt tests-netreg" 2018-02-22 20:29:55 +01:00
tests-phydat unittests: Add test for phydat (phydat_fit) 2018-03-22 15:21:01 +01:00
tests-pkt sys/net/gnrc/pkt: match pktsnip struct start with iolist_t 2018-03-06 14:00:31 +01:00
tests-pktbuf tests/unittests: adapt to iolist pktsnip 2018-03-06 14:00:32 +01:00
tests-pktqueue tests/unittests: adapt to iolist pktsnip 2018-03-06 14:00:32 +01:00
tests-printf_float unittests: Fix printf float test BUFSIZE 2017-12-12 12:16:43 +01:00
tests-priority_pktqueue tests/unittests: adapt to iolist pktsnip 2018-03-06 14:00:32 +01:00
tests-qDSA tests/unittests: add qDSA unittests 2018-03-16 16:00:58 +01:00
tests-relic tests/* : blacklisting for mega-xplained 2018-03-26 11:17:23 -04:00
tests-rpl_srh *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-saul_reg unittests/saul_reg: Add unit test for saul_reg_rm of list head 2018-02-18 12:05:54 +01:00
tests-seq *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-sht1x tests/unittests: Added unit tests for sht1x 2018-06-27 08:58:02 +02:00
tests-sixlowpan *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-sixlowpan_ctx *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-sock_util sock_util: Add tests 2018-07-20 11:48:29 +02:00
tests-spiffs unittests/tests-spiffs: fix dummy mem (as in #8607) 2018-03-05 18:36:58 +01:00
tests-timex *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-tweetnacl tests/tweetnacl: Cleanup headers and includes 2018-04-14 12:47:32 +02:00
tests-ubjson *: remove trailing underscores from header guards 2017-01-19 18:30:53 +01:00
tests-uuid uuid: add uuid_to_string() 2018-10-03 10:44:14 +02:00
tests-vfs unittests/vfs: Add test case for #8309 2018-02-13 18:14:58 +01:00
main.c remove obsolete lpm code 2017-01-12 11:24:15 +01:00
Makefile tests/unittests: exclude samr30-xpro board 2018-10-01 10:20:41 +02:00
map.h cleanup: apply headerguard script output 2017-05-24 17:54:02 +02:00
README.md doc: changed include and function call in README 2017-06-29 14:03:53 +02: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 struture

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,
                        tests_<module>_<header>_set_up,
                        tests_<module>_<header>_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.