From 3580d8b6d67e378c2982523b334a27f15bc8cd71 Mon Sep 17 00:00:00 2001 From: Arkadiusz Cholewinski Date: Thu, 4 Jul 2024 15:38:19 +0200 Subject: [PATCH] tests: ztest: Add ztest param test. Introduces the test for testing the new functionality of ztest shell. Passing -r (repeat iter) parameter. This allows to run a particula testcase or testsuite several times. Passing -p (any value) parameter. This allows to provide a parameter to a test at runtime. Signed-off-by: Arkadiusz Cholewinski --- tests/ztest/ztest_param/CMakeLists.txt | 8 +++ tests/ztest/ztest_param/prj.conf | 3 ++ .../ztest_param/pytest/test_parameters.py | 50 +++++++++++++++++++ tests/ztest/ztest_param/src/main.c | 33 ++++++++++++ tests/ztest/ztest_param/testcase.yaml | 6 +++ 5 files changed, 100 insertions(+) create mode 100644 tests/ztest/ztest_param/CMakeLists.txt create mode 100644 tests/ztest/ztest_param/prj.conf create mode 100644 tests/ztest/ztest_param/pytest/test_parameters.py create mode 100644 tests/ztest/ztest_param/src/main.c create mode 100644 tests/ztest/ztest_param/testcase.yaml diff --git a/tests/ztest/ztest_param/CMakeLists.txt b/tests/ztest/ztest_param/CMakeLists.txt new file mode 100644 index 00000000000..9fb33cb8832 --- /dev/null +++ b/tests/ztest/ztest_param/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(ztest_params) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/ztest/ztest_param/prj.conf b/tests/ztest/ztest_param/prj.conf new file mode 100644 index 00000000000..438b245ac07 --- /dev/null +++ b/tests/ztest/ztest_param/prj.conf @@ -0,0 +1,3 @@ +CONFIG_ZTEST=y +CONFIG_ZTEST_SHELL=y +CONFIG_ZTEST_SHUFFLE=y diff --git a/tests/ztest/ztest_param/pytest/test_parameters.py b/tests/ztest/ztest_param/pytest/test_parameters.py new file mode 100644 index 00000000000..93b08bdbc7e --- /dev/null +++ b/tests/ztest/ztest_param/pytest/test_parameters.py @@ -0,0 +1,50 @@ +# Copyright (c) 2024 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +import pytest +from twister_harness import Shell + +def test_run_all_shuffled(shell: Shell): + lines = shell.exec_command('ztest shuffle -c 1 -s 1') + # Join the list of lines into a single string + output = '\n'.join(lines) + assert "PASS - test_int_param" in output, f"Expected 'PASS - test_int_param' but got {lines}" + assert "PASS - test_dummy" in output, f"Expected 'PASS - test_dummy' but got {lines}" + assert "PASS - test_string_param" in output, f"Expected 'PASS - test_string_param' but got {lines}" + +def test_run_testcase_once(shell: Shell): + lines = shell.exec_command('ztest run-testcase ztest_params::test_dummy') + # Join the list of lines into a single string + output = '\n'.join(lines) + assert "PASS - test_dummy" in output, f"Expected 'PASS - test_dummy' but got {lines}" + +# def test_run_testcase_twice(shell: Shell): + lines = shell.exec_command('ztest run-testcase ztest_params::test_dummy -r 2') + # Join the list of lines into a single string + output = '\n'.join(lines) + pass_dummy_count = output.count("PASS - test_dummy") + assert pass_dummy_count == 2, f"Expected 2 occurrences of 'PASS - test_dummy' but found {pass_dummy_count}" + +def test_run_testsuite_twice(shell: Shell): + lines = shell.exec_command('ztest run-testsuite ztest_params -r 2') + # Join the list of lines into a single string + output = '\n'.join(lines) + pass_ztest_params_count = output.count("TESTSUITE ztest_params succeeded") + assert pass_ztest_params_count == 2, f"Expected 2 occurrences of 'TESTSUITE ztest_params succeeded' but found {pass_ztest_params_count}" + +def test_run_testsuite_once(shell: Shell): + lines = shell.exec_command('ztest run-testsuite ztest_params') + # Join the list of lines into a single string + output = '\n'.join(lines) + assert "TESTSUITE ztest_params succeeded" in output, f"Expected 'TESTSUITE ztest_params succeeded' but got {lines}" + +def test_run_testcase_with_int_parameter(shell: Shell): + lines = shell.exec_command('ztest run-testcase ztest_params::test_int_param -p 44') + # Join the list of lines into a single string + output = '\n'.join(lines) + assert 'Passed int value:44' in output, 'expected response not found' + +@pytest.mark.parametrize("test_input", ["Testing", "Zephyr", "RTOS"]) +def test_run_testcase_with_string_parameter(shell: Shell, test_input): + lines = shell.exec_command(f'ztest run-testcase ztest_params::test_string_param -p {test_input}') + assert f'Passed string:{test_input}' in lines, 'expected response not found' diff --git a/tests/ztest/ztest_param/src/main.c b/tests/ztest/ztest_param/src/main.c new file mode 100644 index 00000000000..37a04715b56 --- /dev/null +++ b/tests/ztest/ztest_param/src/main.c @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +ZTEST_SUITE(ztest_params, NULL, NULL, NULL, NULL, NULL); + +ZTEST(ztest_params, test_dummy) +{ + printf("Dummy test\n"); +} + +ZTEST_P(ztest_params, test_int_param) +{ + if (data != NULL) { + printf("Passed int value:%d\n", atoi(data)); + } else { + printf("Run without parameter\n"); + } +} + +ZTEST_P(ztest_params, test_string_param) +{ + if (data != NULL) { + printf("Passed string:%s\n", (char *)data); + } else { + printf("Run without parameter\n"); + } +} diff --git a/tests/ztest/ztest_param/testcase.yaml b/tests/ztest/ztest_param/testcase.yaml new file mode 100644 index 00000000000..66494f8f6dd --- /dev/null +++ b/tests/ztest/ztest_param/testcase.yaml @@ -0,0 +1,6 @@ +tests: + testing.ztest.ztest_params.ztest_params: + platform_allow: + - qemu_x86 + tags: test_framework + harness: pytest