Skip to content

Instantly share code, notes, and snippets.

@hirokuma
Last active September 23, 2024 07:45
Show Gist options
  • Select an option

  • Save hirokuma/a6531bc9af79d8298cbb7e8c5920b082 to your computer and use it in GitHub Desktop.

Select an option

Save hirokuma/a6531bc9af79d8298cbb7e8c5920b082 to your computer and use it in GitHub Desktop.
ncs unity test template
#!/bin/bash
if [ $# -ne 1 ]; then
echo "need target name"
exit 1
fi
TARGET=$1
mkdir -p tests
mkdir -p tests/${TARGET}
pushd tests/${TARGET}
# CMakelists.txt
if [ ! -f CMakeLists.txt ]; then
cat << EOS > CMakeLists.txt
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(${TARGET}_test)
# Generate runner for the test
test_runner_generate(src/${TARGET}_test.c)
set(ROOTDIR ../..)
# Create mocks
# Include target sources
# add test file
target_sources(app PRIVATE src/${TARGET}_test.c)
target_include_directories(app PRIVATE .)
target_include_directories(app PRIVATE src)
target_compile_options(app PRIVATE
-fprofile-arcs
-ftest-coverage
-ggdb
)
target_link_libraries(app PRIVATE
-lgcov
)
EOS
echo "create: CMakeLists.txt"
else
echo "exist: CMakeLists.txt"
fi
# Kconfig
if [ ! -f Kconfig ]; then
echo "source \"Kconfig.zephyr\"" > Kconfig
echo "create: Kconfig"
else
echo "exist: Kconfig"
fi
# prj.conf
if [ ! -f prj.conf ]; then
echo "CONFIG_UNITY=y" > prj.conf
echo "create: prj.conf"
else
echo "exist: prj.conf"
fi
# testcase.yaml
if [ ! -f testcase.yaml ]; then
cat << EOS > testcase.yaml
tests:
unity.${TARGET}_test:
tags: ${TARGET}_test
integration_platforms:
- native_sim_64
platform_allow:
- native_sim_64
EOS
echo "create: testcase.yaml"
else
echo "exist: testcase.yaml"
fi
mkdir -p src
if [ ! -f src/${TARGET}_test.c ]; then
cat << EOS > src/${TARGET}_test.c
#include <unity.h>
void setUp(void)
{
}
void tearDown(void)
{
}
/*********
* Tests
*********/
/* It is required to be added to each test. That is because unity's
* main may return nonzero, while zephyr's main currently must
* return 0 in all cases (other values are reserved).
*/
extern int unity_main(void);
int main(void)
{
(void)unity_main();
return 0;
}
EOS
echo "create: src/${TARGET}_test.c"
else
echo "exist: src/${TARGET}_test.c"
fi
if [ ! -f src/${TARGET}_test.h ]; then
touch src/${TARGET}_test.h
echo "create: src/${TARGET}_test.h"
else
echo "exist: src/${TARGET}_test.h"
fi
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment