Skip to content

Commit be215a3

Browse files
author
Cruz Monrreal
authored
Merge pull request #7448 from davidsaada/david_unity_test_skip
Unity: Add macros for test skipping.
2 parents 38744b9 + 2b7d836 commit be215a3

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

features/frameworks/unity/source/unity.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static const char UnityStrOk[] = "OK";
2929
static const char UnityStrPass[] = "PASS";
3030
static const char UnityStrFail[] = "FAIL";
3131
static const char UnityStrIgnore[] = "IGNORE";
32+
static const char UnityStrSkip[] = "SKIP";
3233
static const char UnityStrNull[] = "NULL";
3334
static const char UnityStrSpacer[] = ". ";
3435
static const char UnityStrExpected[] = " Expected ";
@@ -1228,6 +1229,19 @@ void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line)
12281229
UNITY_IGNORE_AND_BAIL;
12291230
}
12301231

1232+
/*-----------------------------------------------*/
1233+
void UnitySkipPrint(const char* msg, const UNITY_LINE_TYPE line)
1234+
{
1235+
UnityTestResultsBegin(Unity.TestFile, line);
1236+
UnityPrint(UnityStrSkip);
1237+
if (msg != NULL)
1238+
{
1239+
UNITY_OUTPUT_CHAR(':');
1240+
UNITY_OUTPUT_CHAR(' ');
1241+
UnityPrint(msg);
1242+
}
1243+
}
1244+
12311245
/*-----------------------------------------------*/
12321246
#if defined(UNITY_WEAK_ATTRIBUTE)
12331247
UNITY_WEAK_ATTRIBUTE void setUp(void) { }

features/frameworks/unity/unity/unity.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,17 @@ void tearDown(void);
294294
#define TEST_ASSERT_DOUBLE_IS_NOT_NAN_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NAN((actual), __LINE__, (message))
295295
#define TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE_MESSAGE(actual, message) UNITY_TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE((actual), __LINE__, (message))
296296

297+
/*-------------------------------------------------------
298+
* Test skipping
299+
*-------------------------------------------------------*/
300+
301+
// Use these to skip the test case (marking it successful).
302+
// Use only in test case function itself, and only if it returns nothing (void).
303+
#define TEST_SKIP_MESSAGE(message) UNITY_TEST_SKIP(__LINE__, (message))
304+
#define TEST_SKIP() UNITY_TEST_SKIP(__LINE__, NULL)
305+
#define TEST_SKIP_UNLESS(condition) UNITY_TEST_SKIP_UNLESS((condition), __LINE__, NULL)
306+
#define TEST_SKIP_UNLESS_MESSAGE(condition, message) UNITY_TEST_SKIP_UNLESS((condition), __LINE__, (message))
307+
297308
/* end of UNITY_FRAMEWORK_H */
298309
#ifdef __cplusplus
299310
}

features/frameworks/unity/unity/unity_internals.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ void UnityFail(const char* message, const UNITY_LINE_TYPE line);
554554

555555
void UnityIgnore(const char* message, const UNITY_LINE_TYPE line);
556556

557+
void UnitySkipPrint(const char* message, const UNITY_LINE_TYPE line);
558+
557559
#ifndef UNITY_EXCLUDE_FLOAT
558560
void UnityAssertFloatsWithin(const _UF delta,
559561
const _UF expected,
@@ -785,6 +787,13 @@ extern const char UnityStrErr64[];
785787
#define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE(actual, line, message) UnityAssertDoubleSpecial((_UD)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NOT_DET)
786788
#endif
787789

790+
/*-------------------------------------------------------
791+
* Test skip
792+
*-------------------------------------------------------*/
793+
794+
#define UNITY_TEST_SKIP(line, message) { UnitySkipPrint( (message), (UNITY_LINE_TYPE)(line)); return; }
795+
#define UNITY_TEST_SKIP_UNLESS(condition, line, message) if (!(condition)) UNITY_TEST_SKIP((line), (message))
796+
788797
/* End of UNITY_INTERNALS_H */
789798
#endif
790799

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "mbed.h"
18+
#include "greentea-client/test_env.h"
19+
#include "utest/utest.h"
20+
#include "unity/unity.h"
21+
22+
using namespace utest::v1;
23+
24+
int skipped_at_right_place = 0;
25+
26+
utest::v1::status_t test_skip_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
27+
{
28+
TEST_ASSERT_EQUAL(1, skipped_at_right_place);
29+
greentea_case_teardown_handler(source, passed, failed, failure);
30+
return STATUS_CONTINUE;
31+
}
32+
33+
// Aborting Teardown Handler ------------------------------------------------------------------------------------------
34+
void unconditional_test_skip_test()
35+
{
36+
TEST_SKIP();
37+
// Should not get here
38+
TEST_ASSERT(0);
39+
}
40+
41+
void conditional_test_skip_test()
42+
{
43+
skipped_at_right_place = 0;
44+
TEST_SKIP_UNLESS_MESSAGE(1, "Test skipped at the wrong place!");
45+
46+
skipped_at_right_place = 1;
47+
TEST_SKIP_UNLESS_MESSAGE(0, "Test skipped at the right place.");
48+
49+
// Should not get here
50+
skipped_at_right_place = 0;
51+
TEST_ASSERT(0);
52+
}
53+
54+
// Cases --------------------------------------------------------------------------------------------------------------
55+
Case cases[] = {
56+
Case("Unconditional test skip macro test", unconditional_test_skip_test),
57+
Case("Conditional test skip macro test", conditional_test_skip_test, test_skip_case_teardown),
58+
};
59+
60+
// Specification: Setup & Teardown ------------------------------------------------------------------------------------
61+
utest::v1::status_t greentea_setup(const size_t number_of_cases)
62+
{
63+
GREENTEA_SETUP(15, "default_auto");
64+
65+
return greentea_test_setup_handler(number_of_cases);
66+
}
67+
68+
Specification specification(greentea_setup, cases);
69+
70+
int main()
71+
{
72+
// Run the specification only AFTER setting the custom scheduler(if required).
73+
Harness::run(specification);
74+
}

0 commit comments

Comments
 (0)