samples: testsuite: exemplary pytest shell test
Adding exemplary pytest shell test to show possibilities of new pytest
plugin. This test uses bidirectional communication between tester and
device under test.
Signed-off-by: Grzegorz Chwierut <grzegorz.chwierut@nordicsemi.no>
diff --git a/samples/subsys/testsuite/pytest/CMakeLists.txt b/samples/subsys/testsuite/pytest/basic/CMakeLists.txt
similarity index 100%
rename from samples/subsys/testsuite/pytest/CMakeLists.txt
rename to samples/subsys/testsuite/pytest/basic/CMakeLists.txt
diff --git a/samples/subsys/testsuite/pytest/prj.conf b/samples/subsys/testsuite/pytest/basic/prj.conf
similarity index 100%
rename from samples/subsys/testsuite/pytest/prj.conf
rename to samples/subsys/testsuite/pytest/basic/prj.conf
diff --git a/samples/subsys/testsuite/pytest/pytest/conftest.py b/samples/subsys/testsuite/pytest/basic/pytest/conftest.py
similarity index 100%
rename from samples/subsys/testsuite/pytest/pytest/conftest.py
rename to samples/subsys/testsuite/pytest/basic/pytest/conftest.py
diff --git a/samples/subsys/testsuite/pytest/pytest/test_sample.py b/samples/subsys/testsuite/pytest/basic/pytest/test_sample.py
similarity index 100%
rename from samples/subsys/testsuite/pytest/pytest/test_sample.py
rename to samples/subsys/testsuite/pytest/basic/pytest/test_sample.py
diff --git a/samples/subsys/testsuite/pytest/src/main.c b/samples/subsys/testsuite/pytest/basic/src/main.c
similarity index 100%
rename from samples/subsys/testsuite/pytest/src/main.c
rename to samples/subsys/testsuite/pytest/basic/src/main.c
diff --git a/samples/subsys/testsuite/pytest/testcase.yaml b/samples/subsys/testsuite/pytest/basic/testcase.yaml
similarity index 74%
rename from samples/subsys/testsuite/pytest/testcase.yaml
rename to samples/subsys/testsuite/pytest/basic/testcase.yaml
index 95159d7..55f2f11 100644
--- a/samples/subsys/testsuite/pytest/testcase.yaml
+++ b/samples/subsys/testsuite/pytest/basic/testcase.yaml
@@ -3,7 +3,7 @@
platform_allow: native_posix
harness: pytest
harness_config:
- pytest_args: ["--custom-pytest-arg", "foo"]
+ pytest_args: ["--custom-pytest-arg", "foo", "--cmdopt", "."]
tags:
- testing
- pytest
diff --git a/samples/subsys/testsuite/pytest/shell/CMakeLists.txt b/samples/subsys/testsuite/pytest/shell/CMakeLists.txt
new file mode 100644
index 0000000..a8fae2b
--- /dev/null
+++ b/samples/subsys/testsuite/pytest/shell/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(shell)
+
+FILE(GLOB app_sources src/*.c)
+target_sources(app PRIVATE ${app_sources})
diff --git a/samples/subsys/testsuite/pytest/shell/prj.conf b/samples/subsys/testsuite/pytest/shell/prj.conf
new file mode 100644
index 0000000..3778838
--- /dev/null
+++ b/samples/subsys/testsuite/pytest/shell/prj.conf
@@ -0,0 +1,5 @@
+CONFIG_PRINTK=y
+CONFIG_SHELL=y
+CONFIG_LOG=y
+CONFIG_SHELL_BACKEND_SERIAL=y
+CONFIG_KERNEL_SHELL=y
diff --git a/samples/subsys/testsuite/pytest/shell/pytest/test_shell.py b/samples/subsys/testsuite/pytest/shell/pytest/test_shell.py
new file mode 100755
index 0000000..70e7ca8
--- /dev/null
+++ b/samples/subsys/testsuite/pytest/shell/pytest/test_shell.py
@@ -0,0 +1,36 @@
+# Copyright (c) 2023 Nordic Semiconductor ASA
+#
+# SPDX-License-Identifier: Apache-2.0
+
+import time
+import logging
+import pytest # noqa # pylint: disable=unused-import
+
+from twister_harness.device.device_abstract import DeviceAbstract
+
+logger = logging.getLogger(__name__)
+
+
+def wait_for_message(iter_stdout, message, timeout=60):
+ time_started = time.time()
+ for line in iter_stdout:
+ if line:
+ logger.debug("#: " + line)
+ if message in line:
+ return True
+ if time.time() > time_started + timeout:
+ return False
+
+
+def test_shell_print_help(dut: DeviceAbstract):
+ time.sleep(1) # wait for application initialization on DUT
+
+ dut.write(b'help\n')
+ assert wait_for_message(dut.iter_stdout, "Available commands")
+
+
+def test_shell_print_version(dut: DeviceAbstract):
+ time.sleep(1) # wait for application initialization on DUT
+
+ dut.write(b'kernel version\n')
+ assert wait_for_message(dut.iter_stdout, "Zephyr version")
diff --git a/samples/subsys/testsuite/pytest/shell/src/main.c b/samples/subsys/testsuite/pytest/shell/src/main.c
new file mode 100644
index 0000000..9839489
--- /dev/null
+++ b/samples/subsys/testsuite/pytest/shell/src/main.c
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2023 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+int main(void)
+{
+ /* Shell application source code is injected by applied Kconfg SHELL
+ * options, no more "extra" functionalities are required for exemplary
+ * pytest test.
+ */
+ return 0;
+}
diff --git a/samples/subsys/testsuite/pytest/shell/testcase.yaml b/samples/subsys/testsuite/pytest/shell/testcase.yaml
new file mode 100644
index 0000000..bd9c57b
--- /dev/null
+++ b/samples/subsys/testsuite/pytest/shell/testcase.yaml
@@ -0,0 +1,14 @@
+tests:
+ sample.pytest.shell:
+ filter: CONFIG_SERIAL and dt_chosen_enabled("zephyr,shell-uart")
+ min_ram: 40
+ harness: pytest
+ extra_configs:
+ - CONFIG_NATIVE_UART_0_ON_STDINOUT=y
+ integration_platforms:
+ - native_posix
+ - qemu_cortex_m3
+ tags:
+ - testing
+ - pytest
+ - shell