From 2dc693bbed11faff2c302ea0aa07b18681b9afb7 Mon Sep 17 00:00:00 2001 From: YouhuaX Zhu Date: Fri, 15 Jan 2021 15:58:22 +0800 Subject: [PATCH] tests: pytest: add an example for pytest In this example, python test case get the running directory by handling the "--cmdopt" option passed by pytest. Signed-off-by: YouhuaX Zhu --- .../subsys/testsuite/pytest/CMakeLists.txt | 7 ++++ samples/subsys/testsuite/pytest/prj.conf | 2 + .../testsuite/pytest/pytest/conftest.py | 18 +++++++++ .../testsuite/pytest/pytest/test_sample.py | 37 +++++++++++++++++++ samples/subsys/testsuite/pytest/src/main.c | 19 ++++++++++ samples/subsys/testsuite/pytest/testcase.yaml | 5 +++ 6 files changed, 88 insertions(+) create mode 100644 samples/subsys/testsuite/pytest/CMakeLists.txt create mode 100644 samples/subsys/testsuite/pytest/prj.conf create mode 100644 samples/subsys/testsuite/pytest/pytest/conftest.py create mode 100755 samples/subsys/testsuite/pytest/pytest/test_sample.py create mode 100644 samples/subsys/testsuite/pytest/src/main.c create mode 100644 samples/subsys/testsuite/pytest/testcase.yaml diff --git a/samples/subsys/testsuite/pytest/CMakeLists.txt b/samples/subsys/testsuite/pytest/CMakeLists.txt new file mode 100644 index 00000000000..ed70dc5e6cf --- /dev/null +++ b/samples/subsys/testsuite/pytest/CMakeLists.txt @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(pytest_sample) + +target_sources(app PRIVATE src/main.c) diff --git a/samples/subsys/testsuite/pytest/prj.conf b/samples/subsys/testsuite/pytest/prj.conf new file mode 100644 index 00000000000..c3e81438ced --- /dev/null +++ b/samples/subsys/testsuite/pytest/prj.conf @@ -0,0 +1,2 @@ +CONFIG_ZTEST=y +CONFIG_IDLE_STACK_SIZE=4096 diff --git a/samples/subsys/testsuite/pytest/pytest/conftest.py b/samples/subsys/testsuite/pytest/pytest/conftest.py new file mode 100644 index 00000000000..197c1c4a238 --- /dev/null +++ b/samples/subsys/testsuite/pytest/pytest/conftest.py @@ -0,0 +1,18 @@ +# Copyright (c) 2020 Intel Corporation. +# +# SPDX-License-Identifier: Apache-2.0 + +import pytest + +# add option "--comdopt" to pytest, or it will report "unknown option" +# this option is passed from twister. +def pytest_addoption(parser): + parser.addoption( + '--cmdopt' + ) + +# define fixture to return value of option "--cmdopt", this fixture +# will be requested by other fixture of tests. +@pytest.fixture() +def cmdopt(request): + return request.config.getoption('--cmdopt') diff --git a/samples/subsys/testsuite/pytest/pytest/test_sample.py b/samples/subsys/testsuite/pytest/pytest/test_sample.py new file mode 100755 index 00000000000..70d5950e935 --- /dev/null +++ b/samples/subsys/testsuite/pytest/pytest/test_sample.py @@ -0,0 +1,37 @@ +# Copyright (c) 2020 Intel Corporation. +# +# SPDX-License-Identifier: Apache-2.0 + +import os +import pytest + +# fixture cmdopt defined in conftest.py, it can be requested either in +# tests or in other fixtures + +@pytest.fixture(autouse=True) +def pytest_cmdopt_handle(cmdopt): + ''' An auto fixture, all tests automatically request this fixture. + Argument "cmdopt" is a fixture defined in conftest.py, it returns + the value of an option passed by twister, this fixture export + that value to environment. + ''' + print("handle cmdopt:") + print(cmdopt) + data_path = cmdopt + os.environ['data_path'] = str(data_path) + +def test_case(cmdopt): + ''' Test cases make use of fixture cmdopt to get the value of "--cmdopt" option + passed by twister. Actually, fixture cmdopt returns a path of the directory + which holds the artifacts generated by ztest. The main work of test cases + in this file is to check those stuff in that directory. + This test case simply compare the return value of cmdopt with the + environment varialbe exported by fixture pytest_cmdopt_handle. + ''' + assert os.path.exists(cmdopt) + + print("run test cases in:") + print(cmdopt) + +if __name__ == "__main__": + pytest.main() diff --git a/samples/subsys/testsuite/pytest/src/main.c b/samples/subsys/testsuite/pytest/src/main.c new file mode 100644 index 00000000000..dfb6085e9c6 --- /dev/null +++ b/samples/subsys/testsuite/pytest/src/main.c @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2020 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include +void test_pytest(void) +{ + TC_PRINT("Hello world\n"); +} + +void test_main(void) +{ + ztest_test_suite(test_pytest, + ztest_unit_test(test_pytest) + ); + + ztest_run_test_suite(test_pytest); +} diff --git a/samples/subsys/testsuite/pytest/testcase.yaml b/samples/subsys/testsuite/pytest/testcase.yaml new file mode 100644 index 00000000000..303f7f48e86 --- /dev/null +++ b/samples/subsys/testsuite/pytest/testcase.yaml @@ -0,0 +1,5 @@ +tests: + sample.twister.pytest: + platform_allow: native_posix + harness: pytest + tags: testing pytest