Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(esp): support add target as marker ('|' will be escaped to '-') #287

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion pytest-embedded/pytest_embedded/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ def pytest_addoption(parser):
esp_group = parser.getgroup('embedded-esp')
esp_group.addoption('--target', help='serial target chip type. (Default: "auto")')
esp_group.addoption('--beta-target', help='serial target beta version chip type. (Default: same as [--target])')
esp_group.addoption(
'--add-target-as-marker',
help='add target param as a function marker. Useful in CI with runners with different tags.'
'y/yes/true for True and n/no/false for False. '
'(Default: False, parametrization not supported, `|` will be escaped to `-`)',
)
esp_group.addoption(
'--skip-autoflash',
help='y/yes/true for True and n/no/false for False. Set to True to disable auto flash. (Default: False)',
Expand Down Expand Up @@ -1160,6 +1166,7 @@ def pytest_configure(config: Config) -> None:
parallel_index=config.getoption('parallel_index'),
check_duplicates=config.getoption('check_duplicates', False),
prettify_junit_report=_str_bool(config.getoption('prettify_junit_report', False)),
add_target_as_marker=_str_bool(config.getoption('add_target_as_marker', False)),
)
config.pluginmanager.register(config.stash[_pytest_embedded_key])

Expand All @@ -1178,11 +1185,13 @@ def __init__(
parallel_index: int = 1,
check_duplicates: bool = False,
prettify_junit_report: bool = False,
add_target_as_marker: bool = False,
):
self.parallel_count = parallel_count
self.parallel_index = parallel_index
self.check_duplicates = check_duplicates
self.prettify_junit_report = prettify_junit_report
self.add_target_as_marker = add_target_as_marker

@staticmethod
def _raise_dut_failed_cases_if_exists(duts: t.Iterable[Dut]) -> None:
Expand All @@ -1207,8 +1216,21 @@ def _duplicate_items(items: t.List[_T]) -> t.List[_T]:

return duplicates

@pytest.hookimpl(trylast=True)
@pytest.hookimpl(hookwrapper=True, trylast=True)
def pytest_collection_modifyitems(self, items: t.List[Function]):
if self.add_target_as_marker:
for item in items:
item_target = item.callspec.getparam('target')

if not isinstance(item_target, str):
raise ValueError(f'`target` should be a string, got {type(item_target)} instead')

if item_target:
# https://github.com/pytest-dev/pytest/pull/12277
item.add_marker(item_target.replace('|', '-')) # '|' is not supported until 8.2.0

yield

if self.check_duplicates:
duplicated_test_cases = self._duplicate_items([test.name for test in items])
if duplicated_test_cases:
Expand Down
30 changes: 15 additions & 15 deletions pytest-embedded/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import xml.etree.ElementTree as ET
from dataclasses import dataclass
from pathlib import Path

import pytest
Expand Down Expand Up @@ -162,20 +161,21 @@ def test_default_app_path(app):
(2, 1, 3),
(2, 2, 2),
])
def test_parallel_run(parallel_count, parallel_index, res):
from pytest_embedded.plugin import PytestEmbedded

@dataclass
class _FakeItem:
name: str
path = Path('.')

# _Fake instances are used in order to pass `pytest_collection_modifyitems` hook
fake_items = [_FakeItem(name='1'), _FakeItem(name='2'), _FakeItem(name='3'),
_FakeItem(name='4'), _FakeItem(name='5')]
fake_plugin = PytestEmbedded(parallel_count, parallel_index)
fake_plugin.pytest_collection_modifyitems(fake_items)
assert len(fake_items) == res
def test_parallel_run(testdir, parallel_count, parallel_index, res):
testdir.makepyfile(r"""
def test_1(dut): pass
def test_2(dut): pass
def test_3(dut): pass
def test_4(dut): pass
def test_5(dut): pass
""")

result = testdir.runpytest(
'--parallel-count', parallel_count,
'--parallel-index', parallel_index,
)

result.assert_outcomes(passed=res)


def test_expect(testdir):
Expand Down
Loading