scripts: pytest: add pytester tests

Add tests which use pytester to call pytest with pytest-twister-harness
plugin on previously defined test. This gives a possibility to test
plugin in end-to-end variant.

Signed-off-by: Piotr Golyzniak <piotr.golyzniak@nordicsemi.no>
This commit is contained in:
Piotr Golyzniak 2023-08-07 11:10:35 +02:00 committed by Anas Nashif
commit 577d421ba7
2 changed files with 76 additions and 8 deletions

View file

@ -2,12 +2,13 @@
#
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import os
from pathlib import Path
import pytest
# pytest_plugins = ['pytester']
@pytest.fixture
def resources(request: pytest.FixtureRequest) -> Path:
@ -15,9 +16,17 @@ def resources(request: pytest.FixtureRequest) -> Path:
return Path(request.module.__file__).parent.joinpath('data')
@pytest.fixture(scope='function')
def copy_example(pytester) -> Path:
"""Copy example tests to temporary directory and return path the temp directory."""
resources_dir = Path(__file__).parent / 'data'
pytester.copy_example(str(resources_dir))
return pytester.path
@pytest.fixture
def zephyr_base() -> str:
zephyr_base_path = os.getenv('ZEPHYR_BASE')
if zephyr_base_path is None:
pytest.fail('Environmental variable ZEPHYR_BASE has to be set.')
else:
return zephyr_base_path
@pytest.fixture
def twister_harness(zephyr_base) -> str:
"""Retrun path to pytest-twister-harness src directory"""
pytest_twister_harness_path = str(Path(zephyr_base) / 'scripts' / 'pylib' / 'pytest-twister-harness' / 'src')
return pytest_twister_harness_path

View file

@ -0,0 +1,59 @@
# Copyright (c) 2023 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
import os
import textwrap
from pathlib import Path
import pytest
pytest_plugins = ['pytester']
@pytest.mark.parametrize(
'import_path, class_name, device_type',
[
('twister_harness.device.binary_adapter', 'NativeSimulatorAdapter', 'native'),
('twister_harness.device.qemu_adapter', 'QemuAdapter', 'qemu'),
('twister_harness.device.hardware_adapter', 'HardwareAdapter', 'hardware'),
],
ids=[
'native',
'qemu',
'hardware',
]
)
def test_if_adapter_is_chosen_properly(
import_path: str,
class_name: str,
device_type: str,
tmp_path: Path,
twister_harness: str,
pytester: pytest.Pytester,
):
pytester.makepyfile(
textwrap.dedent(
f"""
from twister_harness import DeviceAdapter
from {import_path} import {class_name}
def test_plugin(device_object):
assert isinstance(device_object, DeviceAdapter)
assert type(device_object) == {class_name}
"""
)
)
build_dir = tmp_path / 'build_dir'
os.mkdir(build_dir)
pytester.syspathinsert(twister_harness)
result = pytester.runpytest(
'--twister-harness',
f'--build-dir={build_dir}',
f'--device-type={device_type}',
'-p', 'twister_harness.plugin'
)
assert result.ret == 0
result.assert_outcomes(passed=1, failed=0, errors=0, skipped=0)