Skip to content

Commit 5bc9a36

Browse files
committed
feat(junit-merger): add flag to preserve Python test cases and PYTHON_FUNC attribute
1 parent 43a417c commit 5bc9a36

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

pytest-embedded-idf/tests/test_idf.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,11 @@ def test_unity_test_case_runner(unity_tester):
877877
assert junit_report[3].find('failure') is None
878878

879879

880+
def test_unity_test_case_runner_new(unity_tester):
881+
unity_tester.run_all_cases()
882+
883+
884+
880885
def test_erase_all_with_port_cache(testdir):
881886
testdir.makepyfile(r"""
882887
def test_erase_all_with_port_cache_case1(dut):
@@ -897,3 +902,93 @@ def test_erase_all_with_port_cache_case2(dut):
897902
)
898903

899904
result.assert_outcomes(passed=2)
905+
906+
907+
def test_no_preserve_python_tests(testdir):
908+
testdir.makepyfile(r"""
909+
def test_python_case(dut):
910+
dut.run_all_single_board_cases(name=["normal_case1", "multiple_stages_test"])
911+
""")
912+
913+
testdir.runpytest(
914+
'-s',
915+
'--embedded-services', 'esp,idf',
916+
'--app-path', os.path.join(testdir.tmpdir, 'unit_test_app_esp32'),
917+
'--log-cli-level', 'DEBUG',
918+
'--junitxml', 'report.xml',
919+
)
920+
921+
junit_report = ET.parse('report.xml').getroot()[0]
922+
923+
assert junit_report.attrib['tests'] == '2'
924+
for testcase in junit_report.findall('testcase'):
925+
assert testcase.attrib['PYTHON_FUNC'] == '0'
926+
927+
def test_preserve_python_tests(testdir):
928+
testdir.makepyfile(r"""
929+
def test_python_case(dut):
930+
dut.run_all_single_board_cases(name=["normal_case1", "multiple_stages_test"])
931+
""")
932+
933+
testdir.runpytest(
934+
'-s',
935+
'--embedded-services', 'esp,idf',
936+
'--app-path', os.path.join(testdir.tmpdir, 'unit_test_app_esp32'),
937+
'--log-cli-level', 'DEBUG',
938+
'--junitxml', 'report.xml',
939+
'--preserve-python-tests-in-report',
940+
)
941+
942+
junit_report = ET.parse('report.xml').getroot()[0]
943+
944+
assert junit_report.attrib['tests'] == '2'
945+
assert junit_report[0].attrib['PYTHON_FUNC'] == '1'
946+
for testcase in junit_report[1:]:
947+
assert testcase.attrib['PYTHON_FUNC'] == '0'
948+
949+
950+
def test_preserve_python_tests_with_failures(testdir):
951+
testdir.makepyfile(r"""
952+
def test_python_case(dut):
953+
dut.run_all_single_board_cases(name=["normal_case1", "normal_case2"])
954+
""")
955+
956+
testdir.runpytest(
957+
'-s',
958+
'--embedded-services', 'esp,idf',
959+
'--app-path', os.path.join(testdir.tmpdir, 'unit_test_app_esp32'),
960+
'--log-cli-level', 'DEBUG',
961+
'--junitxml', 'report.xml',
962+
'--preserve-python-tests-in-report',
963+
)
964+
965+
junit_report = ET.parse('report.xml').getroot()[0]
966+
967+
assert junit_report.attrib['failures'] == '1'
968+
assert junit_report[0].attrib['PYTHON_FUNC'] == '1' # Python test case is preserved
969+
assert junit_report[1].attrib['PYTHON_FUNC'] == '0' # C test case
970+
assert junit_report[1].find('failure') is None # normal_case1 passed
971+
assert junit_report[2].attrib['PYTHON_FUNC'] == '0'
972+
assert junit_report[2].find('failure') is not None # normal_case2 failed
973+
974+
975+
def test_python_func_attribute(testdir):
976+
testdir.makepyfile(r"""
977+
def test_python_case(dut):
978+
dut.run_all_single_board_cases(name=["normal_case1", "multiple_stages_test"])
979+
""")
980+
981+
testdir.runpytest(
982+
'-s',
983+
'--embedded-services', 'esp,idf',
984+
'--app-path', os.path.join(testdir.tmpdir, 'unit_test_app_esp32'),
985+
'--log-cli-level', 'DEBUG',
986+
'--junitxml', 'report.xml',
987+
'--preserve-python-tests-in-report',
988+
)
989+
990+
junit_report = ET.parse('report.xml').getroot()[0]
991+
992+
assert junit_report[0].attrib['PYTHON_FUNC'] == '1' # Python test case
993+
for testcase in junit_report[1:]:
994+
assert testcase.attrib['PYTHON_FUNC'] == '0' # Other test cases

pytest-embedded/pytest_embedded/plugin.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ def pytest_addoption(parser):
142142
base_group.addoption(
143143
'--logfile-extension', default='.log', help='set the extension format of the log files. (Default: ".log")'
144144
)
145+
base_group.addoption(
146+
'--preserve-python-tests-in-report',
147+
action='store_true',
148+
default=False,
149+
help='Preserve Python test cases in the JUnit report alongside other test cases. '
150+
'By default, Python test cases are replaced by the more detailed test case results '
151+
'from submodules (e.g., DUT test cases). Use this flag to retain both.',
152+
)
145153

146154
serial_group = parser.getgroup('embedded-serial')
147155
serial_group.addoption('--port', help='serial port. (Env: "ESPPORT" if service "esp" specified, Default: "None")')
@@ -1183,7 +1191,8 @@ def unity_tester(dut: t.Union['IdfDut', t.Tuple['IdfDut']]) -> t.Optional['CaseT
11831191

11841192

11851193
def pytest_configure(config: Config) -> None:
1186-
config.stash[_junit_merger_key] = JunitMerger(config.option.xmlpath)
1194+
preserve_python_tests_in_report = config.getoption('preserve_python_tests_in_report', False)
1195+
config.stash[_junit_merger_key] = JunitMerger(config.option.xmlpath, preserve_python_tests_in_report)
11871196
config.stash[_junit_report_path_key] = config.option.xmlpath
11881197

11891198
config.stash[_pytest_embedded_key] = PytestEmbedded(

pytest-embedded/pytest_embedded/unity.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ class JunitMerger:
206206
SUB_JUNIT_FILENAME = 'dut.xml'
207207
# multi-dut junit reports should be dut-[INDEX].xml
208208

209-
def __init__(self, main_junit: Optional[str]) -> None:
209+
def __init__(self, main_junit: Optional[str], preserve_python_tests_in_report: Optional[str] = False) -> None:
210210
self.junit_path = main_junit
211+
self.preserve_python_tests_in_report = preserve_python_tests_in_report
211212

212213
self._junit = None
213214

@@ -291,9 +292,13 @@ def merge(self, junit_files: List[str]):
291292
raise ValueError(f'Could\'t find test case {test_case_name}, dumped into "debug.xml" for debugging')
292293

293294
junit_case_is_fail = junit_case.find('failure') is not None
294-
junit_parent.remove(junit_case)
295+
296+
junit_case.attrib['PYTHON_FUNC'] = '1'
297+
if not self.preserve_python_tests_in_report:
298+
junit_parent.remove(junit_case)
295299

296300
for case in merging_cases:
301+
case.attrib['PYTHON_FUNC'] = '0'
297302
junit_parent.append(case)
298303

299304
junit_parent.attrib['errors'] = self._int_add(

0 commit comments

Comments
 (0)