1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/tests/unittests
Alexandre Abadie b2b42c7b71 Merge pull request #5451 from mali/uno
boards: add arduino uno and duemilanove support
2016-09-22 09:29:49 +02:00
..
common unittests: fix length of test data strings 2015-07-13 14:37:24 +02:00
tests tests: make use of testrunner 2016-03-05 20:36:28 +01:00
tests-base64 everything: Remove filename from @file Doxygen command 2015-05-22 07:34:41 +02:00
tests-bitfield tests: unittests: add some bitfield unit tests 2015-07-07 17:49:18 +02:00
tests-bloom unittests/tests-bloom: use static memory for bloom 2015-10-05 11:41:53 +02:00
tests-cbor tests/unittests/tests-cbor: Add static to compilation-unit local variables 2016-02-11 15:19:56 +01:00
tests-checksum tests/checksum: fix style in fletcher32 2016-04-08 22:03:48 +02:00
tests-color sys/color: added RGB inversion and complementary color 2016-03-16 11:43:26 +01:00
tests-core Merge pull request #5629 from kaspar030/improve_clist 2016-07-18 19:36:16 +02:00
tests-crypto core: header cleanup 2016-02-28 22:46:28 +01:00
tests-div sys: div: remove div_u32_by_10() 2015-11-19 13:29:10 +01:00
tests-ecc Add 256 byte block hamming code implementation 2015-11-30 20:08:36 +01:00
tests-fib fib: changed handling of the net prefix 2016-03-30 10:05:05 +02:00
tests-fib_sr unittests: fix formatting of fib_sr tests 2015-12-19 21:58:40 +01:00
tests-fmt tests: unittests: add fmt_u64_dec() tests 2016-06-02 10:40:19 +02:00
tests-gnrc_ipv6 tests: unittests for gnrc_ipv6_get_header 2016-07-22 16:34:52 +02:00
tests-gnrc_ipv6_hdr gnrc ipv6: use ipv6_addr_t ptr for hdr_build 2016-03-23 15:20:12 +01:00
tests-gnrc_netif unittests: apply proper naming for gnrc_netif tests 2016-06-04 17:21:27 +02:00
tests-gnrc_udp udp: fixed a corner case for checksum computation. 2016-02-22 20:30:31 +09:00
tests-hashes hashes: Unified the cryptographic hash functions 2016-05-24 16:52:30 +02:00
tests-ieee802154 tests: provide unittests for ieee802154 module 2016-03-07 16:40:30 +01:00
tests-inet_csum Add unit tests for inet_csum_slice() 2015-12-02 05:53:58 -05:00
tests-ipv4_addr tests: add unittests for ipv4_addr 2015-09-11 16:32:36 +02:00
tests-ipv6_addr ipv6-tests_addr: add test for ipv6_addr_init_iid 2015-10-26 16:55:02 +01:00
tests-ipv6_hdr gnrc: make all gnrc modules sub-modules of gnrc 2015-08-18 23:00:07 +02:00
tests-ipv6_nc tests-ipv6_nc: init interfaces for all unittests 2016-03-24 16:13:21 +01:00
tests-ipv6_netif ndp: change rtr adv src to link local 2016-03-08 21:33:35 -08:00
tests-netopt tests: unittests: add netopt module unit tests 2015-08-21 11:04:06 +02:00
tests-netreg gnrc: make all gnrc modules sub-modules of gnrc 2015-08-18 23:00:07 +02:00
tests-pkt gnrc: make all gnrc modules sub-modules of gnrc 2015-08-18 23:00:07 +02:00
tests-pktbuf unittests: add unittests for overflow fix 2016-01-07 15:17:52 +01:00
tests-pktqueue gnrc: make all gnrc modules sub-modules of gnrc 2015-08-18 23:00:07 +02:00
tests-printf_float unittests: added test for printf with floats 2016-07-18 23:22:28 +02:00
tests-relic boards/waspmote-pro: add support for Waspmote PRO v1.2 2016-07-05 13:05:12 +02:00
tests-rpl_srh rpl: srh: add unittests 2016-02-27 20:40:06 +01:00
tests-saul_reg test: added unittests for the SAUL registry 2016-04-15 15:07:49 +02:00
tests-seq tests/unittests/tests-seq: unittests for Serial Number Arithmetic 2015-08-21 23:30:06 +02:00
tests-sixlowpan unittests: initial import of 6LoWPAN tests 2016-03-06 21:00:24 +01:00
tests-sixlowpan_ctx gnrc: make all gnrc modules sub-modules of gnrc 2015-08-18 23:00:07 +02:00
tests-timex unittests: add test for timex_to_str() 2015-08-11 01:33:21 +02:00
tests-ubjson core: Fix/refactor function naming in core/incude/irq.h 2016-03-20 16:47:34 +01:00
main.c unittests: do not powerdown node at the end 2016-01-23 15:17:36 +01:00
Makefile tests: adapt for arduino uno and duemilanove support 2016-09-21 21:12:58 +02:00
map.h unittests: fix and unify header guards 2015-04-20 14:16:09 +02:00
README.md fix typo 2016-07-20 12:20:50 +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).

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/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, "<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.