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 <youhuax.zhu@intel.com>
This commit is contained in:
parent
965c8b9888
commit
2dc693bbed
6 changed files with 88 additions and 0 deletions
7
samples/subsys/testsuite/pytest/CMakeLists.txt
Normal file
7
samples/subsys/testsuite/pytest/CMakeLists.txt
Normal file
|
@ -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)
|
2
samples/subsys/testsuite/pytest/prj.conf
Normal file
2
samples/subsys/testsuite/pytest/prj.conf
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
CONFIG_ZTEST=y
|
||||||
|
CONFIG_IDLE_STACK_SIZE=4096
|
18
samples/subsys/testsuite/pytest/pytest/conftest.py
Normal file
18
samples/subsys/testsuite/pytest/pytest/conftest.py
Normal file
|
@ -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')
|
37
samples/subsys/testsuite/pytest/pytest/test_sample.py
Executable file
37
samples/subsys/testsuite/pytest/pytest/test_sample.py
Executable file
|
@ -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()
|
19
samples/subsys/testsuite/pytest/src/main.c
Normal file
19
samples/subsys/testsuite/pytest/src/main.c
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020 Intel Corporation
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
#include <ztest.h>
|
||||||
|
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);
|
||||||
|
}
|
5
samples/subsys/testsuite/pytest/testcase.yaml
Normal file
5
samples/subsys/testsuite/pytest/testcase.yaml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
tests:
|
||||||
|
sample.twister.pytest:
|
||||||
|
platform_allow: native_posix
|
||||||
|
harness: pytest
|
||||||
|
tags: testing pytest
|
Loading…
Add table
Add a link
Reference in a new issue