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/support skip test cases without build dir #310

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 15 additions & 3 deletions pytest-embedded/pytest_embedded/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from operator import itemgetter

import pytest
from _pytest.config import Config
from _pytest.config import Config, ExitCode
from _pytest.fixtures import (
FixtureRequest,
)
Expand Down Expand Up @@ -111,6 +111,11 @@ def pytest_addoption(parser):
help='y/yes/true for True and n/no/false for False. '
'Set to True to prettify XML junit report. (Default: False)',
)
base_group.addoption(
'--suppress-no-test-collected-error',
help='y/yes/true for True and n/no/false for False. '
'Set to True to suppress the error when no test cases are collected. (Default: False)',
)

# supports parametrization
base_group.addoption('--root-logdir', help='set session-based root log dir. (Default: system temp folder)')
Expand Down Expand Up @@ -1191,6 +1196,7 @@ def pytest_configure(config: Config) -> None:
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)),
suppress_no_test_collected_error=_str_bool(config.getoption('suppress_no_test_collected_error', False)),
)
config.pluginmanager.register(config.stash[_pytest_embedded_key])

Expand All @@ -1210,12 +1216,14 @@ def __init__(
check_duplicates: bool = False,
prettify_junit_report: bool = False,
add_target_as_marker: bool = False,
suppress_no_test_collected_error: 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
self.suppress_no_test_collected_error = suppress_no_test_collected_error

@staticmethod
def _raise_dut_failed_cases_if_exists(duts: t.Iterable[Dut]) -> None:
Expand Down Expand Up @@ -1251,7 +1259,7 @@ def pytest_collection_modifyitems(self, items: t.List[Function]):

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

yield

Expand Down Expand Up @@ -1297,6 +1305,10 @@ def pytest_runtest_call(self, item: Function):

@pytest.hookimpl(trylast=True) # combine all possible junit reports should be the last step
def pytest_sessionfinish(self, session: Session, exitstatus: int) -> None:
if exitstatus == ExitCode.NO_TESTS_COLLECTED and self.suppress_no_test_collected_error:
session.exitstatus = 0
return

modifier: JunitMerger = session.config.stash[_junit_merger_key]
_stash_session_tempdir = session.config.stash.get(_session_tempdir_key, None)
_stash_junit_report_path = session.config.stash.get(_junit_report_path_key, None)
Expand All @@ -1314,4 +1326,4 @@ def pytest_sessionfinish(self, session: Session, exitstatus: int) -> None:
if self.prettify_junit_report:
_prettify_xml(_stash_junit_report_path)

exitstatus = int(modifier.failed) # True -> 1 False -> 0 # noqa
session.exitstatus = int(modifier.failed) # True -> 1 False -> 0
10 changes: 10 additions & 0 deletions pytest-embedded/tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
import subprocess
import xml.etree.ElementTree as ET
from pathlib import Path

import pytest
from _pytest.config import ExitCode


def test_help(testdir):
Expand Down Expand Up @@ -632,3 +634,11 @@ def test_unclosed_file_handler(test_input, dut):
'-x', # fail at the first fail
)
result.assert_outcomes(passed=1024)


def test_suppress_no_test_collected_error():
ret_code = subprocess.call(['pytest'])
assert ret_code == ExitCode.NO_TESTS_COLLECTED

ret_code = subprocess.call(['pytest', '--suppress-no-test-collected-error', 'y'])
assert ret_code == 0
Loading