Skip to content

Commit

Permalink
add testing example snippets (ARMmbed#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qinghao Shi authored Mar 31, 2020
1 parent aaa617d commit ce61a77
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Tools_Testing/Greentea_Ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"
#include "utest/utest.h"
#include "unity/unity.h"
#include "greentea-client/test_env.h"

using namespace utest::v1;

// This is how a test case looks
static control_t simple_test(const size_t call_count)
{
/* test content here */
TEST_ASSERT_EQUAL(4, 2 * 2);

return CaseNext;
}

utest::v1::status_t greentea_setup(const size_t number_of_cases)
{
// Here, we specify the timeout (60s) and the host test (a built-in host test or the name of our Python file)
GREENTEA_SETUP(60, "default_auto");

return greentea_test_setup_handler(number_of_cases);
}

// List of test cases in this file
Case cases[] = {
Case("simple test", simple_test)
};

Specification specification(greentea_setup, cases);

int main()
{
return !Harness::run(specification);
}
50 changes: 50 additions & 0 deletions Tools_Testing/Greentea_Ex_2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "mbed.h"
#include "utest/utest.h"
#include "unity/unity.h"
#include "greentea-client/test_env.h"

using namespace utest::v1;

static control_t hello_world_test(const size_t call_count)
{
// send a message to the host runner
greentea_send_kv("init", "hello");

// wait until we get a message back
// if this takes too long, the timeout will trigger, so no need to handle this here
char _key[20], _value[128];
while (1) {
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));

// check if the key equals init, and if the return value is 'world'
if (strcmp(_key, "init") == 0) {
TEST_ASSERT_EQUAL(0, strcmp(_value, "world"));
break;
}
}

return CaseNext;
}

utest::v1::status_t greentea_setup(const size_t number_of_cases)
{
// here, we specify the timeout (60s) and the host runner (the name of our Python file)
GREENTEA_SETUP(60, "hello_world_tests");
return greentea_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("hello world", hello_world_test)
};

Specification specification(greentea_setup, cases);

int main()
{
return !Harness::run(specification);
}
62 changes: 62 additions & 0 deletions Tools_Testing/Utest_Ex_1/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/

#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"

using namespace utest::v1;

void test_simple()
{
TEST_ASSERT_EQUAL(0, 0);
printf("Simple test called\n");
}

utest::v1::status_t test_repeats_setup(const Case *const source, const size_t index_of_case)
{
// Call the default handler for proper reporting
utest::v1::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 i.e. 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();
}

// Specify all your test cases here
Case cases[] = {
Case("Simple Test", test_simple),
Case("Repeating Test", test_repeats_setup, test_repeats)
};

// Custom setup handler required for proper Greentea support
utest::v1::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);
}

// Declare your test specification with a custom setup handler
Specification specification(greentea_setup, cases);

int main()
{
// Run the test specification
Harness::run(specification);
}

0 comments on commit ce61a77

Please sign in to comment.