tymonx / utest

Lightweight unit testing framework for C/C++ projects. Suitable for embedded devices.
BSD 3-Clause "New" or "Revised" License
19 stars 2 forks source link
arm arm-microcontrollers bsd-license c-plus-plus c-plus-plus-11 cmake cortex-m cpp cpp11 embedded embedded-devices microcontrollers tdd test-driven-development testing unit-testing

µTest

Language Standard License (3-Clause BSD) Build Status Coverity Scan Build Status Coverage Status Join the chat at https://gitter.im/utest-embedded/Lobby

Lightweight unit testing framework mainly for C++ and also for C projects. Suitable for embedded devices.

Key Features

How to use it

Examples

Test source example:

#include <utest/utest.hpp>

using namespace utest;

static unsigned int Factorial(unsigned int number) {
    return number > 1 ? Factorial(number - 1) * number : 1;
}

static TestRunner g([] (TestSuite& test_suite) {
    test_suite.name("Factorial").run([] (TestCase& test_case) {
        test_case.name("computed").run([] (TestParams& p) {
            TestAssert{p}.equal(Factorial(0), 1u);
            TestAssert{p}.equal(Factorial(1), 1u);
            TestAssert{p}.equal(Factorial(2), 2u);
            TestAssert{p}.equal(Factorial(3), 6u);
            TestAssert{p}.equal(Factorial(10), 3628800u);
        });
    });
});

Test output example:

[==========] Running 1 registered test runner(s).
[----------] Running test(s) from Factorial
[ RUN      ] computed
[       OK ] computed
[----------] 1 test(s) from Factorial
[  PASSED  ] 1 test(s).

[==========] 1 test(s) from 1 test suite(s) ran.
[  PASSED  ] 1 test(s).

Test assert message examples:

unit_test.cpp:7: Failure
Expected: (boolean)[address=0x00007fa3b3411e08,size=1] is true
  Actual: false

unit_test.cpp:13: Failure
Expected: (string)[address=0x00007fff4d0c00b0,size=4] == (string)[address=0x00000000004097ee,size=5]
  Actual: "Text" == "TextB"

unit_test.cpp:17: Failure
Expected: (signed int)[address=0x00007fa3b3411dc8,size=4] == (signed int)[address=0x00007fa3b3411d98,size=4]
  Actual: 1 == 2

Main example with multi writers:

#include <utest/utest.hpp>

#include <utest/test_writer/udp.hpp>
#include <utest/test_writer/file.hpp>
#include <utest/test_reporter/google_test.hpp>

#include <cstdlib>

using namespace utest;

int main(int argc, char* argv[]) {
    test_writer::UDP udp{"127.0.0.1", 51234};
    test_writer::File out{stdout};

    TestWriterRefence writers[]{
        out,
        udp
    };

    test_reporter::GoogleTest google_test{writers};

    TestReporterReference reporters[]{
        google_test
    };

    return TestStatus::PASS == Test{argc, argv}.run().status()
        ? EXIT_SUCCESS : EXIT_FAILURE;
}

Test example with evil macros extension:

#include <utest/utest.hpp>
#include <utest/test_extension/evil_defines.hpp>

UTEST_RUNNER()
    UTEST_SUITE("integer compare")
        int x = 0;

        UTEST_CASE_CONTEXT(x);

        UTEST_CASE_SETUP()
            UTEST_CONTEXT(int) = 5;
        UTEST_CASE_SETUP_END

        UTEST_CASE("equal")
            UTEST_EXPECT_EQ(UTEST_CONTEXT(int), 5);
        UTEST_CASE_END

        UTEST_CASE_SETUP()
            UTEST_CONTEXT(int) = 4;
        UTEST_CASE_SETUP_END

        UTEST_CASE("not equal")
            UTEST_EXPECT_NE(UTEST_CONTEXT(int), 5);
        UTEST_CASE_END
    UTEST_SUITE_END
UTEST_RUNNER_END