Skip to content

Commit ce61a77

Browse files
author
Qinghao Shi
authored
add testing example snippets (#111)
1 parent aaa617d commit ce61a77

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

Tools_Testing/Greentea_Ex_1/main.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
#include "utest/utest.h"
8+
#include "unity/unity.h"
9+
#include "greentea-client/test_env.h"
10+
11+
using namespace utest::v1;
12+
13+
// This is how a test case looks
14+
static control_t simple_test(const size_t call_count)
15+
{
16+
/* test content here */
17+
TEST_ASSERT_EQUAL(4, 2 * 2);
18+
19+
return CaseNext;
20+
}
21+
22+
utest::v1::status_t greentea_setup(const size_t number_of_cases)
23+
{
24+
// Here, we specify the timeout (60s) and the host test (a built-in host test or the name of our Python file)
25+
GREENTEA_SETUP(60, "default_auto");
26+
27+
return greentea_test_setup_handler(number_of_cases);
28+
}
29+
30+
// List of test cases in this file
31+
Case cases[] = {
32+
Case("simple test", simple_test)
33+
};
34+
35+
Specification specification(greentea_setup, cases);
36+
37+
int main()
38+
{
39+
return !Harness::run(specification);
40+
}

Tools_Testing/Greentea_Ex_2/main.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "mbed.h"
7+
#include "utest/utest.h"
8+
#include "unity/unity.h"
9+
#include "greentea-client/test_env.h"
10+
11+
using namespace utest::v1;
12+
13+
static control_t hello_world_test(const size_t call_count)
14+
{
15+
// send a message to the host runner
16+
greentea_send_kv("init", "hello");
17+
18+
// wait until we get a message back
19+
// if this takes too long, the timeout will trigger, so no need to handle this here
20+
char _key[20], _value[128];
21+
while (1) {
22+
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
23+
24+
// check if the key equals init, and if the return value is 'world'
25+
if (strcmp(_key, "init") == 0) {
26+
TEST_ASSERT_EQUAL(0, strcmp(_value, "world"));
27+
break;
28+
}
29+
}
30+
31+
return CaseNext;
32+
}
33+
34+
utest::v1::status_t greentea_setup(const size_t number_of_cases)
35+
{
36+
// here, we specify the timeout (60s) and the host runner (the name of our Python file)
37+
GREENTEA_SETUP(60, "hello_world_tests");
38+
return greentea_test_setup_handler(number_of_cases);
39+
}
40+
41+
Case cases[] = {
42+
Case("hello world", hello_world_test)
43+
};
44+
45+
Specification specification(greentea_setup, cases);
46+
47+
int main()
48+
{
49+
return !Harness::run(specification);
50+
}

Tools_Testing/Utest_Ex_1/main.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "greentea-client/test_env.h"
7+
#include "unity.h"
8+
#include "utest.h"
9+
10+
using namespace utest::v1;
11+
12+
void test_simple()
13+
{
14+
TEST_ASSERT_EQUAL(0, 0);
15+
printf("Simple test called\n");
16+
}
17+
18+
utest::v1::status_t test_repeats_setup(const Case *const source, const size_t index_of_case)
19+
{
20+
// Call the default handler for proper reporting
21+
utest::v1::status_t status = greentea_case_setup_handler(source, index_of_case);
22+
printf("Setting up for '%s'\n", source->get_description());
23+
return status;
24+
}
25+
control_t test_repeats(const size_t call_count)
26+
{
27+
printf("Called for the %u. time\n", call_count);
28+
TEST_ASSERT_NOT_EQUAL(3, call_count);
29+
// Specify how often this test is repeated i.e. n total calls
30+
return (call_count < 2) ? CaseRepeatAll : CaseNext;
31+
}
32+
33+
void test_callback_validate()
34+
{
35+
// You may also use assertions here
36+
TEST_ASSERT_EQUAL_PTR(0, 0);
37+
// Validate the callback
38+
Harness::validate_callback();
39+
}
40+
41+
// Specify all your test cases here
42+
Case cases[] = {
43+
Case("Simple Test", test_simple),
44+
Case("Repeating Test", test_repeats_setup, test_repeats)
45+
};
46+
47+
// Custom setup handler required for proper Greentea support
48+
utest::v1::status_t greentea_setup(const size_t number_of_cases)
49+
{
50+
GREENTEA_SETUP(20, "default_auto");
51+
// Call the default reporting function
52+
return greentea_test_setup_handler(number_of_cases);
53+
}
54+
55+
// Declare your test specification with a custom setup handler
56+
Specification specification(greentea_setup, cases);
57+
58+
int main()
59+
{
60+
// Run the test specification
61+
Harness::run(specification);
62+
}

0 commit comments

Comments
 (0)