diff --git a/pytest-embedded/pytest_embedded/plugin.py b/pytest-embedded/pytest_embedded/plugin.py index 8c7aab5e..974f09b2 100644 --- a/pytest-embedded/pytest_embedded/plugin.py +++ b/pytest-embedded/pytest_embedded/plugin.py @@ -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, ) @@ -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)') @@ -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]) @@ -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: @@ -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 @@ -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) @@ -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 diff --git a/pytest-embedded/tests/test_base.py b/pytest-embedded/tests/test_base.py index 5f1a494f..e2e3cd71 100644 --- a/pytest-embedded/tests/test_base.py +++ b/pytest-embedded/tests/test_base.py @@ -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): @@ -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