mbed-os/features/frameworks/utest/TESTS/readme/main_cpp_template

97 lines
3.2 KiB
Plaintext
Raw Normal View History

Adding test frameworks and test sources This commit adds the following test frameworks: - `greentea-client` (https://github.com/ARMmbed/greentea-client) - This framework provides a key-value api for communicating with the greentea test tool (https://github.com/ARMmbed/greentea) - `unity` (https://github.com/ARMmbed/unity) - This framework provides test assert macros that can be used when writing test cases - `utest` (https://github.com/ARMmbed/utest) - This framework allows you to execute a series of test cases with reporting that works with the greentea test tool (https://github.com/ARMmbed/greentea) The following changes were made when bringing these frameworks into the tree: - References to `mbed_drivers/mbed.h` within utest's tests were migrated to `mbed.h` - The yotta file `module.json` was removed from `greentea-client` and `unity` - `coverage.json` was also removed from `greentea-client` - `.gitignore` and `.gitattributes` were removed from `greentea-client` - Apache 2.0 license files were removed from `greentea-client` This also brings in a number of tests that have been newly written or ported from various sources: - `TESTS/integration` - Very basic tests, used to check if testing frameworks are working correctly - `TESTS/mbed_drivers` (Thanks @PrzemekWirkus!) - TESTS ported from mbed OS 3.0 (https://github.com/ARMmbed/mbed-drivers) - `TESTS/mbedmicro-mbed` (Thanks @PrzemekWirkus!) - Tests that weren't covered by `TESTS/mbed_drivers` that currently live in `libraries/tests/mbed` - `TESTS/mbedmicro-rtos-mbed` (Thanks @PrzemekWirkus!) - Ported tests that currently live in `libraries/tests/rtos/mbed` - `TESTS/storage_abstraction` (Thanks @rgrover!) - Tests for the storage_abstraction hal
2016-07-19 18:39:53 +00:00
/* mbed Microcontroller Library
* Copyright (c) 2013-2015 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file describes how to use some of the basic utest features to write your
* unit test.
*
*/
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "utest.h"
#include "unity.h"
using namespace utest::v1;
static Timeout utest_to;
void test_simple() {
TEST_ASSERT_EQUAL(0, 0);
printf("Simple test called\n");
}
status_t test_repeats_setup(const Case *const source, const size_t index_of_case) {
// Call the default handler for proper reporting
status_t status = greentea_case_setup_handler(source, index_of_case);
printf("Setting up for '%s'\n", source->get_description());
return status;
}
control_t test_repeats(const size_t call_count) {
printf("Called for the %u. time\n", call_count);
TEST_ASSERT_NOT_EQUAL(3, call_count);
// Specify how often this test is repeated ie. n total calls
return (call_count < 2) ? CaseRepeatAll : CaseNext;
}
void test_callback_validate() {
// You may also use assertions here!
TEST_ASSERT_EQUAL_PTR(0, 0);
// Validate the callback
Harness::validate_callback();
}
control_t test_asynchronous() {
TEST_ASSERT_TRUE_MESSAGE(true, "(true == false) o_O");
// Set up a callback in the future. This may also be an interrupt!
utest_to.attach_us(test_callback_validate, (100*1000)); // Fire after 100 ms
// Set a 200ms timeout starting from now
return CaseTimeout(200);
}
control_t test_asynchronous_timeout(const size_t call_count) {
TEST_ASSERT_TRUE_MESSAGE(true, "(true == false) o_O");
// Set a 200ms timeout starting from now,
// but automatically repeat only this handler on timeout.
if (call_count >= 5) {
// but after the 5th call, the callback finally gets validated
utest_to.attach_us(test_callback_validate, (100*1000)); // Fire after 100 ms
}
return CaseRepeatHandlerOnTimeout(200);
}
// Custom setup handler required for proper Greentea support
status_t greentea_setup(const size_t number_of_cases) {
GREENTEA_SETUP(20, "default_auto");
// Call the default reporting function
return greentea_test_setup_handler(number_of_cases);
}
// Specify all your test cases here
Case cases[] = {
Case("Simple Test", test_simple),
Case("Repeating Test", test_repeats_setup, test_repeats),
Case("Asynchronous Test (200ms timeout)", test_asynchronous),
Case("Asynchronous Timeout Repeat", test_asynchronous_timeout)
};
// Declare your test specification with a custom setup handler
Specification specification(greentea_setup, cases);
int main()
{
// Run the specification only AFTER setting the custom scheduler.
Harness::run(specification);
}