Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
6 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CONFIG_ZTEST=y | ||
CONFIG_IDLE_STACK_SIZE=4096 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
tests: | ||
pytest.sample: | ||
platform_allow: qemu_x86 qemu_x86_64 | ||
harness: pytest |