Skip to content

Commit 3c67a2c

Browse files
rmr167shuahkh
authored andcommitted
kunit: tool: print failed tests only
Add flag --failed to kunit.py to print only failed tests. This printing is done after running is over. This patch also adds the method print_test() that will also print your Test object. Before, all printing of tests occurred during parsing. This method could be useful in the future when converting to/from KTAP to this pretty-print output. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rae Moar <[email protected]> Reviewed-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 062a9dd commit 3c67a2c

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

tools/testing/kunit/kunit.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class KunitParseRequest:
5050
raw_output: Optional[str]
5151
json: Optional[str]
5252
summary: bool
53+
failed: bool
5354

5455
@dataclass
5556
class KunitExecRequest(KunitParseRequest):
@@ -237,13 +238,15 @@ def parse_tests(request: KunitParseRequest, metadata: kunit_json.Metadata, input
237238
return KunitResult(KunitStatus.SUCCESS, parse_time), fake_test
238239

239240
default_printer = stdout
240-
if request.summary:
241+
if request.summary or request.failed:
241242
default_printer = null_printer
242243

243244
# Actually parse the test results.
244245
test = kunit_parser.parse_run_tests(input_data, default_printer)
245246
parse_time = time.time() - parse_start
246247

248+
if request.failed:
249+
kunit_parser.print_test(test, request.failed, stdout)
247250
kunit_parser.print_summary_line(test, stdout)
248251

249252
if request.json:
@@ -423,6 +426,10 @@ def add_parse_opts(parser: argparse.ArgumentParser) -> None:
423426
help='Prints only the summary line for parsed test results.'
424427
'Does nothing if --raw_output is set.',
425428
action='store_true')
429+
parser.add_argument('--failed',
430+
help='Prints only the failed parsed test results and summary line.'
431+
'Does nothing if --raw_output is set.',
432+
action='store_true')
426433

427434

428435
def tree_from_args(cli_args: argparse.Namespace) -> kunit_kernel.LinuxSourceTree:
@@ -459,6 +466,7 @@ def run_handler(cli_args: argparse.Namespace) -> None:
459466
raw_output=cli_args.raw_output,
460467
json=cli_args.json,
461468
summary=cli_args.summary,
469+
failed=cli_args.failed,
462470
timeout=cli_args.timeout,
463471
filter_glob=cli_args.filter_glob,
464472
filter=cli_args.filter,
@@ -507,6 +515,7 @@ def exec_handler(cli_args: argparse.Namespace) -> None:
507515
build_dir=cli_args.build_dir,
508516
json=cli_args.json,
509517
summary=cli_args.summary,
518+
failed=cli_args.failed,
510519
timeout=cli_args.timeout,
511520
filter_glob=cli_args.filter_glob,
512521
filter=cli_args.filter,
@@ -532,7 +541,8 @@ def parse_handler(cli_args: argparse.Namespace) -> None:
532541
# We know nothing about how the result was created!
533542
metadata = kunit_json.Metadata()
534543
request = KunitParseRequest(raw_output=cli_args.raw_output,
535-
json=cli_args.json, summary=cli_args.summary)
544+
json=cli_args.json, summary=cli_args.summary,
545+
failed=cli_args.failed)
536546
result, _ = parse_tests(request, metadata, kunit_output)
537547
if result.status != KunitStatus.SUCCESS:
538548
sys.exit(1)

tools/testing/kunit/kunit_parser.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,32 @@ def print_test_footer(test: Test, printer: Printer) -> None:
574574
printer.print_with_timestamp(format_test_divider(message,
575575
len(message) - printer.color_len()))
576576

577+
def print_test(test: Test, failed_only: bool, printer: Printer) -> None:
578+
"""
579+
Prints Test object to given printer. For a child test, the result line is
580+
printed. For a parent test, the test header, all child test results, and
581+
the test footer are all printed. If failed_only is true, only failed/crashed
582+
tests will be printed.
577583
584+
Parameters:
585+
test - Test object to print
586+
failed_only - True if only failed/crashed tests should be printed.
587+
printer - Printer object to output results
588+
"""
589+
if test.name == "main":
590+
printer.print_with_timestamp(DIVIDER)
591+
for subtest in test.subtests:
592+
print_test(subtest, failed_only, printer)
593+
printer.print_with_timestamp(DIVIDER)
594+
elif test.subtests != []:
595+
if not failed_only or not test.ok_status():
596+
print_test_header(test, printer)
597+
for subtest in test.subtests:
598+
print_test(subtest, failed_only, printer)
599+
print_test_footer(test, printer)
600+
else:
601+
if not failed_only or not test.ok_status():
602+
print_test_result(test, printer)
578603

579604
def _summarize_failed_tests(test: Test) -> str:
580605
"""Tries to summarize all the failing subtests in `test`."""

tools/testing/kunit/kunit_tool_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ def test_list_tests(self):
811811
self.linux_source_mock.run_kernel.return_value = ['TAP version 14', 'init: random output'] + want
812812

813813
got = kunit._list_tests(self.linux_source_mock,
814-
kunit.KunitExecRequest(None, None, False, '.kunit', 300, 'suite*', '', None, None, 'suite', False, False))
814+
kunit.KunitExecRequest(None, None, False, False, '.kunit', 300, 'suite*', '', None, None, 'suite', False, False))
815815
self.assertEqual(got, want)
816816
# Should respect the user's filter glob when listing tests.
817817
self.linux_source_mock.run_kernel.assert_called_once_with(
@@ -824,7 +824,7 @@ def test_run_isolated_by_suite(self, mock_tests):
824824

825825
# Should respect the user's filter glob when listing tests.
826826
mock_tests.assert_called_once_with(mock.ANY,
827-
kunit.KunitExecRequest(None, None, False, '.kunit', 300, 'suite*.test*', '', None, None, 'suite', False, False))
827+
kunit.KunitExecRequest(None, None, False, False, '.kunit', 300, 'suite*.test*', '', None, None, 'suite', False, False))
828828
self.linux_source_mock.run_kernel.assert_has_calls([
829829
mock.call(args=None, build_dir='.kunit', filter_glob='suite.test*', filter='', filter_action=None, timeout=300),
830830
mock.call(args=None, build_dir='.kunit', filter_glob='suite2.test*', filter='', filter_action=None, timeout=300),
@@ -837,7 +837,7 @@ def test_run_isolated_by_test(self, mock_tests):
837837

838838
# Should respect the user's filter glob when listing tests.
839839
mock_tests.assert_called_once_with(mock.ANY,
840-
kunit.KunitExecRequest(None, None, False, '.kunit', 300, 'suite*', '', None, None, 'test', False, False))
840+
kunit.KunitExecRequest(None, None, False, False, '.kunit', 300, 'suite*', '', None, None, 'test', False, False))
841841
self.linux_source_mock.run_kernel.assert_has_calls([
842842
mock.call(args=None, build_dir='.kunit', filter_glob='suite.test1', filter='', filter_action=None, timeout=300),
843843
mock.call(args=None, build_dir='.kunit', filter_glob='suite.test2', filter='', filter_action=None, timeout=300),

0 commit comments

Comments
 (0)