Skip to content

Commit aabe621

Browse files
authored
Handle single testsuite (#353)
1 parent 1bc1406 commit aabe621

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

lms/lmstests/public/unittests/services.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
import subprocess # noqa: S404
3-
from typing import Iterable, List, Optional, Tuple
4-
3+
from typing import Iterable, List, Optional
54
from flask_babel import gettext as _ # type: ignore
65
import junitparser
76
from junitparser.junitparser import TestCase
@@ -51,7 +50,6 @@ def _run_tests_on_solution(self):
5150
python_file = 'test_checks.py'
5251
test_output_path = 'output.xml'
5352

54-
junit_results = None
5553
try:
5654
with executers.get_executor(self._executor_name) as executor:
5755
executor.write_file(python_file, python_code)
@@ -78,12 +76,17 @@ def _generate_python_code(self) -> str:
7876
test_code = self._exercise_auto_test.code
7977
return f'{test_code}\n\n{user_code}'
8078

81-
def _get_parsed_suites(
79+
def _get_test_cases(
8280
self, raw_results: bytes,
83-
) -> Optional[Iterable[junitparser.TestSuite]]:
81+
) -> Optional[Iterable[junitparser.TestCase]]:
8482
try:
8583
parsed_string = junitparser.TestSuite.fromstring(raw_results)
86-
return parsed_string.testsuites()
84+
test_suites = tuple(parsed_string.testsuites())
85+
single_test_suite = not test_suites
86+
if single_test_suite:
87+
yield from tuple(parsed_string)
88+
for test_suite in test_suites:
89+
yield from tuple(test_suite)
8790
except SyntaxError: # importing xml make the lint go arrrr
8891
self._logger.exception('Failed to parse junit result')
8992
return None
@@ -98,17 +101,15 @@ def _populate_junit_results(self, raw_results: bytes) -> None:
98101
if not raw_results:
99102
return None
100103

101-
suites = self._get_parsed_suites(raw_results)
102-
if not suites:
104+
test_cases = self._get_test_cases(raw_results)
105+
if not test_cases:
103106
return None
104107

105108
tests_ran = False
106109
number_of_failures = 0
107-
for test_suite in suites:
108-
failures, ran = self._handle_test_suite(test_suite)
109-
number_of_failures += failures
110-
if ran and not tests_ran:
111-
tests_ran = ran
110+
for test_case in test_cases:
111+
number_of_failures += int(self._handle_test_case(test_case))
112+
tests_ran = True
112113

113114
if not tests_ran:
114115
self._handle_failed_to_execute_tests(raw_results)
@@ -181,13 +182,3 @@ def _handle_test_case(self, case: TestCase) -> NumberOfErrors:
181182
self._handle_result(case.name, result)
182183
number_of_failures += 1
183184
return number_of_failures
184-
185-
def _handle_test_suite(
186-
self, test_suite: junitparser.TestSuite,
187-
) -> Tuple[int, bool]:
188-
number_of_failures = 0
189-
tests_ran = False
190-
for case in test_suite:
191-
tests_ran = True
192-
number_of_failures += int(self._handle_test_case(case))
193-
return number_of_failures, tests_ran

0 commit comments

Comments
 (0)