Skip to content

Commit bfd73e0

Browse files
sulixshuahkh
authored andcommitted
kunit: tool: Parse and print the reason tests are skipped
When a KUnit test (or other KTAP test) is skipped, a "skip reason" can be provided. kunit.py has never done anything with this, ignoring anything included in the KTAP output after the 'SKIP' directive. Since we have it, and it's used, print it in a nice friendly yellow in parentheses after a skipped test's name. (And, by parsing it, it can be included in the JUnit results as well.) Link: https://lore.kernel.org/r/20260606013827.240790-1-david@davidgow.net Signed-off-by: David Gow <david@davidgow.net> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 5c1553d commit bfd73e0

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

tools/testing/kunit/kunit_parser.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ def __init__(self) -> None:
4444
self.subtests = [] # type: List[Test]
4545
self.log = [] # type: List[str]
4646
self.counts = TestCounts()
47+
self.skip_reason = ''
4748

4849
def __str__(self) -> str:
4950
"""Returns string representation of a Test class object."""
5051
return (f'Test({self.status}, {self.name}, {self.expected_count}, '
51-
f'{self.subtests}, {self.log}, {self.counts})')
52+
f'{self.subtests}, {self.log}, {self.counts}, {self.skip_reason})')
5253

5354
def __repr__(self) -> str:
5455
"""Returns string representation of a Test class object."""
@@ -352,9 +353,9 @@ def parse_test_plan(lines: LineStream, test: Test) -> bool:
352353
lines.pop()
353354
return True
354355

355-
TEST_RESULT = re.compile(r'^\s*(ok|not ok) ([0-9]+) ?(- )?([^#]*)( # .*)?$')
356+
TEST_RESULT = re.compile(r'^\s*(ok|not ok) ([0-9]+) ?(:?- )?([^#]*)( # .*)?$')
356357

357-
TEST_RESULT_SKIP = re.compile(r'^\s*(ok|not ok) ([0-9]+) ?(- )?(.*) # SKIP ?(.*)$')
358+
TEST_RESULT_SKIP = re.compile(r'^\s*(ok|not ok) ([0-9]+) ?(:?- )?(.*) # SKIP ?(.*)$')
358359

359360
def peek_test_name_match(lines: LineStream, test: Test) -> bool:
360361
"""
@@ -418,7 +419,7 @@ def parse_test_result(lines: LineStream, test: Test,
418419

419420
# Set name of test object
420421
if skip_match:
421-
test.name = skip_match.group(4) or skip_match.group(5)
422+
test.name = skip_match.group(4)
422423
else:
423424
test.name = match.group(4)
424425

@@ -431,6 +432,7 @@ def parse_test_result(lines: LineStream, test: Test,
431432
status = match.group(1)
432433
if skip_match:
433434
test.status = TestStatus.SKIPPED
435+
test.skip_reason = skip_match.group(5) or ''
434436
elif status == 'ok':
435437
test.status = TestStatus.SUCCESS
436438
else:
@@ -539,7 +541,10 @@ def format_test_result(test: Test, printer: Printer) -> str:
539541
if test.status == TestStatus.SUCCESS:
540542
return printer.green('[PASSED] ') + test.name
541543
if test.status == TestStatus.SKIPPED:
542-
return printer.yellow('[SKIPPED] ') + test.name
544+
skip_message = printer.yellow('[SKIPPED] ') + test.name
545+
if test.skip_reason != '':
546+
skip_message += printer.yellow(' (' + test.skip_reason + ')')
547+
return skip_message
543548
if test.status == TestStatus.NO_TESTS:
544549
return printer.yellow('[NO TESTS RUN] ') + test.name
545550
if test.status == TestStatus.TEST_CRASHED:

tools/testing/kunit/kunit_tool_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,27 @@ def test_skipped_test(self):
235235
with open(skipped_log) as file:
236236
result = kunit_parser.parse_run_tests(file.readlines(), stdout)
237237

238+
# The test result is skipped, and the skip reason is valid
239+
self.assertEqual(kunit_parser.TestStatus.SKIPPED, result.subtests[1].subtests[1].status)
240+
self.assertEqual("this test should be skipped", result.subtests[1].subtests[1].skip_reason)
241+
238242
# A skipped test does not fail the whole suite.
239243
self.assertEqual(kunit_parser.TestStatus.SUCCESS, result.status)
240244
self.assertEqual(result.counts, kunit_parser.TestCounts(passed=4, skipped=1))
241245

246+
def test_skipped_reason_parse(self):
247+
skipped_log = _test_data_path('test_skip_all_tests.log')
248+
with open(skipped_log) as file:
249+
result = kunit_parser.parse_run_tests(file.readlines(), stdout)
250+
251+
# The first test is skipped, with the correct reaons
252+
self.assertEqual(kunit_parser.TestStatus.SKIPPED, result.subtests[0].subtests[0].status)
253+
self.assertEqual("all tests skipped", result.subtests[0].subtests[0].skip_reason)
254+
255+
# The first suite is skipped, with no reason
256+
self.assertEqual(kunit_parser.TestStatus.SKIPPED, result.subtests[0].status)
257+
self.assertEqual("", result.subtests[0].skip_reason)
258+
242259
def test_skipped_all_tests(self):
243260
skipped_log = _test_data_path('test_skip_all_tests.log')
244261
with open(skipped_log) as file:

0 commit comments

Comments
 (0)