Skip to content

Commit

Permalink
tests: pytest: add an example for pytest
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
youhuazhu committed Jan 15, 2021
1 parent e664edd commit f0af9b4
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/pytest/pytest_sample/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)
2 changes: 2 additions & 0 deletions tests/pytest/pytest_sample/prj.conf
@@ -0,0 +1,2 @@
CONFIG_ZTEST=y
CONFIG_IDLE_STACK_SIZE=4096
14 changes: 14 additions & 0 deletions tests/pytest/pytest_sample/pytest/conftest.py
@@ -0,0 +1,14 @@
# Copyright (c) 2020 Intel Corporation.
#
# SPDX-License-Identifier: Apache-2.0

import pytest

def pytest_addoption(parser):
parser.addoption(
'--cmdopt'
)

@pytest.fixture()
def cmdopt(request):
return request.config.getoption('--cmdopt')
24 changes: 24 additions & 0 deletions tests/pytest/pytest_sample/pytest/test_ctf.py
@@ -0,0 +1,24 @@
# Copyright (c) 2020 Intel Corporation.
#
# SPDX-License-Identifier: Apache-2.0

import os
import pytest

@pytest.fixture(autouse=True)
def pytest_cmdopt_handle(cmdopt):
''' handle cmdopt '''
print("handle cmdopt:")
print(cmdopt)
data_path = cmdopt
os.environ['data_path'] = str(data_path)

def test_case(cmdopt):
''' test case '''
if not os.path.exists(cmdopt):
assert 0
print("run test cases in:")
print(cmdopt)

if __name__ == "__main__":
pytest.main()
19 changes: 19 additions & 0 deletions tests/pytest/pytest_sample/src/main.c
@@ -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);
}
4 changes: 4 additions & 0 deletions tests/pytest/pytest_sample/testcase.yaml
@@ -0,0 +1,4 @@
tests:
pytest.sample:
platform_allow: qemu_x86 qemu_x86_64
harness: pytest

0 comments on commit f0af9b4

Please sign in to comment.