Skip to content

Commit 5308f19

Browse files
committed
tests: port assert test to zephyr unit tests
Assert tests to use zephyr unit testing framework. It fixes false negative result when the posix board was used. Signed-off-by: Krzysztof Taborowski <[email protected]>
1 parent 5b5cc72 commit 5308f19

File tree

5 files changed

+35
-37
lines changed

5 files changed

+35
-37
lines changed

tests/unit_tests/pal_assert/CMakeLists.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
cmake_minimum_required(VERSION 3.20.0)
88

9-
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
1010
project(sidewalk_test_assert)
1111

12+
set(SIDEWALK_BASE $ENV{ZEPHYR_BASE}/../sidewalk)
13+
# Workaround on build system not able to find this definition (in kernel.h)
14+
add_definitions(-DARCH_STACK_PTR_ALIGN=8)
1215
# add test file
13-
FILE(GLOB app_sources src/*.c)
14-
target_sources(app PRIVATE ${app_sources})
15-
16-
# generate runner for the test
17-
test_runner_generate(${app_sources})
16+
target_include_directories(testbinary PRIVATE ${SIDEWALK_BASE}/subsys/sal/common/sid_pal_ifc)
17+
target_sources(testbinary PRIVATE
18+
${SIDEWALK_BASE}/subsys/sal/sid_pal/src/sid_assert.c
19+
src/main.c
20+
)

tests/unit_tests/pal_assert/Kconfig

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
#
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
6-
config SIDEWALK_BUILD
7-
default y
8-
9-
config SIDEWALK_ASSERT
10-
default y
11-
imply ASSERT
6+
config SIDEWALK_LOG_LEVEL
7+
default 0
128

139
source "Kconfig.zephyr"

tests/unit_tests/pal_assert/prj.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#
44
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
#
6-
CONFIG_UNITY=y
6+
CONFIG_ZTEST=y
7+
CONFIG_ASSERT=y

tests/unit_tests/pal_assert/src/main.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7-
#include <unity.h>
87
#include <sid_pal_assert_ifc.h>
98
#include <stdbool.h>
10-
#include <zephyr/toolchain.h>
9+
#include <zephyr/ztest.h>
10+
#include <zephyr/fff.h>
1111

12-
void setUp(void)
12+
DEFINE_FFF_GLOBALS;
13+
14+
FAKE_VOID_FUNC_VARARG(z_log_minimal_printk, const char *, ...);
15+
16+
static void suite_setup(void *f)
1317
{
18+
RESET_FAKE(z_log_minimal_printk);
1419
}
1520

1621
/******************************************************************
@@ -24,28 +29,23 @@ void assert_post_action(const char *file, unsigned int line)
2429

2530
if (should_assert) {
2631
should_assert = false;
27-
TEST_PASS_MESSAGE("Asserted.");
32+
ztest_test_pass();
2833
}
29-
TEST_FAIL_MESSAGE("Asserted, but should not.");
34+
ztest_test_fail();
3035
}
3136

32-
void test_sid_pal_assert(void)
37+
ZTEST(assert_tests, test_sid_pal_assert_true)
3338
{
34-
should_assert = false;
35-
SID_PAL_ASSERT(true);
36-
37-
should_assert = true;
38-
SID_PAL_ASSERT(false);
39-
TEST_ASSERT_FALSE_MESSAGE(should_assert, "No assert when it should be.");
39+
should_assert = false;
40+
SID_PAL_ASSERT(true);
41+
zassert_false(should_assert, "No assert when it should be.");
4042
}
4143

42-
/* It is required to be added to each test. That is because unity is using
43-
* different main signature (returns int) and zephyr expects main which does
44-
* not return value.
45-
*/
46-
extern int unity_main(void);
47-
48-
int main(void)
44+
ZTEST(assert_tests, test_sid_pal_assert_false)
4945
{
50-
return unity_main();
46+
should_assert = true;
47+
SID_PAL_ASSERT(false);
48+
zassert_false(should_assert, "Asserted, but should not.");
5149
}
50+
51+
ZTEST_SUITE(assert_tests, NULL, NULL, suite_setup, NULL, NULL);
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
tests:
22
sidewalk.unit_tests.assert:
3-
sysbuild: true
4-
platform_allow: native_posix
3+
sysbuild: false
54
tags: Sidewalk
6-
integration_platforms:
7-
- native_posix
5+
type: unit

0 commit comments

Comments
 (0)