Skip to content

Commit f6f24a2

Browse files
Yuval Peressnashif
authored andcommitted
modules: Add fff mocking framework
Issue #38643 Introduce a more powerful and well established mocking framework into Zephyr. It also allows running the actual FFF tests using the zephyr SDK and ztest framework to ensure compatibility. As per TSC meeting, the fff.h header was directly added to subsys/testsuite/include/. As per the guidelines, the file is exactly the same as it is in FFF's library, but re-styled with clang-format. The west.yml entry was added using the "ci" group and filtered by default. (note that the tests will break until the CI actually specifies that the group is needed). Signed-off-by: Yuval Peress <[email protected]>
1 parent f17630b commit f6f24a2

File tree

11 files changed

+8949
-0
lines changed

11 files changed

+8949
-0
lines changed

modules/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ comment "loramac-node module not available."
6767
comment "CANopenNode module not available."
6868
depends on !ZEPHYR_CANOPENNODE_MODULE
6969

70+
comment "FFF module not available."
71+
depends on !ZEPHYR_FFF_MODULE
72+
7073
# This ensures that symbols are available in Kconfig for dependency checking
7174
# and referencing, while keeping the settings themselves unavailable when the
7275
# modules are not present in the workspace

modules/fff/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright (c) 2021 Google LLC
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
6+
if(CONFIG_FFF_TEST)
7+
# Add the FFF C test suites as ztest suites
8+
message("CONFIG_FFF_TEST_C=${CONFIG_FFF_TEST_C}")
9+
message("CONFIG_FFF_TEST_GLOBAL_C=${CONFIG_FFF_TEST_GLOBAL_C}")
10+
zephyr_library()
11+
zephyr_include_directories(
12+
include
13+
"${ZEPHYR_CURRENT_MODULE_DIR}"
14+
"${ZEPHYR_CURRENT_MODULE_DIR}/test"
15+
)
16+
if(DEFINED CONFIG_FFF_TEST_C)
17+
zephyr_library_sources("${ZEPHYR_CURRENT_MODULE_DIR}/test/fff_test_c.c")
18+
elseif(DEFINED CONFIG_FFF_TEST_GLOBAL_C)
19+
zephyr_library_sources(
20+
"${ZEPHYR_CURRENT_MODULE_DIR}/test/fff_test_global_c.c"
21+
"${ZEPHYR_CURRENT_MODULE_DIR}/test/global_fakes.c"
22+
)
23+
endif()
24+
endif()

modules/fff/Kconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) 2021 Google LLC
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config ZEPHYR_FFF_MODULE
5+
bool
6+
7+
config FFF_TEST
8+
bool "FFF Test suite"
9+
help
10+
Enable the FFF test suite. This should really only be called from the tests for FFF under
11+
tests/ztest/mock_fff/. It brings in the test sources from the upstream FFF module.
12+
13+
if FFF_TEST
14+
15+
choice FFF_TEST
16+
prompt "The type of FFF test to bring in"
17+
18+
config FFF_TEST_C
19+
bool "Compile the FFF default C test suite as a zephyr library"
20+
21+
config FFF_TEST_GLOBAL_C
22+
bool "Compile the FFF global C test suite as a zephyr library"
23+
24+
endchoice # FFF_TEST
25+
26+
endif # FFF_TEST
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Copyright (c) 2021 Google LLC
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef MODULES_FFF_TEST_INCLUDE_C_TEST_FRAMEWORK_H_
7+
#define MODULES_FFF_TEST_INCLUDE_C_TEST_FRAMEWORK_H_
8+
9+
#include <ztest.h>
10+
#include <sys/printk.h>
11+
12+
void setup(void);
13+
void fff_test_suite(void);
14+
15+
#define PRINTF(FMT, args...)
16+
#define TEST_F(SUITE, NAME) __attribute__((unused)) static void test_##NAME(void)
17+
#define RUN_TEST(SUITE, NAME) \
18+
do { \
19+
ztest_test_suite( \
20+
SUITE##_##NAME, \
21+
ztest_unit_test_setup_teardown(test_##NAME, setup, unit_test_noop)); \
22+
ztest_run_test_suite(SUITE##_##NAME); \
23+
} while (0)
24+
#define ASSERT_EQ(A, B) zassert_equal((A), (B), NULL)
25+
#define ASSERT_TRUE(A) zassert_true((A), NULL)
26+
27+
#endif /* MODULES_FFF_TEST_INCLUDE_C_TEST_FRAMEWORK_H_ */

subsys/testsuite/include/fff.h

Lines changed: 8820 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright 2021 Google LLC
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(mock_fff)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})

tests/ztest/mock_fff/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_FFF_TEST=y
3+
CONFIG_FFF_TEST_C=y
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_FFF_TEST=y
3+
CONFIG_FFF_TEST_GLOBAL_C=y

tests/ztest/mock_fff/src/main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Copyright 2021 Google LLC
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <ztest.h>
7+
#include <fff.h>
8+
9+
#include <c_test_framework.h>
10+
11+
void test_main(void)
12+
{
13+
fff_test_suite();
14+
}

tests/ztest/mock_fff/testcase.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests:
2+
testing.ztest.mock_fff:
3+
filter: CONFIG_ZEPHYR_FFF_MODULE
4+
tags: test_framework
5+
integration_platforms:
6+
- native_posix
7+
testing.ztest.mock_fff_global:
8+
filter: CONFIG_ZEPHYR_FFF_MODULE
9+
tags: test_framework
10+
extra_args: CONF_FILE=prj_global.conf
11+
integration_platforms:
12+
- native_posix

0 commit comments

Comments
 (0)