Skip to content

Commit

Permalink
trunner: add support for pytests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-bloch committed Apr 3, 2024
1 parent d8f54fa commit 290d271
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
7 changes: 6 additions & 1 deletion trunner/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import yaml

from trunner.ctx import TestContext
from trunner.harness import PyHarness, unity_harness
from trunner.harness import PyHarness, unity_harness, pytest_harness
from trunner.types import AppOptions, BootloaderOptions, TestOptions, ShellOptions


Expand Down Expand Up @@ -62,6 +62,8 @@ def _parse_type(self, config: dict):
self._parse_pyharness(config)
elif test_type == "unity":
self._parse_unity()
elif test_type == "pytest":
self._parse_pytest()
else:
raise ParserError("unknown key!")

Expand Down Expand Up @@ -99,6 +101,9 @@ def _parse_pyharness(self, config: dict):
def _parse_unity(self):
self.test.harness = PyHarness(self.ctx.target.dut, self.ctx, unity_harness, self.test.kwargs)

def _parse_pytest(self):
self.test.harness = PyHarness(self.ctx.host.dut, self.ctx, pytest_harness, self.test.kwargs)

def _parse_load(self, config: dict):
apps = config.get("load", [])
apps_to_boot = []
Expand Down
2 changes: 2 additions & 0 deletions trunner/harness/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .psh import ShellHarness
from .pyharness import PyHarness
from .unity import unity_harness
from .pytest import pytest_harness

__all__ = [
"HarnessBuilder",
Expand All @@ -46,4 +47,5 @@
"PloImageProperty",
"PloJffsImageProperty",
"unity_harness",
"pytest_harness"
]
35 changes: 35 additions & 0 deletions trunner/harness/pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Optional

from trunner.ctx import TestContext
from trunner.dut import HostDut
from trunner.types import Status, TestResult

def pytest_harness(dut: HostDut, ctx: TestContext, result: TestResult, **kwargs) -> Optional[TestResult]:

Check failure on line 7 in trunner/harness/pytest.py

View workflow job for this annotation

GitHub Actions / build (3.11)

E302 expected 2 blank lines, found 1

Check failure on line 7 in trunner/harness/pytest.py

View workflow job for this annotation

GitHub Actions / build (3.9)

E302 expected 2 blank lines, found 1

Check failure on line 7 in trunner/harness/pytest.py

View workflow job for this annotation

GitHub Actions / build (3.10)

E302 expected 2 blank lines, found 1
test_re = r"\:\:(?P<group>.+)\:\:(?P<name>.+)(?P<status>PASSED|SKIPPED|FAILED|ERROR)"
final_re = r"\[100\%\]"

status = Status.OK

test_path = kwargs.get("path")
cmd = f"pytest -v --tb=no {test_path}"

dut.set_args(cmd, encoding="utf-8")
dut.open()

while True:
idx = dut.expect([test_re, final_re], timeout=200)
parsed = dut.match.groupdict()

if idx == 0:
sub_status = Status.from_str(parsed["status"])
if sub_status is Status.FAIL:
status = sub_status
subname = f"{parsed['name']}"

Check warning on line 27 in trunner/harness/pytest.py

View workflow job for this annotation

GitHub Actions / build (3.11)

F841 local variable 'subname' is assigned to but never used

Check warning on line 27 in trunner/harness/pytest.py

View workflow job for this annotation

GitHub Actions / build (3.9)

F841 local variable 'subname' is assigned to but never used

Check warning on line 27 in trunner/harness/pytest.py

View workflow job for this annotation

GitHub Actions / build (3.10)

F841 local variable 'subname' is assigned to but never used
# result.add_subresult(subname, sub_status)
continue

elif idx == 1:
break


return TestResult(status=status, msg="")

Check failure on line 35 in trunner/harness/pytest.py

View workflow job for this annotation

GitHub Actions / build (3.11)

E303 too many blank lines (2)

Check failure on line 35 in trunner/harness/pytest.py

View workflow job for this annotation

GitHub Actions / build (3.9)

E303 too many blank lines (2)

Check failure on line 35 in trunner/harness/pytest.py

View workflow job for this annotation

GitHub Actions / build (3.10)

E303 too many blank lines (2)
6 changes: 5 additions & 1 deletion trunner/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
from abc import ABC, abstractmethod

from trunner.ctx import TestContext

from trunner.dut import HostDut

class Host(ABC): # pylint: disable=too-few-public-methods

Check failure on line 7 in trunner/host.py

View workflow job for this annotation

GitHub Actions / build (3.11)

E302 expected 2 blank lines, found 1

Check failure on line 7 in trunner/host.py

View workflow job for this annotation

GitHub Actions / build (3.9)

E302 expected 2 blank lines, found 1

Check failure on line 7 in trunner/host.py

View workflow job for this annotation

GitHub Actions / build (3.10)

E302 expected 2 blank lines, found 1
"""Base class for Host abstraction"""
name: str

def __init__(self) -> None:
super().__init__()
self.dut: HostDut = HostDut()

@abstractmethod
def has_gpio(self) -> bool:
"""Return true if host has gpio utility and can restart the target device using it."""
Expand Down
2 changes: 1 addition & 1 deletion trunner/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def run_tests(self, tests: Sequence[TestOptions]) -> Sequence[TestResult]:
if test.ignore:
result.skip()
else:
set_logfiles(self.target.dut, self.ctx)
set_logfiles(self.ctx.target.dut and self.ctx.host.dut, self.ctx)
harness = self.target.build_test(test)

if not test.should_reboot: # WARN: build_test may change TestOptions
Expand Down
2 changes: 1 addition & 1 deletion trunner/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Status(Enum):

@classmethod
def from_str(cls, s):
if s in ("FAIL", "FAILED", "BAD"):
if s in ("FAIL", "FAILED", "BAD", "ERROR"):
return Status.FAIL
if s in ("OK", "PASS", "PASSED"):
return Status.OK
Expand Down

0 comments on commit 290d271

Please sign in to comment.