Skip to content

Commit 732dd48

Browse files
gchwiernashif
authored andcommitted
twister: pytest: Simplify interface to pytest-twister-harness
Add DeviceAbstract class to default imports from pytest-twister-harness package to simplify importing DUT package, when creating tests. Signed-off-by: Grzegorz Chwierut <[email protected]>
1 parent 2958a40 commit 732dd48

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

doc/develop/test/pytest.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,14 @@ Pytest scans the given folder looking for tests, following its default
5757
One can also pass some extra arguments to the pytest from yaml file using ``pytest_args`` keyword
5858
under ``harness_config``, e.g.: ``pytest_args: [‘-k=test_method’, ‘--log-level=DEBUG’]``.
5959

60-
Two imports are important to include in .py sources:
60+
Following import is required to include in .py sources:
6161

6262
.. code-block:: python
6363
64-
import pytest # noqa # pylint: disable=unused-import
65-
from pytest_twister_harness.device.device_abstract import DeviceAbstract
64+
from twister_harness import Device
6665
67-
The first enables pytest-twister-harness plugin indirectly, as it is added with pytest.
68-
It also gives access to ``dut`` fixture. The second is important for type checking and enabling
69-
IDE hints for duts. The ``dut`` fixture is the core of pytest harness plugin. When used as an
66+
It is important for type checking and enabling IDE hints for ``dut``s (objects representing
67+
Devices Under Test). The ``dut`` fixture is the core of pytest harness plugin. When used as an
7068
argument of a test function it gives access to a DeviceAbstract type object. The fixture yields a
7169
device prepared according to the requested type (native posix, qemu, hardware, etc.). All types of
7270
devices share the same API. This allows for writing tests which are device-type-agnostic.

samples/subsys/testsuite/pytest/shell/pytest/test_shell.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import time
66
import logging
77

8-
from twister_harness.device.device_abstract import DeviceAbstract
8+
from twister_harness import Device
99

1010
logger = logging.getLogger(__name__)
1111

1212

13-
def wait_for_message(dut: DeviceAbstract, message, timeout=20):
13+
def wait_for_message(dut: Device, message, timeout=20):
1414
time_started = time.time()
1515
for line in dut.iter_stdout:
1616
if line:
@@ -21,7 +21,7 @@ def wait_for_message(dut: DeviceAbstract, message, timeout=20):
2121
return False
2222

2323

24-
def wait_for_prompt(dut: DeviceAbstract, prompt='uart:~$', timeout=20):
24+
def wait_for_prompt(dut: Device, prompt='uart:~$', timeout=20):
2525
time_started = time.time()
2626
while True:
2727
dut.write(b'\n')
@@ -33,13 +33,13 @@ def wait_for_prompt(dut: DeviceAbstract, prompt='uart:~$', timeout=20):
3333
return False
3434

3535

36-
def test_shell_print_help(dut: DeviceAbstract):
36+
def test_shell_print_help(dut: Device):
3737
wait_for_prompt(dut)
3838
dut.write(b'help\n')
3939
assert wait_for_message(dut, "Available commands")
4040

4141

42-
def test_shell_print_version(dut: DeviceAbstract):
42+
def test_shell_print_version(dut: Device):
4343
wait_for_prompt(dut)
4444
dut.write(b'kernel version\n')
4545
assert wait_for_message(dut, "Zephyr version")

scripts/pylib/pytest-twister-harness/src/twister_harness/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
# flake8: noqa
6+
7+
from twister_harness.device.device_abstract import DeviceAbstract as Device
8+
9+
__all__= ['Device']
10+
511
__version__ = '0.0.1'

0 commit comments

Comments
 (0)